sst-linux/drivers/clocksource
David Woodhouse 65ae99b51e clockevents/drivers/i8253: Fix stop sequence for timer 0
commit 531b2ca0a940ac9db03f246c8b77c4201de72b00 upstream.

According to the data sheet, writing the MODE register should stop the
counter (and thus the interrupts). This appears to work on real hardware,
at least modern Intel and AMD systems. It should also work on Hyper-V.

However, on some buggy virtual machines the mode change doesn't have any
effect until the counter is subsequently loaded (or perhaps when the IRQ
next fires).

So, set MODE 0 and then load the counter, to ensure that those buggy VMs
do the right thing and the interrupts stop. And then write MODE 0 *again*
to stop the counter on compliant implementations too.

Apparently, Hyper-V keeps firing the IRQ *repeatedly* even in mode zero
when it should only happen once, but the second MODE write stops that too.

Userspace test program (mostly written by tglx):
=====
 #include <stdio.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <stdint.h>
 #include <sys/io.h>

static __always_inline void __out##bwl(type value, uint16_t port)	\
{									\
	asm volatile("out" #bwl " %" #bw "0, %w1"			\
		     : : "a"(value), "Nd"(port));			\
}									\
									\
static __always_inline type __in##bwl(uint16_t port)			\
{									\
	type value;							\
	asm volatile("in" #bwl " %w1, %" #bw "0"			\
		     : "=a"(value) : "Nd"(port));			\
	return value;							\
}

BUILDIO(b, b, uint8_t)

 #define inb __inb
 #define outb __outb

 #define PIT_MODE	0x43
 #define PIT_CH0	0x40
 #define PIT_CH2	0x42

static int is8254;

static void dump_pit(void)
{
	if (is8254) {
		// Latch and output counter and status
		outb(0xC2, PIT_MODE);
		printf("%02x %02x %02x\n", inb(PIT_CH0), inb(PIT_CH0), inb(PIT_CH0));
	} else {
		// Latch and output counter
		outb(0x0, PIT_MODE);
		printf("%02x %02x\n", inb(PIT_CH0), inb(PIT_CH0));
	}
}

int main(int argc, char* argv[])
{
	int nr_counts = 2;

	if (argc > 1)
		nr_counts = atoi(argv[1]);

	if (argc > 2)
		is8254 = 1;

	if (ioperm(0x40, 4, 1) != 0)
		return 1;

	dump_pit();

	printf("Set oneshot\n");
	outb(0x38, PIT_MODE);
	outb(0x00, PIT_CH0);
	outb(0x0F, PIT_CH0);

	dump_pit();
	usleep(1000);
	dump_pit();

	printf("Set periodic\n");
	outb(0x34, PIT_MODE);
	outb(0x00, PIT_CH0);
	outb(0x0F, PIT_CH0);

	dump_pit();
	usleep(1000);
	dump_pit();
	dump_pit();
	usleep(100000);
	dump_pit();
	usleep(100000);
	dump_pit();

	printf("Set stop (%d counter writes)\n", nr_counts);
	outb(0x30, PIT_MODE);
	while (nr_counts--)
		outb(0xFF, PIT_CH0);

	dump_pit();
	usleep(100000);
	dump_pit();
	usleep(100000);
	dump_pit();

	printf("Set MODE 0\n");
	outb(0x30, PIT_MODE);

	dump_pit();
	usleep(100000);
	dump_pit();
	usleep(100000);
	dump_pit();

	return 0;
}
=====

Suggested-by: Sean Christopherson <seanjc@google.com>
Co-developed-by: Li RongQing <lirongqing@baidu.com>
Signed-off-by: Li RongQing <lirongqing@baidu.com>
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Tested-by: Michael Kelley <mhkelley@outlook.com>
Link: https://lore.kernel.org/all/20240802135555.564941-2-dwmw2@infradead.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2025-03-28 21:58:48 +01:00
..
acpi_pm.c
arc_timer.c
arm_arch_timer.c clocksource/drivers/arm_arch_timer: limit XGene-1 workaround 2023-11-20 11:52:01 +01:00
arm_global_timer.c clocksource/drivers/arm_global_timer: Guard against division by zero 2024-08-29 17:30:36 +02:00
armv7m_systick.c
asm9260_timer.c
bcm2835_timer.c
bcm_kona_timer.c
clksrc_st_lpc.c
clksrc-dbx500-prcmu.c
clps711x-timer.c
dummy_timer.c
dw_apb_timer_of.c
dw_apb_timer.c
em_sti.c
exynos_mct.c
hyperv_timer.c x86/hyperv: Fix hv tsc page based sched_clock for hibernation 2025-01-09 13:29:56 +01:00
i8253.c clockevents/drivers/i8253: Fix stop sequence for timer 0 2025-03-28 21:58:48 +01:00
ingenic-ost.c
ingenic-sysost.c
ingenic-timer.c
jcore-pit.c
Kconfig clocksource/drivers:sp804: Make user selectable 2024-12-14 19:53:19 +01:00
Makefile
mips-gic-timer.c
mmio.c
mps2-timer.c
mxs_timer.c
nomadik-mtu.c
numachip.c
renesas-ostm.c
samsung_pwm_timer.c
scx200_hrt.c
sh_cmt.c clocksource/drivers/sh_cmt: Address race condition for clock events 2024-08-14 13:52:45 +02:00
sh_mtu2.c
sh_tmu.c
timer-armada-370-xp.c
timer-atmel-pit.c
timer-atmel-st.c
timer-atmel-tcb.c clocksource/drivers/timer-atmel-tcb: Fix initialization on SAM9 hardware 2023-11-28 17:06:55 +00:00
timer-cadence-ttc.c
timer-clint.c
timer-cs5535.c
timer-davinci.c
timer-digicolor.c
timer-fsl-ftm.c
timer-fttmr010.c
timer-goldfish.c
timer-gx6605s.c
timer-gxp.c
timer-imx-gpt.c clocksource/drivers/timer-imx-gpt: Fix potential memory leak 2023-11-28 17:06:54 +00:00
timer-imx-sysctr.c
timer-imx-tpm.c clocksource/drivers/imx-tpm: Fix next event not taking effect sometime 2024-09-12 11:10:27 +02:00
timer-integrator-ap.c
timer-ixp4xx.c
timer-keystone.c
timer-lpc32xx.c
timer-mediatek.c
timer-meson6.c
timer-microchip-pit64b.c
timer-milbeaut.c
timer-mp-csky.c
timer-msc313e.c
timer-npcm7xx.c
timer-of.c clocksource/drivers/timer-of: Remove percpu irq related code 2024-09-12 11:10:27 +02:00
timer-of.h clocksource/drivers/timer-of: Remove percpu irq related code 2024-09-12 11:10:27 +02:00
timer-orion.c
timer-owl.c
timer-oxnas-rps.c
timer-pistachio.c
timer-probe.c
timer-pxa.c
timer-qcom.c clocksource/drivers/qcom: Add missing iounmap() on errors in msm_dt_timer_init() 2024-10-17 15:20:47 +02:00
timer-rda.c
timer-riscv.c
timer-rockchip.c
timer-sp804.c
timer-sp.h
timer-sprd.c
timer-stm32-lp.c
timer-stm32.c
timer-sun4i.c
timer-sun5i.c
timer-tegra186.c
timer-tegra.c
timer-ti-32k.c
timer-ti-dm-systimer.c clocksource/drivers/timer-ti-dm: Fix child node refcount handling 2024-12-14 19:53:19 +01:00
timer-ti-dm.c clocksource/drivers/timer-ti-dm: Fix make W=n kerneldoc warnings 2024-01-25 15:27:39 -08:00
timer-versatile.c
timer-vf-pit.c
timer-vt8500.c
timer-zevio.c