Skip to contentSkip to conversation

i8253: Fix PIT shutdown quirk on Hyper-V

PATCH New Michael Kelley 2 participants 7 messages Nov 4, 2018

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

v2 cover i8253: Fix PIT shutdown quirk on Hyper-V

pit_shutdown() doesn't work on Hyper-V because of a quirk in the PIT emulation. This problem exists in all versions of Hyper-V and had not been noticed previously. When the counter register is set to zero, the emulated PIT continues to interrupt @18.2 HZ.

Account for this quirk by adding a global variable in the i8253 code that controls whether the counter register is zero'ed. Then in Hyper-V initialization code, override the default setting so the counter register is not zero'ed.

Changes in v2:
* Instead of a function call to check if running on Hyper-V, use a global variable to control whether the counter register is zero'ed. [Juergen Gross & Thomas Gleixner]

Michael Kelley (2):
i8253: Add support for PIT shutdown quirk
x86/hyper-v: Enable PIT shutdown quirk

 arch/x86/kernel/cpu/mshyperv.c | 11 +++++++++++
 drivers/clocksource/i8253.c    | 14 ++++++++++++--
 include/linux/i8253.h          |  1 +
 3 files changed, 24 insertions(+), 2 deletions(-)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH v2 1/2 i8253: Add support for PIT shutdown quirk

Add support for platforms where pit_shutdown() doesn't work because of a quirk in the PIT emulation. On these platforms setting the counter register to zero causes the PIT to start running again, negating the shutdown. Provide a global variable that controls whether the counter register is zero'ed, which platform specific code can override.

---
 drivers/clocksource/i8253.c | 14 ++++++++++++--
 include/linux/i8253.h       |  1 +
 2 files changed, 13 insertions(+), 2 deletions(-)
drivers/clocksource/i8253.c +12 -2
index 9c38895..2a4202a 100644
@@ -20,6 +20,13 @@
 DEFINE_RAW_SPINLOCK(i8253_lock);
 EXPORT_SYMBOL(i8253_lock);
 
+/*
+ * Handle PIT quirk in pit_shutdown() where zeroing the counter register
+ * restarts the PIT, negating the shutdown. On platforms with the quirk,
+ * platform specific code can set this to false.
+ */
+bool i8253_clear_counter __ro_after_init = true;
+
 #ifdef CONFIG_CLKSRC_I8253
 /*
  * Since the PIT overflows every tick, its not very useful
@@ -109,8 +116,11 @@ static int pit_shutdown(struct clock_event_device *evt)
 	raw_spin_lock(&i8253_lock);
 
 	outb_p(0x30, PIT_MODE);
-	outb_p(0, PIT_CH0);
-	outb_p(0, PIT_CH0);
+
+	if (i8253_clear_counter) {
+		outb_p(0, PIT_CH0);
+		outb_p(0, PIT_CH0);
+	}
 
 	raw_spin_unlock(&i8253_lock);
 	return 0;
include/linux/i8253.h +1 -0
index e6bb36a..31c4be5 100644
@@ -21,6 +21,7 @@
 #define PIT_LATCH	((PIT_TICK_RATE + HZ/2) / HZ)
 
 extern raw_spinlock_t i8253_lock;
+extern bool i8253_clear_counter;
 extern struct clock_event_device i8253_clockevent;
 extern void clockevent_i8253_init(bool oneshot);
 
Signature · 1 line
1.8.3.1
#2 lore raw

[tip:x86/urgent] clockevents/drivers/i8253: Add support for PIT shutdown quirk

clockevents/drivers/i8253: Add support for PIT shutdown quirk
Commit-ID:  35b69a420bfb56b7b74cb635ea903db05e357bec
Gitweb:     https://git.kernel.org/tip/35b69a420bfb56b7b74cb635ea903db05e357bec
Author:     Michael Kelley <[email protected]>
AuthorDate: Sun, 4 Nov 2018 03:48:54 +0000
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Sun, 4 Nov 2018 11:04:46 +0100

clockevents/drivers/i8253: Add support for PIT shutdown quirk

Add support for platforms where pit_shutdown() doesn't work because of a quirk in the PIT emulation. On these platforms setting the counter register to zero causes the PIT to start running again, negating the shutdown.

Provide a global variable that controls whether the counter register is zero'ed, which platform specific code can override.

---
 drivers/clocksource/i8253.c | 14 ++++++++++++--
 include/linux/i8253.h       |  1 +
 2 files changed, 13 insertions(+), 2 deletions(-)
drivers/clocksource/i8253.c +12 -2
index 9c38895542f4..d4350bb10b83 100644
@@ -20,6 +20,13 @@
 DEFINE_RAW_SPINLOCK(i8253_lock);
 EXPORT_SYMBOL(i8253_lock);
 
+/*
+ * Handle PIT quirk in pit_shutdown() where zeroing the counter register
+ * restarts the PIT, negating the shutdown. On platforms with the quirk,
+ * platform specific code can set this to false.
+ */
+bool i8253_clear_counter_on_shutdown __ro_after_init = true;
+
 #ifdef CONFIG_CLKSRC_I8253
 /*
  * Since the PIT overflows every tick, its not very useful
@@ -109,8 +116,11 @@ static int pit_shutdown(struct clock_event_device *evt)
 	raw_spin_lock(&i8253_lock);
 
 	outb_p(0x30, PIT_MODE);
-	outb_p(0, PIT_CH0);
-	outb_p(0, PIT_CH0);
+
+	if (i8253_clear_counter_on_shutdown) {
+		outb_p(0, PIT_CH0);
+		outb_p(0, PIT_CH0);
+	}
 
 	raw_spin_unlock(&i8253_lock);
 	return 0;
include/linux/i8253.h +1 -0
index e6bb36a97519..8336b2f6f834 100644
@@ -21,6 +21,7 @@
 #define PIT_LATCH	((PIT_TICK_RATE + HZ/2) / HZ)
 
 extern raw_spinlock_t i8253_lock;
+extern bool i8253_clear_counter_on_shutdown;
 extern struct clock_event_device i8253_clockevent;
 extern void clockevent_i8253_init(bool oneshot);
#3 lore raw

[tip:x86/urgent] clockevents/drivers/i8253: Add support for PIT shutdown quirk

In reply to Michael Kelley
clockevents/drivers/i8253: Add support for PIT shutdown quirk
Commit-ID:  35b69a420bfb56b7b74cb635ea903db05e357bec
Gitweb:     https://git.kernel.org/tip/35b69a420bfb56b7b74cb635ea903db05e357bec
Author:     Michael Kelley <[email protected]>
AuthorDate: Sun, 4 Nov 2018 03:48:54 +0000
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Sun, 4 Nov 2018 11:04:46 +0100

clockevents/drivers/i8253: Add support for PIT shutdown quirk

Add support for platforms where pit_shutdown() doesn't work because of a quirk in the PIT emulation. On these platforms setting the counter register to zero causes the PIT to start running again, negating the shutdown.

Provide a global variable that controls whether the counter register is zero'ed, which platform specific code can override.

---
 drivers/clocksource/i8253.c | 14 ++++++++++++--
 include/linux/i8253.h       |  1 +
 2 files changed, 13 insertions(+), 2 deletions(-)
drivers/clocksource/i8253.c +12 -2
index 9c38895542f4..d4350bb10b83 100644
@@ -20,6 +20,13 @@
 DEFINE_RAW_SPINLOCK(i8253_lock);
 EXPORT_SYMBOL(i8253_lock);
 
+/*
+ * Handle PIT quirk in pit_shutdown() where zeroing the counter register
+ * restarts the PIT, negating the shutdown. On platforms with the quirk,
+ * platform specific code can set this to false.
+ */
+bool i8253_clear_counter_on_shutdown __ro_after_init = true;
+
 #ifdef CONFIG_CLKSRC_I8253
 /*
  * Since the PIT overflows every tick, its not very useful
@@ -109,8 +116,11 @@ static int pit_shutdown(struct clock_event_device *evt)
 	raw_spin_lock(&i8253_lock);
 
 	outb_p(0x30, PIT_MODE);
-	outb_p(0, PIT_CH0);
-	outb_p(0, PIT_CH0);
+
+	if (i8253_clear_counter_on_shutdown) {
+		outb_p(0, PIT_CH0);
+		outb_p(0, PIT_CH0);
+	}
 
 	raw_spin_unlock(&i8253_lock);
 	return 0;
include/linux/i8253.h +1 -0
index e6bb36a97519..8336b2f6f834 100644
@@ -21,6 +21,7 @@
 #define PIT_LATCH	((PIT_TICK_RATE + HZ/2) / HZ)
 
 extern raw_spinlock_t i8253_lock;
+extern bool i8253_clear_counter_on_shutdown;
 extern struct clock_event_device i8253_clockevent;
 extern void clockevent_i8253_init(bool oneshot);
 
#4 lore raw

PATCH v2 2/2 x86/hyper-v: Enable PIT shutdown quirk

In reply to Michael Kelley

Hyper-V emulation of the PIT has a quirk such that the normal PIT shutdown path doesn't work. Enable the PIT code that handles this quirk.

---
 arch/x86/kernel/cpu/mshyperv.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
arch/x86/kernel/cpu/mshyperv.c +11 -0
index 1c72f38..65b2f88 100644
@@ -20,6 +20,7 @@
 #include <linux/interrupt.h>
 #include <linux/irq.h>
 #include <linux/kexec.h>
+#include <linux/i8253.h>
 #include <asm/processor.h>
 #include <asm/hypervisor.h>
 #include <asm/hyperv-tlfs.h>
@@ -295,6 +296,16 @@ static void __init ms_hyperv_init_platform(void)
 	if (efi_enabled(EFI_BOOT))
 		x86_platform.get_nmi_reason = hv_get_nmi_reason;
 
+	/*
+	 * Hyper-V VMs have a PIT emulation quirk such that zeroing the
+	 * counter register during PIT shutdown restarts the PIT. So it
+	 * continues to interrupt @18.2 HZ. Setting i8253_clear_counter
+	 * to false tells pit_shutdown() not to zero the counter so that
+	 * the PIT really is shutdown. Generation 2 VMs don't have a PIT,
+	 * and setting this value has no effect.
+	 */
+	i8253_clear_counter = false;
+
 #if IS_ENABLED(CONFIG_HYPERV)
 	/*
 	 * Setup the hook to get control post apic initialization.
Signature · 1 line
1.8.3.1
#5 lore raw

[tip:x86/urgent] x86/hyper-v: Enable PIT shutdown quirk

x86/hyper-v: Enable PIT shutdown quirk
Commit-ID:  1de72c706488b7be664a601cf3843bd01e327e58
Gitweb:     https://git.kernel.org/tip/1de72c706488b7be664a601cf3843bd01e327e58
Author:     Michael Kelley <[email protected]>
AuthorDate: Sun, 4 Nov 2018 03:48:57 +0000
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Sun, 4 Nov 2018 11:04:46 +0100

x86/hyper-v: Enable PIT shutdown quirk

Hyper-V emulation of the PIT has a quirk such that the normal PIT shutdown path doesn't work, because clearing the counter register restarts the timer.

Disable the counter clearing on PIT shutdown.

---
 arch/x86/kernel/cpu/mshyperv.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
arch/x86/kernel/cpu/mshyperv.c +11 -0
index 1c72f3819eb1..e81a2db42df7 100644
@@ -20,6 +20,7 @@
 #include <linux/interrupt.h>
 #include <linux/irq.h>
 #include <linux/kexec.h>
+#include <linux/i8253.h>
 #include <asm/processor.h>
 #include <asm/hypervisor.h>
 #include <asm/hyperv-tlfs.h>
@@ -295,6 +296,16 @@ static void __init ms_hyperv_init_platform(void)
 	if (efi_enabled(EFI_BOOT))
 		x86_platform.get_nmi_reason = hv_get_nmi_reason;
 
+	/*
+	 * Hyper-V VMs have a PIT emulation quirk such that zeroing the
+	 * counter register during PIT shutdown restarts the PIT. So it
+	 * continues to interrupt @18.2 HZ. Setting i8253_clear_counter
+	 * to false tells pit_shutdown() not to zero the counter so that
+	 * the PIT really is shutdown. Generation 2 VMs don't have a PIT,
+	 * and setting this value has no effect.
+	 */
+	i8253_clear_counter_on_shutdown = false;
+
 #if IS_ENABLED(CONFIG_HYPERV)
 	/*
 	 * Setup the hook to get control post apic initialization.
#6 lore raw

[tip:x86/urgent] x86/hyper-v: Enable PIT shutdown quirk

In reply to Michael Kelley
x86/hyper-v: Enable PIT shutdown quirk
Commit-ID:  1de72c706488b7be664a601cf3843bd01e327e58
Gitweb:     https://git.kernel.org/tip/1de72c706488b7be664a601cf3843bd01e327e58
Author:     Michael Kelley <[email protected]>
AuthorDate: Sun, 4 Nov 2018 03:48:57 +0000
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Sun, 4 Nov 2018 11:04:46 +0100

x86/hyper-v: Enable PIT shutdown quirk

Hyper-V emulation of the PIT has a quirk such that the normal PIT shutdown path doesn't work, because clearing the counter register restarts the timer.

Disable the counter clearing on PIT shutdown.

---
 arch/x86/kernel/cpu/mshyperv.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
arch/x86/kernel/cpu/mshyperv.c +11 -0
index 1c72f3819eb1..e81a2db42df7 100644
@@ -20,6 +20,7 @@
 #include <linux/interrupt.h>
 #include <linux/irq.h>
 #include <linux/kexec.h>
+#include <linux/i8253.h>
 #include <asm/processor.h>
 #include <asm/hypervisor.h>
 #include <asm/hyperv-tlfs.h>
@@ -295,6 +296,16 @@ static void __init ms_hyperv_init_platform(void)
 	if (efi_enabled(EFI_BOOT))
 		x86_platform.get_nmi_reason = hv_get_nmi_reason;
 
+	/*
+	 * Hyper-V VMs have a PIT emulation quirk such that zeroing the
+	 * counter register during PIT shutdown restarts the PIT. So it
+	 * continues to interrupt @18.2 HZ. Setting i8253_clear_counter
+	 * to false tells pit_shutdown() not to zero the counter so that
+	 * the PIT really is shutdown. Generation 2 VMs don't have a PIT,
+	 * and setting this value has no effect.
+	 */
+	i8253_clear_counter_on_shutdown = false;
+
 #if IS_ENABLED(CONFIG_HYPERV)
 	/*
 	 * Setup the hook to get control post apic initialization.