Skip to contentSkip to conversation

x86, apic: Add apic calibration hook.

PATCH In review Alok Kataria 2 participants 10 messages Aug 18, 2010 – Sep 1, 2010

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86, apic: Add apic calibration hook.

Add a new function pointer calibrate_apic to x86_platform_ops. On native this does the usual apic calibration. On VMware's platform we override it with a routine which gets that information from the hypervisor.

arch/x86/include/asm/apic.h +1 -0
Index: linux-x86-tree.git/arch/x86/include/asm/apic.h
===================================================================
@@ -238,6 +238,7 @@ extern void setup_boot_APIC_clock(void);
 extern void setup_secondary_APIC_clock(void);
 extern int APIC_init_uniprocessor(void);
 extern void enable_NMI_through_LVT0(void);
+extern unsigned int native_calibrate_apic(void);
 
 /*
  * On 32bit this is mach-xxx local
arch/x86/include/asm/x86_init.h +2 -0
Index: linux-x86-tree.git/arch/x86/include/asm/x86_init.h
===================================================================
@@ -143,6 +143,7 @@ struct x86_cpuinit_ops {
  * @is_untracked_pat_range	exclude from PAT logic
  * @nmi_init			enable NMI on cpus
  * @i8042_detect		pre-detect if i8042 controller exists
+ * @calibrate_apic:		calibrate APIC bus freq
  */
 struct x86_platform_ops {
 	unsigned long (*calibrate_tsc)(void);
@@ -152,6 +153,7 @@ struct x86_platform_ops {
 	bool (*is_untracked_pat_range)(u64 start, u64 end);
 	void (*nmi_init)(void);
 	int (*i8042_detect)(void);
+	unsigned int (*calibrate_apic)(void);
 };
 
 extern struct x86_init_ops x86_init;
arch/x86/kernel/apic/apic.c +39 -13

97 lines not on this page · raw message

arch/x86/kernel/cpu/vmware.c +19 -3
Index: linux-x86-tree.git/arch/x86/kernel/cpu/vmware.c
===================================================================
@@ -72,17 +72,33 @@ static unsigned long vmware_get_tsc_khz(
 	return tsc_hz;
 }
 
+static unsigned int vmware_calibrate_apic(void)
+{
+	unsigned int calibration_result;
+	uint32_t eax, ebx, ecx, edx;
+
+	VMWARE_PORT(GETHZ, eax, ebx, ecx, edx);
+	BUG_ON(!ecx);
+	calibration_result = ecx / HZ;
+
+	printk(KERN_INFO "APIC bus freq read from hypervisor : %u.%03u MHz\n",
+			 calibration_result / (1000000 / HZ),
+			 calibration_result % (1000000 / HZ));
+	return calibration_result;
+}
+
 static void __init vmware_platform_setup(void)
 {
 	uint32_t eax, ebx, ecx, edx;
 
 	VMWARE_PORT(GETHZ, eax, ebx, ecx, edx);
 
-	if (ebx != UINT_MAX)
+	if (ebx != UINT_MAX) {
 		x86_platform.calibrate_tsc = vmware_get_tsc_khz;
-	else
+		x86_platform.calibrate_apic = vmware_calibrate_apic;
+	} else
 		printk(KERN_WARNING
-		       "Failed to get TSC freq from the hypervisor\n");
+		       "Failed to setup VMware hypervisor platform\n");
 }
 
 /*
arch/x86/kernel/x86_init.c +2 -1
Index: linux-x86-tree.git/arch/x86/kernel/x86_init.c
===================================================================
@@ -95,7 +95,8 @@ struct x86_platform_ops x86_platform = {
 	.iommu_shutdown			= iommu_shutdown_noop,
 	.is_untracked_pat_range		= is_ISA_range,
 	.nmi_init			= default_nmi_init,
-	.i8042_detect			= default_i8042_detect
+	.i8042_detect			= default_i8042_detect,
+	.calibrate_apic			= native_calibrate_apic
 };
 
 EXPORT_SYMBOL_GPL(x86_platform);
#1 lore raw
Quoted 1 line · Alok Kataria

Add a new function pointer calibrate_apic to x86_platform_ops.

I guess I should move this in init_ops too, anything else that needs to be changed ?

Thanks,
Alok

Quoted 202 lines

On native this
does the usual apic calibration. On VMware's platform we override it with a routine which gets that information from the hypervisor.

Index: linux-x86-tree.git/arch/x86/include/asm/apic.h
===================================================================

arch/x86/include/asm/apic.h +1 -0
@@ -238,6 +238,7 @@ extern void setup_boot_APIC_clock(void);
 extern void setup_secondary_APIC_clock(void);
 extern int APIC_init_uniprocessor(void);
 extern void enable_NMI_through_LVT0(void);
+extern unsigned int native_calibrate_apic(void);
 
 /*
  * On 32bit this is mach-xxx local

Index: linux-x86-tree.git/arch/x86/include/asm/x86_init.h
===================================================================

arch/x86/include/asm/x86_init.h +2 -0
@@ -143,6 +143,7 @@ struct x86_cpuinit_ops {
  * @is_untracked_pat_range	exclude from PAT logic
  * @nmi_init			enable NMI on cpus
  * @i8042_detect		pre-detect if i8042 controller exists
+ * @calibrate_apic:		calibrate APIC bus freq
  */
 struct x86_platform_ops {
 	unsigned long (*calibrate_tsc)(void);
@@ -152,6 +153,7 @@ struct x86_platform_ops {
 	bool (*is_untracked_pat_range)(u64 start, u64 end);
 	void (*nmi_init)(void);
 	int (*i8042_detect)(void);
+	unsigned int (*calibrate_apic)(void);
 };
 
 extern struct x86_init_ops x86_init;

Index: linux-x86-tree.git/arch/x86/kernel/apic/apic.c
===================================================================

arch/x86/kernel/apic/apic.c +39 -13
@@ -176,7 +176,7 @@ static struct resource lapic_resource = 
 	.flags = IORESOURCE_MEM | IORESOURCE_BUSY,
 };
 
-static unsigned int calibration_result;
+static unsigned int apic_calibration_res;
 
 static int lapic_next_event(unsigned long delta,
 			    struct clock_event_device *evt);
@@ -431,7 +431,7 @@ static void lapic_timer_setup(enum clock
 	switch (mode) {
 	case CLOCK_EVT_MODE_PERIODIC:
 	case CLOCK_EVT_MODE_ONESHOT:
-		__setup_APIC_LVTT(calibration_result,
+		__setup_APIC_LVTT(apic_calibration_res,
 				  mode != CLOCK_EVT_MODE_PERIODIC, 1);
 		break;
 	case CLOCK_EVT_MODE_UNUSED:
@@ -590,13 +590,46 @@ calibrate_by_pmtimer(long deltapm, long 
 	return 0;
 }
 
+static void __init initialize_lapic_clkevt(long delta)
+{
+	/* Calculate the scaled math multiplication factor */
+	lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
+				       lapic_clockevent.shift);
+	lapic_clockevent.max_delta_ns =
+		clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
+	lapic_clockevent.min_delta_ns =
+		clockevent_delta2ns(0xF, &lapic_clockevent);
+}
+
 static int __init calibrate_APIC_clock(void)
 {
+	apic_calibration_res = x86_platform.calibrate_apic();
+	if (apic_calibration_res) {
+		struct clock_event_device *levt = &__get_cpu_var(lapic_events);
+		long delta;
+
+		if (lapic_clockevent.mult)
+			return 0;
+
+		delta = (apic_calibration_res * LAPIC_CAL_LOOPS) / APIC_DIVISOR;
+		initialize_lapic_clkevt(delta);
+		levt->features &= ~CLOCK_EVT_FEAT_DUMMY;
+		return 0;
+	} else {
+		pr_warning("Unable to calibrate APIC frequency, "
+			   "disabling apic timer\n");
+		return -1;
+	}
+}
+
+unsigned int __init native_calibrate_apic(void)
+{
 	struct clock_event_device *levt = &__get_cpu_var(lapic_events);
 	void (*real_handler)(struct clock_event_device *dev);
 	unsigned long deltaj;
 	long delta, deltatsc;
 	int pm_referenced = 0;
+	unsigned int calibration_result;
 
 	local_irq_disable();
 
@@ -631,14 +664,7 @@ static int __init calibrate_APIC_clock(v
 	pm_referenced = !calibrate_by_pmtimer(lapic_cal_pm2 - lapic_cal_pm1,
 					&delta, &deltatsc);
 
-	/* Calculate the scaled math multiplication factor */
-	lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
-				       lapic_clockevent.shift);
-	lapic_clockevent.max_delta_ns =
-		clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
-	lapic_clockevent.min_delta_ns =
-		clockevent_delta2ns(0xF, &lapic_clockevent);
-
+	initialize_lapic_clkevt(delta);
 	calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
 
 	apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
@@ -664,7 +690,7 @@ static int __init calibrate_APIC_clock(v
 	if (calibration_result < (1000000 / HZ)) {
 		local_irq_enable();
 		pr_warning("APIC frequency too slow, disabling apic timer\n");
-		return -1;
+		return 0;
 	}
 
 	levt->features &= ~CLOCK_EVT_FEAT_DUMMY;
@@ -706,10 +732,10 @@ static int __init calibrate_APIC_clock(v
 
 	if (levt->features & CLOCK_EVT_FEAT_DUMMY) {
 		pr_warning("APIC timer disabled due to verification failure\n");
-			return -1;
+			return 0;
 	}
 
-	return 0;
+	return calibration_result;
 }
 
 /*

Index: linux-x86-tree.git/arch/x86/kernel/cpu/vmware.c
===================================================================

arch/x86/kernel/cpu/vmware.c +19 -3
@@ -72,17 +72,33 @@ static unsigned long vmware_get_tsc_khz(
 	return tsc_hz;
 }
 
+static unsigned int vmware_calibrate_apic(void)
+{
+	unsigned int calibration_result;
+	uint32_t eax, ebx, ecx, edx;
+
+	VMWARE_PORT(GETHZ, eax, ebx, ecx, edx);
+	BUG_ON(!ecx);
+	calibration_result = ecx / HZ;
+
+	printk(KERN_INFO "APIC bus freq read from hypervisor : %u.%03u MHz\n",
+			 calibration_result / (1000000 / HZ),
+			 calibration_result % (1000000 / HZ));
+	return calibration_result;
+}
+
 static void __init vmware_platform_setup(void)
 {
 	uint32_t eax, ebx, ecx, edx;
 
 	VMWARE_PORT(GETHZ, eax, ebx, ecx, edx);
 
-	if (ebx != UINT_MAX)
+	if (ebx != UINT_MAX) {
 		x86_platform.calibrate_tsc = vmware_get_tsc_khz;
-	else
+		x86_platform.calibrate_apic = vmware_calibrate_apic;
+	} else
 		printk(KERN_WARNING
-		       "Failed to get TSC freq from the hypervisor\n");
+		       "Failed to setup VMware hypervisor platform\n");
 }
 
 /*

Index: linux-x86-tree.git/arch/x86/kernel/x86_init.c
===================================================================

arch/x86/kernel/x86_init.c +2 -1
@@ -95,7 +95,8 @@ struct x86_platform_ops x86_platform = {
 	.iommu_shutdown			= iommu_shutdown_noop,
 	.is_untracked_pat_range		= is_ISA_range,
 	.nmi_init			= default_nmi_init,
-	.i8042_detect			= default_i8042_detect
+	.i8042_detect			= default_i8042_detect,
+	.calibrate_apic			= native_calibrate_apic
 };
 
 EXPORT_SYMBOL_GPL(x86_platform);
#2 lore raw
In reply to Alok Kataria

Here is the patch which adds this hook in the x86_init_ops.timers structure.

---
Add a new function ptr calibrate_apic to x86_init_ops structure. On native this does the usual apic calibration. On VMware's platform we override it with a routine which gets that information from the hypervisor.

arch/x86/include/asm/apic.h +1 -0
Index: linux-x86-tree.git/arch/x86/include/asm/apic.h
===================================================================
@@ -238,6 +238,7 @@ extern void setup_boot_APIC_clock(void);
 extern void setup_secondary_APIC_clock(void);
 extern int APIC_init_uniprocessor(void);
 extern void enable_NMI_through_LVT0(void);
+extern unsigned int native_calibrate_apic(void);
 
 /*
  * On 32bit this is mach-xxx local
arch/x86/include/asm/x86_init.h +2 -0
Index: linux-x86-tree.git/arch/x86/include/asm/x86_init.h
===================================================================
@@ -83,11 +83,13 @@ struct x86_init_paging {
  *				boot cpu
  * @tsc_pre_init:		platform function called before TSC init
  * @timer_init:			initialize the platform timer (default PIT/HPET)
+ * @calibrate_apic:		calibrate APIC bus freq
  */
 struct x86_init_timers {
 	void (*setup_percpu_clockev)(void);
 	void (*tsc_pre_init)(void);
 	void (*timer_init)(void);
+	unsigned int (*calibrate_apic)(void);
 };
 
 /**
arch/x86/kernel/apic/apic.c +39 -13

97 lines not on this page · raw message

arch/x86/kernel/cpu/vmware.c +19 -3
Index: linux-x86-tree.git/arch/x86/kernel/cpu/vmware.c
===================================================================
@@ -72,17 +72,33 @@ static unsigned long vmware_get_tsc_khz(
 	return tsc_hz;
 }
 
+static unsigned int __init vmware_calibrate_apic(void)
+{
+	unsigned int calibration_result;
+	uint32_t eax, ebx, ecx, edx;
+
+	VMWARE_PORT(GETHZ, eax, ebx, ecx, edx);
+	BUG_ON(!ecx);
+	calibration_result = ecx / HZ;
+
+	printk(KERN_INFO "APIC bus freq read from hypervisor : %u.%03u MHz\n",
+			 calibration_result / (1000000 / HZ),
+			 calibration_result % (1000000 / HZ));
+	return calibration_result;
+}
+
 static void __init vmware_platform_setup(void)
 {
 	uint32_t eax, ebx, ecx, edx;
 
 	VMWARE_PORT(GETHZ, eax, ebx, ecx, edx);
 
-	if (ebx != UINT_MAX)
+	if (ebx != UINT_MAX) {
 		x86_platform.calibrate_tsc = vmware_get_tsc_khz;
-	else
+		x86_init.timers.calibrate_apic = vmware_calibrate_apic;
+	} else
 		printk(KERN_WARNING
-		       "Failed to get TSC freq from the hypervisor\n");
+		       "Failed to setup VMware hypervisor platform\n");
 }
 
 /*
arch/x86/kernel/x86_init.c +2 -1
Index: linux-x86-tree.git/arch/x86/kernel/x86_init.c
===================================================================
@@ -68,6 +68,7 @@ struct x86_init_ops x86_init __initdata 
 		.setup_percpu_clockev	= setup_boot_APIC_clock,
 		.tsc_pre_init		= x86_init_noop,
 		.timer_init		= hpet_time_init,
+		.calibrate_apic		= native_calibrate_apic
 	},
 
 	.iommu = {
@@ -95,7 +96,7 @@ struct x86_platform_ops x86_platform = {
 	.iommu_shutdown			= iommu_shutdown_noop,
 	.is_untracked_pat_range		= is_ISA_range,
 	.nmi_init			= default_nmi_init,
-	.i8042_detect			= default_i8042_detect
+	.i8042_detect			= default_i8042_detect,
 };
 
 EXPORT_SYMBOL_GPL(x86_platform);
Quoted 202 lines · Alok Kataria

Add a new function pointer calibrate_apic to x86_platform_ops. On native this does the usual apic calibration. On VMware's platform we override it with a routine which gets that information from the hypervisor.

Index: linux-x86-tree.git/arch/x86/include/asm/apic.h
===================================================================

arch/x86/include/asm/apic.h +1 -0
@@ -238,6 +238,7 @@ extern void setup_boot_APIC_clock(void);
 extern void setup_secondary_APIC_clock(void);
 extern int APIC_init_uniprocessor(void);
 extern void enable_NMI_through_LVT0(void);
+extern unsigned int native_calibrate_apic(void);
 
 /*
  * On 32bit this is mach-xxx local

Index: linux-x86-tree.git/arch/x86/include/asm/x86_init.h
===================================================================

arch/x86/include/asm/x86_init.h +2 -0
@@ -143,6 +143,7 @@ struct x86_cpuinit_ops {
  * @is_untracked_pat_range	exclude from PAT logic
  * @nmi_init			enable NMI on cpus
  * @i8042_detect		pre-detect if i8042 controller exists
+ * @calibrate_apic:		calibrate APIC bus freq
  */
 struct x86_platform_ops {
 	unsigned long (*calibrate_tsc)(void);
@@ -152,6 +153,7 @@ struct x86_platform_ops {
 	bool (*is_untracked_pat_range)(u64 start, u64 end);
 	void (*nmi_init)(void);
 	int (*i8042_detect)(void);
+	unsigned int (*calibrate_apic)(void);
 };
 
 extern struct x86_init_ops x86_init;

Index: linux-x86-tree.git/arch/x86/kernel/apic/apic.c
===================================================================

arch/x86/kernel/apic/apic.c +39 -13
@@ -176,7 +176,7 @@ static struct resource lapic_resource = 
 	.flags = IORESOURCE_MEM | IORESOURCE_BUSY,
 };
 
-static unsigned int calibration_result;
+static unsigned int apic_calibration_res;
 
 static int lapic_next_event(unsigned long delta,
 			    struct clock_event_device *evt);
@@ -431,7 +431,7 @@ static void lapic_timer_setup(enum clock
 	switch (mode) {
 	case CLOCK_EVT_MODE_PERIODIC:
 	case CLOCK_EVT_MODE_ONESHOT:
-		__setup_APIC_LVTT(calibration_result,
+		__setup_APIC_LVTT(apic_calibration_res,
 				  mode != CLOCK_EVT_MODE_PERIODIC, 1);
 		break;
 	case CLOCK_EVT_MODE_UNUSED:
@@ -590,13 +590,46 @@ calibrate_by_pmtimer(long deltapm, long 
 	return 0;
 }
 
+static void __init initialize_lapic_clkevt(long delta)
+{
+	/* Calculate the scaled math multiplication factor */
+	lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
+				       lapic_clockevent.shift);
+	lapic_clockevent.max_delta_ns =
+		clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
+	lapic_clockevent.min_delta_ns =
+		clockevent_delta2ns(0xF, &lapic_clockevent);
+}
+
 static int __init calibrate_APIC_clock(void)
 {
+	apic_calibration_res = x86_platform.calibrate_apic();
+	if (apic_calibration_res) {
+		struct clock_event_device *levt = &__get_cpu_var(lapic_events);
+		long delta;
+
+		if (lapic_clockevent.mult)
+			return 0;
+
+		delta = (apic_calibration_res * LAPIC_CAL_LOOPS) / APIC_DIVISOR;
+		initialize_lapic_clkevt(delta);
+		levt->features &= ~CLOCK_EVT_FEAT_DUMMY;
+		return 0;
+	} else {
+		pr_warning("Unable to calibrate APIC frequency, "
+			   "disabling apic timer\n");
+		return -1;
+	}
+}
+
+unsigned int __init native_calibrate_apic(void)
+{
 	struct clock_event_device *levt = &__get_cpu_var(lapic_events);
 	void (*real_handler)(struct clock_event_device *dev);
 	unsigned long deltaj;
 	long delta, deltatsc;
 	int pm_referenced = 0;
+	unsigned int calibration_result;
 
 	local_irq_disable();
 
@@ -631,14 +664,7 @@ static int __init calibrate_APIC_clock(v
 	pm_referenced = !calibrate_by_pmtimer(lapic_cal_pm2 - lapic_cal_pm1,
 					&delta, &deltatsc);
 
-	/* Calculate the scaled math multiplication factor */
-	lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
-				       lapic_clockevent.shift);
-	lapic_clockevent.max_delta_ns =
-		clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
-	lapic_clockevent.min_delta_ns =
-		clockevent_delta2ns(0xF, &lapic_clockevent);
-
+	initialize_lapic_clkevt(delta);
 	calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
 
 	apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
@@ -664,7 +690,7 @@ static int __init calibrate_APIC_clock(v
 	if (calibration_result < (1000000 / HZ)) {
 		local_irq_enable();
 		pr_warning("APIC frequency too slow, disabling apic timer\n");
-		return -1;
+		return 0;
 	}
 
 	levt->features &= ~CLOCK_EVT_FEAT_DUMMY;
@@ -706,10 +732,10 @@ static int __init calibrate_APIC_clock(v
 
 	if (levt->features & CLOCK_EVT_FEAT_DUMMY) {
 		pr_warning("APIC timer disabled due to verification failure\n");
-			return -1;
+			return 0;
 	}
 
-	return 0;
+	return calibration_result;
 }
 
 /*

Index: linux-x86-tree.git/arch/x86/kernel/cpu/vmware.c
===================================================================

arch/x86/kernel/cpu/vmware.c +19 -3
@@ -72,17 +72,33 @@ static unsigned long vmware_get_tsc_khz(
 	return tsc_hz;
 }
 
+static unsigned int vmware_calibrate_apic(void)
+{
+	unsigned int calibration_result;
+	uint32_t eax, ebx, ecx, edx;
+
+	VMWARE_PORT(GETHZ, eax, ebx, ecx, edx);
+	BUG_ON(!ecx);
+	calibration_result = ecx / HZ;
+
+	printk(KERN_INFO "APIC bus freq read from hypervisor : %u.%03u MHz\n",
+			 calibration_result / (1000000 / HZ),
+			 calibration_result % (1000000 / HZ));
+	return calibration_result;
+}
+
 static void __init vmware_platform_setup(void)
 {
 	uint32_t eax, ebx, ecx, edx;
 
 	VMWARE_PORT(GETHZ, eax, ebx, ecx, edx);
 
-	if (ebx != UINT_MAX)
+	if (ebx != UINT_MAX) {
 		x86_platform.calibrate_tsc = vmware_get_tsc_khz;
-	else
+		x86_platform.calibrate_apic = vmware_calibrate_apic;
+	} else
 		printk(KERN_WARNING
-		       "Failed to get TSC freq from the hypervisor\n");
+		       "Failed to setup VMware hypervisor platform\n");
 }
 
 /*

Index: linux-x86-tree.git/arch/x86/kernel/x86_init.c
===================================================================

arch/x86/kernel/x86_init.c +2 -1
@@ -95,7 +95,8 @@ struct x86_platform_ops x86_platform = {
 	.iommu_shutdown			= iommu_shutdown_noop,
 	.is_untracked_pat_range		= is_ISA_range,
 	.nmi_init			= default_nmi_init,
-	.i8042_detect			= default_i8042_detect
+	.i8042_detect			= default_i8042_detect,
+	.calibrate_apic			= native_calibrate_apic
 };
 
 EXPORT_SYMBOL_GPL(x86_platform);
#3 lore raw
Quoted 8 lines · Alok Kataria

Here is the patch which adds this hook in the x86_init_ops.timers structure.

---
Add a new function ptr calibrate_apic to x86_init_ops structure. On native this does the usual apic calibration. On VMware's platform we override it with a routine which gets that information from the hypervisor.

Any comments on this one ?

Thanks in advance.
Alok

Quoted 407 lines

Index: linux-x86-tree.git/arch/x86/include/asm/apic.h
===================================================================

arch/x86/include/asm/apic.h +1 -0
@@ -238,6 +238,7 @@ extern void setup_boot_APIC_clock(void);
 extern void setup_secondary_APIC_clock(void);
 extern int APIC_init_uniprocessor(void);
 extern void enable_NMI_through_LVT0(void);
+extern unsigned int native_calibrate_apic(void);
 
 /*
  * On 32bit this is mach-xxx local

Index: linux-x86-tree.git/arch/x86/include/asm/x86_init.h
===================================================================

arch/x86/include/asm/x86_init.h +2 -0
@@ -83,11 +83,13 @@ struct x86_init_paging {
  *				boot cpu
  * @tsc_pre_init:		platform function called before TSC init
  * @timer_init:			initialize the platform timer (default PIT/HPET)
+ * @calibrate_apic:		calibrate APIC bus freq
  */
 struct x86_init_timers {
 	void (*setup_percpu_clockev)(void);
 	void (*tsc_pre_init)(void);
 	void (*timer_init)(void);
+	unsigned int (*calibrate_apic)(void);
 };
 
 /**

Index: linux-x86-tree.git/arch/x86/kernel/apic/apic.c
===================================================================

arch/x86/kernel/apic/apic.c +39 -13
@@ -176,7 +176,7 @@ static struct resource lapic_resource = 
 	.flags = IORESOURCE_MEM | IORESOURCE_BUSY,
 };
 
-static unsigned int calibration_result;
+static unsigned int apic_calibration_res;
 
 static int lapic_next_event(unsigned long delta,
 			    struct clock_event_device *evt);
@@ -431,7 +431,7 @@ static void lapic_timer_setup(enum clock
 	switch (mode) {
 	case CLOCK_EVT_MODE_PERIODIC:
 	case CLOCK_EVT_MODE_ONESHOT:
-		__setup_APIC_LVTT(calibration_result,
+		__setup_APIC_LVTT(apic_calibration_res,
 				  mode != CLOCK_EVT_MODE_PERIODIC, 1);
 		break;
 	case CLOCK_EVT_MODE_UNUSED:
@@ -590,13 +590,46 @@ calibrate_by_pmtimer(long deltapm, long 
 	return 0;
 }
 
+static void __init initialize_lapic_clkevt(long delta)
+{
+	/* Calculate the scaled math multiplication factor */
+	lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
+				       lapic_clockevent.shift);
+	lapic_clockevent.max_delta_ns =
+		clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
+	lapic_clockevent.min_delta_ns =
+		clockevent_delta2ns(0xF, &lapic_clockevent);
+}
+
 static int __init calibrate_APIC_clock(void)
 {
+	apic_calibration_res = x86_init.timers.calibrate_apic();
+	if (apic_calibration_res) {
+		struct clock_event_device *levt = &__get_cpu_var(lapic_events);
+		long delta;
+
+		if (lapic_clockevent.mult)
+			return 0;
+
+		delta = (apic_calibration_res * LAPIC_CAL_LOOPS) / APIC_DIVISOR;
+		initialize_lapic_clkevt(delta);
+		levt->features &= ~CLOCK_EVT_FEAT_DUMMY;
+		return 0;
+	} else {
+		pr_warning("Unable to calibrate APIC frequency, "
+			   "disabling apic timer\n");
+		return -1;
+	}
+}
+
+unsigned int __init native_calibrate_apic(void)
+{
 	struct clock_event_device *levt = &__get_cpu_var(lapic_events);
 	void (*real_handler)(struct clock_event_device *dev);
 	unsigned long deltaj;
 	long delta, deltatsc;
 	int pm_referenced = 0;
+	unsigned int calibration_result;
 
 	local_irq_disable();
 
@@ -631,14 +664,7 @@ static int __init calibrate_APIC_clock(v
 	pm_referenced = !calibrate_by_pmtimer(lapic_cal_pm2 - lapic_cal_pm1,
 					&delta, &deltatsc);
 
-	/* Calculate the scaled math multiplication factor */
-	lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
-				       lapic_clockevent.shift);
-	lapic_clockevent.max_delta_ns =
-		clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
-	lapic_clockevent.min_delta_ns =
-		clockevent_delta2ns(0xF, &lapic_clockevent);
-
+	initialize_lapic_clkevt(delta);
 	calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
 
 	apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
@@ -664,7 +690,7 @@ static int __init calibrate_APIC_clock(v
 	if (calibration_result < (1000000 / HZ)) {
 		local_irq_enable();
 		pr_warning("APIC frequency too slow, disabling apic timer\n");
-		return -1;
+		return 0;
 	}
 
 	levt->features &= ~CLOCK_EVT_FEAT_DUMMY;
@@ -706,10 +732,10 @@ static int __init calibrate_APIC_clock(v
 
 	if (levt->features & CLOCK_EVT_FEAT_DUMMY) {
 		pr_warning("APIC timer disabled due to verification failure\n");
-			return -1;
+			return 0;
 	}
 
-	return 0;
+	return calibration_result;
 }
 
 /*

Index: linux-x86-tree.git/arch/x86/kernel/cpu/vmware.c
===================================================================

arch/x86/kernel/cpu/vmware.c +19 -3
@@ -72,17 +72,33 @@ static unsigned long vmware_get_tsc_khz(
 	return tsc_hz;
 }
 
+static unsigned int __init vmware_calibrate_apic(void)
+{
+	unsigned int calibration_result;
+	uint32_t eax, ebx, ecx, edx;
+
+	VMWARE_PORT(GETHZ, eax, ebx, ecx, edx);
+	BUG_ON(!ecx);
+	calibration_result = ecx / HZ;
+
+	printk(KERN_INFO "APIC bus freq read from hypervisor : %u.%03u MHz\n",
+			 calibration_result / (1000000 / HZ),
+			 calibration_result % (1000000 / HZ));
+	return calibration_result;
+}
+
 static void __init vmware_platform_setup(void)
 {
 	uint32_t eax, ebx, ecx, edx;
 
 	VMWARE_PORT(GETHZ, eax, ebx, ecx, edx);
 
-	if (ebx != UINT_MAX)
+	if (ebx != UINT_MAX) {
 		x86_platform.calibrate_tsc = vmware_get_tsc_khz;
-	else
+		x86_init.timers.calibrate_apic = vmware_calibrate_apic;
+	} else
 		printk(KERN_WARNING
-		       "Failed to get TSC freq from the hypervisor\n");
+		       "Failed to setup VMware hypervisor platform\n");
 }
 
 /*

Index: linux-x86-tree.git/arch/x86/kernel/x86_init.c
===================================================================

arch/x86/kernel/x86_init.c +2 -1
@@ -68,6 +68,7 @@ struct x86_init_ops x86_init __initdata 
 		.setup_percpu_clockev	= setup_boot_APIC_clock,
 		.tsc_pre_init		= x86_init_noop,
 		.timer_init		= hpet_time_init,
+		.calibrate_apic		= native_calibrate_apic
 	},
 
 	.iommu = {
@@ -95,7 +96,7 @@ struct x86_platform_ops x86_platform = {
 	.iommu_shutdown			= iommu_shutdown_noop,
 	.is_untracked_pat_range		= is_ISA_range,
 	.nmi_init			= default_nmi_init,
-	.i8042_detect			= default_i8042_detect
+	.i8042_detect			= default_i8042_detect,
 };
 
 EXPORT_SYMBOL_GPL(x86_platform);
Quoted 202 lines · Alok Kataria

Add a new function pointer calibrate_apic to x86_platform_ops. On native this does the usual apic calibration. On VMware's platform we override it with a routine which gets that information from the hypervisor.

Index: linux-x86-tree.git/arch/x86/include/asm/apic.h
===================================================================

arch/x86/include/asm/apic.h +1 -0
@@ -238,6 +238,7 @@ extern void setup_boot_APIC_clock(void);
 extern void setup_secondary_APIC_clock(void);
 extern int APIC_init_uniprocessor(void);
 extern void enable_NMI_through_LVT0(void);
+extern unsigned int native_calibrate_apic(void);
 
 /*
  * On 32bit this is mach-xxx local

Index: linux-x86-tree.git/arch/x86/include/asm/x86_init.h
===================================================================

arch/x86/include/asm/x86_init.h +2 -0
@@ -143,6 +143,7 @@ struct x86_cpuinit_ops {
  * @is_untracked_pat_range	exclude from PAT logic
  * @nmi_init			enable NMI on cpus
  * @i8042_detect		pre-detect if i8042 controller exists
+ * @calibrate_apic:		calibrate APIC bus freq
  */
 struct x86_platform_ops {
 	unsigned long (*calibrate_tsc)(void);
@@ -152,6 +153,7 @@ struct x86_platform_ops {
 	bool (*is_untracked_pat_range)(u64 start, u64 end);
 	void (*nmi_init)(void);
 	int (*i8042_detect)(void);
+	unsigned int (*calibrate_apic)(void);
 };
 
 extern struct x86_init_ops x86_init;

Index: linux-x86-tree.git/arch/x86/kernel/apic/apic.c
===================================================================

arch/x86/kernel/apic/apic.c +39 -13
@@ -176,7 +176,7 @@ static struct resource lapic_resource = 
 	.flags = IORESOURCE_MEM | IORESOURCE_BUSY,
 };
 
-static unsigned int calibration_result;
+static unsigned int apic_calibration_res;
 
 static int lapic_next_event(unsigned long delta,
 			    struct clock_event_device *evt);
@@ -431,7 +431,7 @@ static void lapic_timer_setup(enum clock
 	switch (mode) {
 	case CLOCK_EVT_MODE_PERIODIC:
 	case CLOCK_EVT_MODE_ONESHOT:
-		__setup_APIC_LVTT(calibration_result,
+		__setup_APIC_LVTT(apic_calibration_res,
 				  mode != CLOCK_EVT_MODE_PERIODIC, 1);
 		break;
 	case CLOCK_EVT_MODE_UNUSED:
@@ -590,13 +590,46 @@ calibrate_by_pmtimer(long deltapm, long 
 	return 0;
 }
 
+static void __init initialize_lapic_clkevt(long delta)
+{
+	/* Calculate the scaled math multiplication factor */
+	lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
+				       lapic_clockevent.shift);
+	lapic_clockevent.max_delta_ns =
+		clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
+	lapic_clockevent.min_delta_ns =
+		clockevent_delta2ns(0xF, &lapic_clockevent);
+}
+
 static int __init calibrate_APIC_clock(void)
 {
+	apic_calibration_res = x86_platform.calibrate_apic();
+	if (apic_calibration_res) {
+		struct clock_event_device *levt = &__get_cpu_var(lapic_events);
+		long delta;
+
+		if (lapic_clockevent.mult)
+			return 0;
+
+		delta = (apic_calibration_res * LAPIC_CAL_LOOPS) / APIC_DIVISOR;
+		initialize_lapic_clkevt(delta);
+		levt->features &= ~CLOCK_EVT_FEAT_DUMMY;
+		return 0;
+	} else {
+		pr_warning("Unable to calibrate APIC frequency, "
+			   "disabling apic timer\n");
+		return -1;
+	}
+}
+
+unsigned int __init native_calibrate_apic(void)
+{
 	struct clock_event_device *levt = &__get_cpu_var(lapic_events);
 	void (*real_handler)(struct clock_event_device *dev);
 	unsigned long deltaj;
 	long delta, deltatsc;
 	int pm_referenced = 0;
+	unsigned int calibration_result;
 
 	local_irq_disable();
 
@@ -631,14 +664,7 @@ static int __init calibrate_APIC_clock(v
 	pm_referenced = !calibrate_by_pmtimer(lapic_cal_pm2 - lapic_cal_pm1,
 					&delta, &deltatsc);
 
-	/* Calculate the scaled math multiplication factor */
-	lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
-				       lapic_clockevent.shift);
-	lapic_clockevent.max_delta_ns =
-		clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
-	lapic_clockevent.min_delta_ns =
-		clockevent_delta2ns(0xF, &lapic_clockevent);
-
+	initialize_lapic_clkevt(delta);
 	calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
 
 	apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
@@ -664,7 +690,7 @@ static int __init calibrate_APIC_clock(v
 	if (calibration_result < (1000000 / HZ)) {
 		local_irq_enable();
 		pr_warning("APIC frequency too slow, disabling apic timer\n");
-		return -1;
+		return 0;
 	}
 
 	levt->features &= ~CLOCK_EVT_FEAT_DUMMY;
@@ -706,10 +732,10 @@ static int __init calibrate_APIC_clock(v
 
 	if (levt->features & CLOCK_EVT_FEAT_DUMMY) {
 		pr_warning("APIC timer disabled due to verification failure\n");
-			return -1;
+			return 0;
 	}
 
-	return 0;
+	return calibration_result;
 }
 
 /*

Index: linux-x86-tree.git/arch/x86/kernel/cpu/vmware.c
===================================================================

arch/x86/kernel/cpu/vmware.c +19 -3
@@ -72,17 +72,33 @@ static unsigned long vmware_get_tsc_khz(
 	return tsc_hz;
 }
 
+static unsigned int vmware_calibrate_apic(void)
+{
+	unsigned int calibration_result;
+	uint32_t eax, ebx, ecx, edx;
+
+	VMWARE_PORT(GETHZ, eax, ebx, ecx, edx);
+	BUG_ON(!ecx);
+	calibration_result = ecx / HZ;
+
+	printk(KERN_INFO "APIC bus freq read from hypervisor : %u.%03u MHz\n",
+			 calibration_result / (1000000 / HZ),
+			 calibration_result % (1000000 / HZ));
+	return calibration_result;
+}
+
 static void __init vmware_platform_setup(void)
 {
 	uint32_t eax, ebx, ecx, edx;
 
 	VMWARE_PORT(GETHZ, eax, ebx, ecx, edx);
 
-	if (ebx != UINT_MAX)
+	if (ebx != UINT_MAX) {
 		x86_platform.calibrate_tsc = vmware_get_tsc_khz;
-	else
+		x86_platform.calibrate_apic = vmware_calibrate_apic;
+	} else
 		printk(KERN_WARNING
-		       "Failed to get TSC freq from the hypervisor\n");
+		       "Failed to setup VMware hypervisor platform\n");
 }
 
 /*

Index: linux-x86-tree.git/arch/x86/kernel/x86_init.c
===================================================================

arch/x86/kernel/x86_init.c +2 -1
@@ -95,7 +95,8 @@ struct x86_platform_ops x86_platform = {
 	.iommu_shutdown			= iommu_shutdown_noop,
 	.is_untracked_pat_range		= is_ISA_range,
 	.nmi_init			= default_nmi_init,
-	.i8042_detect			= default_i8042_detect
+	.i8042_detect			= default_i8042_detect,
+	.calibrate_apic			= native_calibrate_apic
 };
 
 EXPORT_SYMBOL_GPL(x86_platform);
#4 lore raw
In reply to Alok Kataria
Quoted 11 lines · Alok Kataria
arch/x86/kernel/apic/apic.c +10 -0
+static void __init initialize_lapic_clkevt(long delta)
+{
+	/* Calculate the scaled math multiplication factor */
+	lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
+				       lapic_clockevent.shift);
+	lapic_clockevent.max_delta_ns =
+		clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
+	lapic_clockevent.min_delta_ns =
+		clockevent_delta2ns(0xF, &lapic_clockevent);
+}

arch/x86/kernel/apic/apic.c calibrate_by_pmtimer(long deltapm, long · line 612

Quoted 10 lines
arch/x86/kernel/apic/apic.c +8 -0
+
 static int __init calibrate_APIC_clock(void)
 {
+	apic_calibration_res = x86_init.timers.calibrate_apic();
+	if (apic_calibration_res) {
+		struct clock_event_device *levt = &__get_cpu_var(lapic_events);
+		long delta;
+
+		if (lapic_clockevent.mult)
+			return 0;

Why is this necessary ? calibrate_APIC_clock() is only called once.

Thanks,

tglx

#5 lore raw

Hi Thomas,

Thanks for taking a look.

Quoted 16 lines · Thomas Gleixner
Quoted 15 lines · Alok Kataria
arch/x86/kernel/apic/apic.c +12 -0
+static void __init initialize_lapic_clkevt(long delta)
+{
+	/* Calculate the scaled math multiplication factor */
+	lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
+				       lapic_clockevent.shift);
+	lapic_clockevent.max_delta_ns =
+		clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
+	lapic_clockevent.min_delta_ns =
+		clockevent_delta2ns(0xF, &lapic_clockevent);
+}
+
 static int __init calibrate_APIC_clock(void)
 {
+	apic_calibration_res = x86_init.timers.calibrate_apic();
Quoted 9 lines

arch/x86/kernel/apic/apic.c calibrate_by_pmtimer(long deltapm, long · line 612

Quoted 6 lines
arch/x86/kernel/apic/apic.c +6 -0
+	if (apic_calibration_res) {
+		struct clock_event_device *levt = &__get_cpu_var(lapic_events);
+		long delta;
+
+		if (lapic_clockevent.mult)
+			return 0;

Why is this necessary ? calibrate_APIC_clock() is only called once.

Yes, though timer.calibrate_apci() could behave differently on different platforms. For eg. the vmware_calibrate_apic doesn't initialize any of the lapic_clockevent state and we need this to set it up in that case.

Thanks,
Alok

#6 lore raw

Alok,

Quoted 31 lines · Alok Kataria

Hi Thomas,

Thanks for taking a look.

Quoted 16 lines · Thomas Gleixner
Quoted 15 lines · Alok Kataria
arch/x86/kernel/apic/apic.c +12 -0
+static void __init initialize_lapic_clkevt(long delta)
+{
+	/* Calculate the scaled math multiplication factor */
+	lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
+				       lapic_clockevent.shift);
+	lapic_clockevent.max_delta_ns =
+		clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
+	lapic_clockevent.min_delta_ns =
+		clockevent_delta2ns(0xF, &lapic_clockevent);
+}
+
 static int __init calibrate_APIC_clock(void)
 {
+	apic_calibration_res = x86_init.timers.calibrate_apic();
Quoted 9 lines

arch/x86/kernel/apic/apic.c calibrate_by_pmtimer(long deltapm, long · line 612

Quoted 6 lines
arch/x86/kernel/apic/apic.c +6 -0
+	if (apic_calibration_res) {
+		struct clock_event_device *levt = &__get_cpu_var(lapic_events);
+		long delta;
+
+		if (lapic_clockevent.mult)
+			return 0;

Why is this necessary ? calibrate_APIC_clock() is only called once.

Quoted 3 lines

Yes, though timer.calibrate_apci() could behave differently on different platforms. For eg. the vmware_calibrate_apic doesn't initialize any of the lapic_clockevent state and we need this to set it up in that case.

Can we please make this consistent ?

Thanks,

tglx

#7 lore raw
Quoted 35 lines · Thomas Gleixner

Alok,

Quoted 31 lines · Alok Kataria

Hi Thomas,

Thanks for taking a look.

Quoted 25 lines · Thomas Gleixner
Quoted 11 lines · Alok Kataria
arch/x86/kernel/apic/apic.c +10 -0
+static void __init initialize_lapic_clkevt(long delta)
+{
+	/* Calculate the scaled math multiplication factor */
+	lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
+				       lapic_clockevent.shift);
+	lapic_clockevent.max_delta_ns =
+		clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
+	lapic_clockevent.min_delta_ns =
+		clockevent_delta2ns(0xF, &lapic_clockevent);
+}

arch/x86/kernel/apic/apic.c calibrate_by_pmtimer(long deltapm, long · line 612

Quoted 10 lines
arch/x86/kernel/apic/apic.c +8 -0
+
 static int __init calibrate_APIC_clock(void)
 {
+	apic_calibration_res = x86_init.timers.calibrate_apic();
+	if (apic_calibration_res) {
+		struct clock_event_device *levt = &__get_cpu_var(lapic_events);
+		long delta;
+
+		if (lapic_clockevent.mult)
+			return 0;

Why is this necessary ? calibrate_APIC_clock() is only called once.

Quoted 5 lines
Quoted 3 lines

Yes, though timer.calibrate_apci() could behave differently on different platforms. For eg. the vmware_calibrate_apic doesn't initialize any of the lapic_clockevent state and we need this to set it up in that case.

Can we please make this consistent ?

Let me rework this patch and get back in a day or two.

Thanks,
Alok

Quoted 4 lines

Thanks,

tglx

#8 lore raw

Hi Thomas,

Quoted 9 lines · Alok Kataria
Quoted 8 lines · Thomas Gleixner
Quoted 7 lines · Alok Kataria
Quoted 6 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+
+		if (lapic_clockevent.mult)
+			return 0;

Why is this necessary ? calibrate_APIC_clock() is only called once.

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

Yes, though timer.calibrate_apci() could behave differently on different platforms. For eg. the vmware_calibrate_apic doesn't initialize any of the lapic_clockevent state and we need this to set it up in that case.

Can we please make this consistent ?

How does this patch look ?

Thanks,
Alok

--

Add apic calibration hook.

From: Alok N Kataria <[email protected]>

Add a new function ptr calibrate_apic to x86_init_ops structure. On native this does the usual apic calibration. On VMware's platform we override it with a routine which gets that information from the hypervisor.

---

 arch/x86/include/asm/apic.h     |    6 +++
 arch/x86/include/asm/x86_init.h |    2 +
 arch/x86/kernel/apic/apic.c     |   88 ++++++++++++++++++++-------------------
 arch/x86/kernel/cpu/vmware.c    |   30 ++++++++++++-
 arch/x86/kernel/x86_init.c      |    1 
 5 files changed, 82 insertions(+), 45 deletions(-)
arch/x86/include/asm/apic.h +6 -0
index 1fa03e0..5012ee5 100644
@@ -17,6 +17,10 @@
 
 #define ARCH_APICTIMER_STOPS_ON_C3	1
 
+/* Clock divisor */
+#define APIC_DIVISOR	16
+#define LAPIC_CAL_LOOPS	(HZ/10)
+
 /*
  * Debugging macros
  */
@@ -238,6 +242,8 @@ extern void setup_boot_APIC_clock(void);
 extern void setup_secondary_APIC_clock(void);
 extern int APIC_init_uniprocessor(void);
 extern void enable_NMI_through_LVT0(void);
+extern int native_calibrate_apic(void);
+extern int initialize_lapic(long delta, unsigned int calibration_result);
 
 /*
  * On 32bit this is mach-xxx local
arch/x86/include/asm/x86_init.h +2 -0
index baa579c..58f982b 100644
@@ -83,11 +83,13 @@ struct x86_init_paging {
  *				boot cpu
  * @tsc_pre_init:		platform function called before TSC init
  * @timer_init:			initialize the platform timer (default PIT/HPET)
+ * @calibrate_apic:		calibrate APIC bus freq (ticks per HZ)
  */
 struct x86_init_timers {
 	void (*setup_percpu_clockev)(void);
 	void (*tsc_pre_init)(void);
 	void (*timer_init)(void);
+	int (*calibrate_apic)(void);
 };
 
 /**
arch/x86/kernel/apic/apic.c +46 -42

150 lines not on this page · raw message

arch/x86/kernel/cpu/vmware.c +27 -3
index 227b044..e8b760e 100644
@@ -26,6 +26,7 @@
 #include <asm/div64.h>
 #include <asm/x86_init.h>
 #include <asm/hypervisor.h>
+#include <asm/apic.h>
 
 #define CPUID_VMWARE_INFO_LEAF	0x40000000
 #define VMWARE_HYPERVISOR_MAGIC	0x564D5868
@@ -72,17 +73,40 @@ static unsigned long vmware_get_tsc_khz(void)
 	return tsc_hz;
 }
 
+static int __init vmware_calibrate_apic(void)
+{
+	unsigned int calibration_result;
+	uint32_t eax, ebx, ecx, edx;
+	int err;
+	long delta;
+	unsigned long flags;
+
+	VMWARE_PORT(GETHZ, eax, ebx, ecx, edx);
+	BUG_ON(!ecx);
+	calibration_result = ecx / HZ;
+
+	printk(KERN_INFO "APIC bus freq read from hypervisor\n");
+
+	delta  = (calibration_result * LAPIC_CAL_LOOPS) / APIC_DIVISOR;
+	local_irq_save(flags);
+	err = initialize_lapic(delta, calibration_result);
+	local_irq_restore(flags);
+
+	return err;
+}
+
 static void __init vmware_platform_setup(void)
 {
 	uint32_t eax, ebx, ecx, edx;
 
 	VMWARE_PORT(GETHZ, eax, ebx, ecx, edx);
 
-	if (ebx != UINT_MAX)
+	if (ebx != UINT_MAX) {
 		x86_platform.calibrate_tsc = vmware_get_tsc_khz;
-	else
+		x86_init.timers.calibrate_apic = vmware_calibrate_apic;
+	} else
 		printk(KERN_WARNING
-		       "Failed to get TSC freq from the hypervisor\n");
+		       "Failed to setup VMware hypervisor platform\n");
 }
 
 /*
arch/x86/kernel/x86_init.c +1 -0
index cd6da6b..f162e14 100644
@@ -68,6 +68,7 @@ struct x86_init_ops x86_init __initdata = {
 		.setup_percpu_clockev	= setup_boot_APIC_clock,
 		.tsc_pre_init		= x86_init_noop,
 		.timer_init		= hpet_time_init,
+		.calibrate_apic		= native_calibrate_apic
 	},
 
 	.iommu = {
#9 lore raw
Quoted 12 lines · Alok Kataria

Hi Thomas,

Quoted 9 lines · Alok Kataria
Quoted 8 lines · Thomas Gleixner
Quoted 7 lines · Alok Kataria
Quoted 6 lines

arch/x86/kernel/apic/apic.c calibrate_by_pmtimer(long deltapm, long *delta, long *deltatsc) · line 617

Quoted 3 lines
arch/x86/kernel/apic/apic.c +3 -0
+
+		if (lapic_clockevent.mult)
+			return 0;

Why is this necessary ? calibrate_APIC_clock() is only called once.

Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

Yes, though timer.calibrate_apci() could behave differently on different platforms. For eg. the vmware_calibrate_apic doesn't initialize any of the lapic_clockevent state and we need this to set it up in that case.

Can we please make this consistent ?

How does this patch look ?

Thomas, any comments on the patch below, just want to make sure that this didn't fell through the cracks.

Thanks,
Alok

Quoted 296 lines

--

Add apic calibration hook.

From: Alok N Kataria <[email protected]>

Add a new function ptr calibrate_apic to x86_init_ops structure. On native this does the usual apic calibration. On VMware's platform we override it with a routine which gets that information from the hypervisor.

---

 arch/x86/include/asm/apic.h     |    6 +++
 arch/x86/include/asm/x86_init.h |    2 +
 arch/x86/kernel/apic/apic.c     |   88 ++++++++++++++++++++-------------------
 arch/x86/kernel/cpu/vmware.c    |   30 ++++++++++++-
 arch/x86/kernel/x86_init.c      |    1 
 5 files changed, 82 insertions(+), 45 deletions(-)
arch/x86/include/asm/apic.h +6 -0
index 1fa03e0..5012ee5 100644
@@ -17,6 +17,10 @@
 
 #define ARCH_APICTIMER_STOPS_ON_C3	1
 
+/* Clock divisor */
+#define APIC_DIVISOR	16
+#define LAPIC_CAL_LOOPS	(HZ/10)
+
 /*
  * Debugging macros
  */
@@ -238,6 +242,8 @@ extern void setup_boot_APIC_clock(void);
 extern void setup_secondary_APIC_clock(void);
 extern int APIC_init_uniprocessor(void);
 extern void enable_NMI_through_LVT0(void);
+extern int native_calibrate_apic(void);
+extern int initialize_lapic(long delta, unsigned int calibration_result);
 
 /*
  * On 32bit this is mach-xxx local
arch/x86/include/asm/x86_init.h +2 -0
index baa579c..58f982b 100644
@@ -83,11 +83,13 @@ struct x86_init_paging {
  *				boot cpu
  * @tsc_pre_init:		platform function called before TSC init
  * @timer_init:			initialize the platform timer (default PIT/HPET)
+ * @calibrate_apic:		calibrate APIC bus freq (ticks per HZ)
  */
 struct x86_init_timers {
 	void (*setup_percpu_clockev)(void);
 	void (*tsc_pre_init)(void);
 	void (*timer_init)(void);
+	int (*calibrate_apic)(void);
 };
 
 /**
arch/x86/kernel/apic/apic.c +46 -42
index e3b534c..ca5d084 100644
@@ -176,7 +176,7 @@ static struct resource lapic_resource = {
 	.flags = IORESOURCE_MEM | IORESOURCE_BUSY,
 };
 
-static unsigned int calibration_result;
+static unsigned int apic_calibration_res;
 
 static int lapic_next_event(unsigned long delta,
 			    struct clock_event_device *evt);
@@ -326,13 +326,6 @@ int lapic_get_maxlvt(void)
 }
 
 /*
- * Local APIC timer
- */
-
-/* Clock divisor */
-#define APIC_DIVISOR 16
-
-/*
  * This function sets up the local APIC timer, with a timeout of
  * 'clocks' APIC bus clock. During calibration we actually call
  * this function twice on the boot CPU, once with a bogus timeout
@@ -431,7 +424,7 @@ static void lapic_timer_setup(enum clock_event_mode mode,
 	switch (mode) {
 	case CLOCK_EVT_MODE_PERIODIC:
 	case CLOCK_EVT_MODE_ONESHOT:
-		__setup_APIC_LVTT(calibration_result,
+		__setup_APIC_LVTT(apic_calibration_res,
 				  mode != CLOCK_EVT_MODE_PERIODIC, 1);
 		break;
 	case CLOCK_EVT_MODE_UNUSED:
@@ -500,8 +493,6 @@ static void __cpuinit setup_APIC_timer(void)
  * back to normal later in the boot process).
  */
 
-#define LAPIC_CAL_LOOPS		(HZ/10)
-
 static __initdata int lapic_cal_loops = -1;
 static __initdata long lapic_cal_t1, lapic_cal_t2;
 static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
@@ -590,13 +581,50 @@ calibrate_by_pmtimer(long deltapm, long *delta, long *deltatsc)
 	return 0;
 }
 
-static int __init calibrate_APIC_clock(void)
+int __init initialize_lapic(long delta, unsigned int calibration_result)
+{
+	struct clock_event_device *levt = &__get_cpu_var(lapic_events);
+
+	/* Calculate the scaled math multiplication factor */
+	lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
+				       lapic_clockevent.shift);
+	lapic_clockevent.max_delta_ns =
+		clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
+	lapic_clockevent.min_delta_ns =
+		clockevent_delta2ns(0xF, &lapic_clockevent);
+
+	apic_calibration_res = calibration_result;
+
+	apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
+	apic_printk(APIC_VERBOSE, "..... mult: %u\n", lapic_clockevent.mult);
+	apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
+		    calibration_result);
+
+	apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
+		    "%u.%04u MHz.\n",
+		    calibration_result / (1000000 / HZ),
+		    calibration_result % (1000000 / HZ));
+
+	/*
+	 * Do a sanity check on the APIC calibration result
+	 */
+	if (calibration_result < (1000000 / HZ)) {
+		pr_warning("APIC frequency too slow, disabling apic timer\n");
+		return -1;
+	}
+
+	levt->features &= ~CLOCK_EVT_FEAT_DUMMY;
+	return 0;
+}
+
+int __init native_calibrate_apic(void)
 {
 	struct clock_event_device *levt = &__get_cpu_var(lapic_events);
 	void (*real_handler)(struct clock_event_device *dev);
 	unsigned long deltaj;
 	long delta, deltatsc;
 	int pm_referenced = 0;
+	unsigned int calibration_result;
 
 	local_irq_disable();
 
@@ -631,20 +659,12 @@ static int __init calibrate_APIC_clock(void)
 	pm_referenced = !calibrate_by_pmtimer(lapic_cal_pm2 - lapic_cal_pm1,
 					&delta, &deltatsc);
 
-	/* Calculate the scaled math multiplication factor */
-	lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
-				       lapic_clockevent.shift);
-	lapic_clockevent.max_delta_ns =
-		clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
-	lapic_clockevent.min_delta_ns =
-		clockevent_delta2ns(0xF, &lapic_clockevent);
-
 	calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
 
-	apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
-	apic_printk(APIC_VERBOSE, "..... mult: %u\n", lapic_clockevent.mult);
-	apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
-		    calibration_result);
+	if (initialize_lapic(delta, calibration_result)) {
+		local_irq_enable();
+		return -1;
+	}
 
 	if (cpu_has_tsc) {
 		apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
@@ -653,22 +673,6 @@ static int __init calibrate_APIC_clock(void)
 			    (deltatsc / LAPIC_CAL_LOOPS) % (1000000 / HZ));
 	}
 
-	apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
-		    "%u.%04u MHz.\n",
-		    calibration_result / (1000000 / HZ),
-		    calibration_result % (1000000 / HZ));
-
-	/*
-	 * Do a sanity check on the APIC calibration result
-	 */
-	if (calibration_result < (1000000 / HZ)) {
-		local_irq_enable();
-		pr_warning("APIC frequency too slow, disabling apic timer\n");
-		return -1;
-	}
-
-	levt->features &= ~CLOCK_EVT_FEAT_DUMMY;
-
 	/*
 	 * PM timer calibration failed or not turned on
 	 * so lets try APIC timer based calibration
@@ -706,7 +710,7 @@ static int __init calibrate_APIC_clock(void)
 
 	if (levt->features & CLOCK_EVT_FEAT_DUMMY) {
 		pr_warning("APIC timer disabled due to verification failure\n");
-			return -1;
+		return -1;
 	}
 
 	return 0;
@@ -738,7 +742,7 @@ void __init setup_boot_APIC_clock(void)
 	apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
 		    "calibrating APIC timer ...\n");
 
-	if (calibrate_APIC_clock()) {
+	if (x86_init.timers.calibrate_apic()) {
 		/* No broadcast on UP ! */
 		if (num_possible_cpus() > 1)
 			setup_APIC_timer();
arch/x86/kernel/cpu/vmware.c +27 -3
index 227b044..e8b760e 100644
@@ -26,6 +26,7 @@
 #include <asm/div64.h>
 #include <asm/x86_init.h>
 #include <asm/hypervisor.h>
+#include <asm/apic.h>
 
 #define CPUID_VMWARE_INFO_LEAF	0x40000000
 #define VMWARE_HYPERVISOR_MAGIC	0x564D5868
@@ -72,17 +73,40 @@ static unsigned long vmware_get_tsc_khz(void)
 	return tsc_hz;
 }
 
+static int __init vmware_calibrate_apic(void)
+{
+	unsigned int calibration_result;
+	uint32_t eax, ebx, ecx, edx;
+	int err;
+	long delta;
+	unsigned long flags;
+
+	VMWARE_PORT(GETHZ, eax, ebx, ecx, edx);
+	BUG_ON(!ecx);
+	calibration_result = ecx / HZ;
+
+	printk(KERN_INFO "APIC bus freq read from hypervisor\n");
+
+	delta  = (calibration_result * LAPIC_CAL_LOOPS) / APIC_DIVISOR;
+	local_irq_save(flags);
+	err = initialize_lapic(delta, calibration_result);
+	local_irq_restore(flags);
+
+	return err;
+}
+
 static void __init vmware_platform_setup(void)
 {
 	uint32_t eax, ebx, ecx, edx;
 
 	VMWARE_PORT(GETHZ, eax, ebx, ecx, edx);
 
-	if (ebx != UINT_MAX)
+	if (ebx != UINT_MAX) {
 		x86_platform.calibrate_tsc = vmware_get_tsc_khz;
-	else
+		x86_init.timers.calibrate_apic = vmware_calibrate_apic;
+	} else
 		printk(KERN_WARNING
-		       "Failed to get TSC freq from the hypervisor\n");
+		       "Failed to setup VMware hypervisor platform\n");
 }
 
 /*
arch/x86/kernel/x86_init.c +1 -0
index cd6da6b..f162e14 100644
@@ -68,6 +68,7 @@ struct x86_init_ops x86_init __initdata = {
 		.setup_percpu_clockev	= setup_boot_APIC_clock,
 		.tsc_pre_init		= x86_init_noop,
 		.timer_init		= hpet_time_init,
+		.calibrate_apic		= native_calibrate_apic
 	},
 
 	.iommu = {