Skip to contentSkip to conversation

clocksource: Add standalone MMIO ARM arch timer driver

PATCH In review Marc Zyngier 2 participants 7 messages Aug 14, 2025 – Oct 30, 2025

Subsystems (4)
lore
Share & tools
lore
author maintainer
#1 lore raw

PATCH v2 1/4 ACPI: GTDT: Generate platform devices for MMIO timers

In reply to Marc Zyngier

In preparation for the MMIO timer support code becoming an actual driver, mimic what is done for the SBSA watchdog and expose a synthetic device for each MMIO timer block.

---
 drivers/acpi/arm64/gtdt.c | 29 +++++++++++++++++++++++++----
 1 file changed, 25 insertions(+), 4 deletions(-)
drivers/acpi/arm64/gtdt.c +25 -4
index 70f8290b659de..fd995a1d3d248 100644
@@ -388,11 +388,11 @@ static int __init gtdt_import_sbsa_gwdt(struct acpi_gtdt_watchdog *wd,
 	return 0;
 }
 
-static int __init gtdt_sbsa_gwdt_init(void)
+static int __init gtdt_platform_timer_init(void)
 {
 	void *platform_timer;
 	struct acpi_table_header *table;
-	int ret, timer_count, gwdt_count = 0;
+	int ret, timer_count, gwdt_count = 0, mmio_timer_count = 0;
 
 	if (acpi_disabled)
 		return 0;
@@ -414,20 +414,41 @@ static int __init gtdt_sbsa_gwdt_init(void)
 		goto out_put_gtdt;
 
 	for_each_platform_timer(platform_timer) {
+		ret = 0;
+
 		if (is_non_secure_watchdog(platform_timer)) {
 			ret = gtdt_import_sbsa_gwdt(platform_timer, gwdt_count);
 			if (ret)
-				break;
+				continue;
 			gwdt_count++;
+		} else 	if (is_timer_block(platform_timer)) {
+			struct arch_timer_mem atm = {};
+			struct platform_device *pdev;
+
+			ret = gtdt_parse_timer_block(platform_timer, &atm);
+			if (ret)
+				continue;
+
+			pdev = platform_device_register_data(NULL, "gtdt-arm-mmio-timer",
+							     gwdt_count, &atm,
+							     sizeof(atm));
+			if (IS_ERR(pdev)) {
+				pr_err("Can't register timer %d\n", gwdt_count);
+				continue;
+			}
+
+			mmio_timer_count++;
 		}
 	}
 
 	if (gwdt_count)
 		pr_info("found %d SBSA generic Watchdog(s).\n", gwdt_count);
+	if (mmio_timer_count)
+		pr_info("found %d Generic MMIO timer(s).\n", mmio_timer_count);
 
 out_put_gtdt:
 	acpi_put_table(table);
 	return ret;
 }
 
-device_initcall(gtdt_sbsa_gwdt_init);
+device_initcall(gtdt_platform_timer_init);
Signature · 1 line
2.39.2
author maintainer
#2 lore raw

PATCH v2 2/4 clocksource/drivers/arm_arch_timer: Add standalone MMIO driver

In reply to Marc Zyngier

Add a new driver for the MMIO side of the ARM architected timer. Most of it has been lifted from the existing arch timer code, massaged, and finally rewritten.

It supports both DT and ACPI as firmware descriptions.

---
 MAINTAINERS                               |   1 +
 drivers/clocksource/arm_arch_timer_mmio.c | 421 ++++++++++++++++++++++
 2 files changed, 422 insertions(+)
 create mode 100644 drivers/clocksource/arm_arch_timer_mmio.c
MAINTAINERS +1 -0
index fe168477caa45..2243b726edd7e 100644
@@ -1990,6 +1990,7 @@ S:	Maintained
 F:	arch/arm/include/asm/arch_timer.h
 F:	arch/arm64/include/asm/arch_timer.h
 F:	drivers/clocksource/arm_arch_timer.c
+F:	drivers/clocksource/arm_arch_timer_mmio.c
 
 ARM GENERIC INTERRUPT CONTROLLER DRIVERS
 M:	Marc Zyngier <[email protected]>
drivers/clocksource/arm_arch_timer_mmio.c new file +421 -0

421 lines not on this page · raw message

Signature · 1 line
2.39.2
author maintainer
#6 lore raw

PATCH v2 4/4 clocksource/drivers/arm_arch_timer_mmio: Add MMIO clocksource

In reply to Marc Zyngier

The MMIO driver can also double as a clocksource, something that was missing in its previous incarnation. Add it for completeness.

---
 drivers/clocksource/arm_arch_timer_mmio.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
drivers/clocksource/arm_arch_timer_mmio.c +19 -0
index 3522d1d8cb97b..ebe1987d651eb 100644
@@ -45,6 +45,7 @@ enum arch_timer_access {
 
 struct arch_timer {
 	struct clock_event_device	evt;
+	struct clocksource		cs;
 	struct arch_timer_mem		*gt_block;
 	void __iomem			*base;
 	enum arch_timer_access		access;
@@ -52,6 +53,7 @@ struct arch_timer {
 };
 
 #define evt_to_arch_timer(e) container_of(e, struct arch_timer, evt)
+#define cs_to_arch_timer(c) container_of(c, struct arch_timer, cs)
 
 static void arch_timer_mmio_write(struct arch_timer *timer,
 				  enum arch_timer_reg reg, u64 val)
@@ -128,6 +130,13 @@ static noinstr u64 arch_counter_mmio_get_cnt(struct arch_timer *t)
 	return ((u64) cnt_hi << 32) | cnt_lo;
 }
 
+static u64 arch_mmio_counter_read(struct clocksource *cs)
+{
+	struct arch_timer *at = cs_to_arch_timer(cs);
+
+	return arch_counter_mmio_get_cnt(at);
+}
+
 static int arch_timer_mmio_shutdown(struct clock_event_device *clk)
 {
 	struct arch_timer *at = evt_to_arch_timer(clk);
@@ -256,6 +265,16 @@ static void arch_timer_mmio_setup(struct arch_timer *at, int irq)
 					(unsigned long)CLOCKSOURCE_MASK(56));
 
 	enable_irq(at->evt.irq);
+
+	at->cs = (struct clocksource) {
+		.name	= "arch_mmio_counter",
+		.rating	= 300,
+		.read	= arch_mmio_counter_read,
+		.mask	= CLOCKSOURCE_MASK(56),
+		.flags	= CLOCK_SOURCE_IS_CONTINUOUS,
+	};
+
+	clocksource_register_hz(&at->cs, at->rate);
 }
 
 static int arch_timer_mmio_frame_register(struct platform_device *pdev,
Signature · 1 line
2.39.2

Marc Zyngier

author maintainer
#0 lore raw

v2 cover clocksource: Add standalone MMIO ARM arch timer driver

For the past 10 years, both Mark and I have been lamenting about the sorry state of the badly named "arch_timer" driver, and about the way the MMIO part is intricately weaved into the system-register part.

The time has finally come to take a stab at it.

This small series simply creates a new timer driver for the MMIO arch timer, and only that. It is an actual driver, and not some kludge that has to run super early (that's what the per-CPU timers are for). This allows, in turn, a pretty large cleanup of the per-CPU driver, though there is more to come -- one thing at a time.

As an added bonus, we get a clocksource, which the original code didn't provide. Just in case it might be useful. The end-result is far more readable, and about 100 lines smaller.

Patches on top of 6.17-rc1.

* From v1 [1]:

- Narrow the max delta to something that fits an unsigned long, as the core code doesn't deal with 64bit quantities on 32bit CPUs.

- Collected RBs and TBs from Sudeep, with thanks.

Marc Zyngier (4):
ACPI: GTDT: Generate platform devices for MMIO timers clocksource/drivers/arm_arch_timer: Add standalone MMIO driver clocksource/drivers/arm_arch_timer_mmio: Switch over to standalone
driver
clocksource/drivers/arm_arch_timer_mmio: Add MMIO clocksource

 MAINTAINERS                               |   1 +
 drivers/acpi/arm64/gtdt.c                 |  29 +-
 drivers/clocksource/Makefile              |   1 +
 drivers/clocksource/arm_arch_timer.c      | 686 ++--------------------
 drivers/clocksource/arm_arch_timer_mmio.c | 440 ++++++++++++++
 include/clocksource/arm_arch_timer.h      |   5 -
 6 files changed, 532 insertions(+), 630 deletions(-)
 create mode 100644 drivers/clocksource/arm_arch_timer_mmio.c
Signature · 1 line
2.39.2
author maintainer
#3 lore raw

PATCH v2 3/4 clocksource/drivers/arm_arch_timer_mmio: Switch over to standalone driver

Remove all the MMIO support from the per-CPU timer driver, and switch over to the standalove driver.

---
 drivers/clocksource/Makefile         |   1 +
 drivers/clocksource/arm_arch_timer.c | 686 +++------------------------
 include/clocksource/arm_arch_timer.h |   5 -
 3 files changed, 66 insertions(+), 626 deletions(-)
drivers/clocksource/Makefile +1 -0
index 205bf3b0a8f3f..0dcd958e21443 100644
@@ -64,6 +64,7 @@ obj-$(CONFIG_REALTEK_OTTO_TIMER)	+= timer-rtl-otto.o
 
 obj-$(CONFIG_ARC_TIMERS)		+= arc_timer.o
 obj-$(CONFIG_ARM_ARCH_TIMER)		+= arm_arch_timer.o
+obj-$(CONFIG_ARM_ARCH_TIMER)		+= arm_arch_timer_mmio.o
 obj-$(CONFIG_ARM_GLOBAL_TIMER)		+= arm_global_timer.o
 obj-$(CONFIG_ARMV7M_SYSTICK)		+= armv7m_systick.o
 obj-$(CONFIG_ARM_TIMER_SP804)		+= timer-sp804.o
drivers/clocksource/arm_arch_timer.c +65 -621

860 lines not on this page · raw message

include/clocksource/arm_arch_timer.h +0 -5
index ce6521ad04d12..2eda895f19f54 100644
@@ -9,9 +9,6 @@
 #include <linux/timecounter.h>
 #include <linux/types.h>
 
-#define ARCH_TIMER_TYPE_CP15		BIT(0)
-#define ARCH_TIMER_TYPE_MEM		BIT(1)
-
 #define ARCH_TIMER_CTRL_ENABLE		(1 << 0)
 #define ARCH_TIMER_CTRL_IT_MASK		(1 << 1)
 #define ARCH_TIMER_CTRL_IT_STAT		(1 << 2)
@@ -51,8 +48,6 @@ enum arch_timer_spi_nr {
 
 #define ARCH_TIMER_PHYS_ACCESS		0
 #define ARCH_TIMER_VIRT_ACCESS		1
-#define ARCH_TIMER_MEM_PHYS_ACCESS	2
-#define ARCH_TIMER_MEM_VIRT_ACCESS	3
 
 #define ARCH_TIMER_MEM_MAX_FRAMES	8
 
Signature · 1 line
2.39.2
#4 lore raw

Re: [PATCH v2 3/4] clocksource/drivers/arm_arch_timer_mmio: Switch over to standalone driver

On Thu, Aug 14, 2025 at 04:46:21PM +0100, Marc Zyngier wrote:

[...]

Quoted 1 line
drivers/clocksource/arm_arch_timer.c +0 -0

[...]

Quoted 30 lines

#ifdef CONFIG_ACPI_GTDT

drivers/clocksource/arm_arch_timer.c +0 -29
-static int __init
-arch_timer_mem_verify_cntfrq(struct arch_timer_mem *timer_mem)
-{
-	struct arch_timer_mem_frame *frame;
-	u32 rate;
-	int i;
-
-	for (i = 0; i < ARCH_TIMER_MEM_MAX_FRAMES; i++) {
-		frame = &timer_mem->frame[i];
-
-		if (!frame->valid)
-			continue;
-
-		rate = arch_timer_mem_frame_get_cntfrq(frame);
-		if (rate == arch_timer_rate)
-			continue;
-
-		pr_err(FW_BUG "CNTFRQ mismatch: frame @ %pa: (0x%08lx), CPU: (0x%08lx)\n",
-			&frame->cntbase,
-			(unsigned long)rate, (unsigned long)arch_timer_rate);
-
-		return -EINVAL;
-	}
-
-	return 0;
-}
-
-static int __init arch_timer_mem_acpi_init(int platform_timer_count)
-{

drivers/clocksource/arm_arch_timer.c static int __init arch_timer_of_init(struct device_node *np) · line 1702

Quoted 10 lines
drivers/clocksource/arm_arch_timer.c +0 -10
-	struct arch_timer_mem *timers, *timer;
-	struct arch_timer_mem_frame *frame, *best_frame = NULL;
-	int timer_count, i, ret = 0;
-
-	timers = kcalloc(platform_timer_count, sizeof(*timers),
-			    GFP_KERNEL);
-	if (!timers)
-		return -ENOMEM;
-
-	ret = acpi_arch_timer_mem_init(timers, &timer_count);

You probably already noticed (I was checking the initcall level in drivers/acpi/arm64/gtdt.c to start sorting out GICv5 IWB dependencies), AFAICS acpi_arch_timer_mem_init() is now unused so it can be removed.

Thanks,
Lorenzo

author maintainer
#5 lore raw

Re: [PATCH v2 3/4] clocksource/drivers/arm_arch_timer_mmio: Switch over to standalone driver

Quoted 45 lines · Lorenzo Pieralisi

On Thu, Aug 14, 2025 at 04:46:21PM +0100, Marc Zyngier wrote:

[...]

Quoted 1 line
drivers/clocksource/arm_arch_timer.c +0 -0

[...]

Quoted 36 lines

#ifdef CONFIG_ACPI_GTDT

drivers/clocksource/arm_arch_timer.c +0 -35
-static int __init
-arch_timer_mem_verify_cntfrq(struct arch_timer_mem *timer_mem)
-{
-	struct arch_timer_mem_frame *frame;
-	u32 rate;
-	int i;
-
-	for (i = 0; i < ARCH_TIMER_MEM_MAX_FRAMES; i++) {
-		frame = &timer_mem->frame[i];
-
-		if (!frame->valid)
-			continue;
-
-		rate = arch_timer_mem_frame_get_cntfrq(frame);
-		if (rate == arch_timer_rate)
-			continue;
-
-		pr_err(FW_BUG "CNTFRQ mismatch: frame @ %pa: (0x%08lx), CPU: (0x%08lx)\n",
-			&frame->cntbase,
-			(unsigned long)rate, (unsigned long)arch_timer_rate);
-
-		return -EINVAL;
-	}
-
-	return 0;
-}
-
-static int __init arch_timer_mem_acpi_init(int platform_timer_count)
-{
-	struct arch_timer_mem *timers, *timer;
-	struct arch_timer_mem_frame *frame, *best_frame = NULL;
-	int timer_count, i, ret = 0;
-
-	timers = kcalloc(platform_timer_count, sizeof(*timers),
-			    GFP_KERNEL);
Quoted 8 lines

drivers/clocksource/arm_arch_timer.c static int __init arch_timer_of_init(struct device_node *np) · line 1702

Quoted 4 lines
drivers/clocksource/arm_arch_timer.c +0 -4
-	if (!timers)
-		return -ENOMEM;
-
-	ret = acpi_arch_timer_mem_init(timers, &timer_count);

You probably already noticed (I was checking the initcall level in drivers/acpi/arm64/gtdt.c to start sorting out GICv5 IWB dependencies), AFAICS acpi_arch_timer_mem_init() is now unused so it can be removed.

See https://lore.kernel.org/r/[email protected]

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.