Skip to contentSkip to conversation

clocksource: Add standalone MMIO ARM arch timer driver

PATCH Superseded Marc Zyngier 6 participants 18 messages Aug 7, 2025 – Oct 30, 2025

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Marc Zyngier

author maintainer
#0 lore raw

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 have 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.16.

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 | 439 ++++++++++++++
 include/clocksource/arm_arch_timer.h      |   5 -
 6 files changed, 531 insertions(+), 630 deletions(-)
 create mode 100644 drivers/clocksource/arm_arch_timer_mmio.c
Signature · 1 line
2.39.2
author maintainer
#1 lore raw

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

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
#2 lore raw

Re: [PATCH 1/4] ACPI: GTDT: Generate platform devices for MMIO timers

Quoted 40 lines · 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 +6 -3
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)) {

drivers/acpi/arm64/gtdt.c static int __init gtdt_sbsa_gwdt_init(void) · line 434

Quoted 10 lines
drivers/acpi/arm64/gtdt.c +10 -0
+			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));

Did you mean to pass mmio_timer_count as the id argument to platform_device_register_data()?

drivers/acpi/arm64/gtdt.c static int __init gtdt_sbsa_gwdt_init(void) · line 438

Quoted 4 lines
drivers/acpi/arm64/gtdt.c +4 -0
+			if (IS_ERR(pdev)) {
+				pr_err("Can't register timer %d\n", gwdt_count);
+				continue;
+			}

and here too.

Thanks,
Pavan

author maintainer
#3 lore raw

Re: [PATCH 1/4] ACPI: GTDT: Generate platform devices for MMIO timers

Quoted 47 lines · Pavan Kondeti
Quoted 45 lines · 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 +11 -3
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)
Quoted 8 lines

drivers/acpi/arm64/gtdt.c static int __init gtdt_sbsa_gwdt_init(void) · line 434

Quoted 5 lines
drivers/acpi/arm64/gtdt.c +5 -0
+				continue;
+
+			pdev = platform_device_register_data(NULL, "gtdt-arm-mmio-timer",
+							     gwdt_count, &atm,
+							     sizeof(atm));

Did you mean to pass mmio_timer_count as the id argument to platform_device_register_data()?

I did. Clearly a brain fart. And looking at this again, there are additional cleanups that can be applied. I'll spin something shortly.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author maintainer
#4 lore raw

PATCH 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 | 420 ++++++++++++++++++++++
 2 files changed, 421 insertions(+)
 create mode 100644 drivers/clocksource/arm_arch_timer_mmio.c
MAINTAINERS +1 -0
index c0b444e5fd5ad..be521c150d3fb 100644
@@ -1952,6 +1952,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 +420 -0

420 lines not on this page · raw message

Signature · 1 line
2.39.2
#5 lore raw

Re: [PATCH 2/4] clocksource/drivers/arm_arch_timer: Add standalone MMIO driver

Quoted 13 lines · 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 | 420 ++++++++++++++++++++++
 2 files changed, 421 insertions(+)
 create mode 100644 drivers/clocksource/arm_arch_timer_mmio.c

[...]

Quoted 7 lines
drivers/clocksource/arm_arch_timer_mmio.c new file +7 -0
+static void arch_timer_mmio_setup(struct arch_timer *at, int irq)
+{
+	at->evt = (struct clock_event_device) {
+		.features		   = (CLOCK_EVT_FEAT_ONESHOT |
+					      CLOCK_EVT_FEAT_DYNIRQ),
+		.name			   = "arch_mem_timer",
+		.rating			   = 400,

drivers/clocksource/arm_arch_timer_mmio.c line 255

Quoted 10 lines
drivers/clocksource/arm_arch_timer_mmio.c new file +10 -0
+		.cpumask		   = cpu_possible_mask,
+		.irq 			   = irq,
+		.set_next_event		   = arch_timer_mmio_set_next_event,
+		.set_state_oneshot_stopped = arch_timer_mmio_shutdown,
+		.set_state_shutdown	   = arch_timer_mmio_shutdown,
+	};
+
+	at->evt.set_state_shutdown(&at->evt);
+
+	clockevents_config_and_register(&at->evt, at->rate, 0xf, CLOCKSOURCE_MASK(56));

This doesn't work on 32 bit - clockevents_config_and_register()'s final argument is an unsigned long, and a 56 bit mask doesn't fit. This triggers a compiler warning:

drivers/clocksource/arm_arch_timer_mmio.c: In function
'arch_timer_mmio_setup':
./include/linux/bits.h:47:9: error: conversion from 'long long unsigned
int' to 'long unsigned int' changes value from '72057594037927935' to '4294967295' [-Werror=overflow]

    47 |         ((t)(GENMASK_INPUT_CHECK(h, l) +                        \
       |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    48 |              (type_max(t) << (l) &                              \
       |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    49 |               type_max(t) >> (BITS_PER_TYPE(t) - 1 - (h)))))
       |               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 ./include/linux/bits.h:52:33: note: in expansion of macro 'GENMASK_TYPE'
    52 | #define GENMASK_ULL(h, l)       GENMASK_TYPE(unsigned long
long, h, l)
       |                                 ^~~~~~~~~~~~
 ./include/linux/clocksource.h:153:32: note: in expansion of macro
'GENMASK_ULL'
   153 | #define CLOCKSOURCE_MASK(bits) GENMASK_ULL((bits) - 1, 0)
       |                                ^~~~~~~~~~~
 drivers/clocksource/arm_arch_timer_mmio.c:255:66: note: in expansion of
macro 'CLOCKSOURCE_MASK'
   255 |         clockevents_config_and_register(&at->evt, at->rate,
0xf, CLOCKSOURCE_MASK(56));
       |

^~~~~~~~~~~~~~~~

Possible this should really be min(CLOCKSOURCE_MASK(56), ULONG_MAX)? But I'm not familiar enough with this code. Most likely it's dead code on a 32 bit platform.

Thanks,
Steve

author maintainer
#6 lore raw

Re: [PATCH 2/4] clocksource/drivers/arm_arch_timer: Add standalone MMIO driver

Quoted 29 lines · Steven Price
Quoted 13 lines · 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 | 420 ++++++++++++++++++++++
 2 files changed, 421 insertions(+)
 create mode 100644 drivers/clocksource/arm_arch_timer_mmio.c

[...]

Quoted 13 lines
drivers/clocksource/arm_arch_timer_mmio.c new file +13 -0
+static void arch_timer_mmio_setup(struct arch_timer *at, int irq)
+{
+	at->evt = (struct clock_event_device) {
+		.features		   = (CLOCK_EVT_FEAT_ONESHOT |
+					      CLOCK_EVT_FEAT_DYNIRQ),
+		.name			   = "arch_mem_timer",
+		.rating			   = 400,
+		.cpumask		   = cpu_possible_mask,
+		.irq 			   = irq,
+		.set_next_event		   = arch_timer_mmio_set_next_event,
+		.set_state_oneshot_stopped = arch_timer_mmio_shutdown,
+		.set_state_shutdown	   = arch_timer_mmio_shutdown,
+	};
Quoted 8 lines

drivers/clocksource/arm_arch_timer_mmio.c line 255

Quoted 4 lines
drivers/clocksource/arm_arch_timer_mmio.c new file +4 -0
+
+	at->evt.set_state_shutdown(&at->evt);
+
+	clockevents_config_and_register(&at->evt, at->rate, 0xf, CLOCKSOURCE_MASK(56));

This doesn't work on 32 bit - clockevents_config_and_register()'s final argument is an unsigned long, and a 56 bit mask doesn't fit. This triggers a compiler warning:

Already reported, see [email protected].

Quoted 3 lines

Possible this should really be min(CLOCKSOURCE_MASK(56), ULONG_MAX)? But I'm not familiar enough with this code. Most likely it's dead code on a 32 bit platform.

No, this definitely exists on 32bit crap, since it has been part of the architecture from the ARMv7+VE days.

I think this is more of an impedance mismatch between the CLOCKSOURCE_MASK() helper and the clockevents_config_and_register(), and a (unsigned long) cast would do the trick.

But it also means that the per-cpu timer also gets truncated the same way, and that has interesting impacts on how often the timer is reprogrammed.

Daniel, do you want a patch on top or a new series?

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author maintainer
#7 lore raw

Re: [PATCH 2/4] clocksource/drivers/arm_arch_timer: Add standalone MMIO driver

Quoted 47 lines · Marc Zyngier
Quoted 29 lines · Steven Price
Quoted 13 lines · 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 | 420 ++++++++++++++++++++++
 2 files changed, 421 insertions(+)
 create mode 100644 drivers/clocksource/arm_arch_timer_mmio.c

[...]

Quoted 13 lines
drivers/clocksource/arm_arch_timer_mmio.c new file +13 -0
+static void arch_timer_mmio_setup(struct arch_timer *at, int irq)
+{
+	at->evt = (struct clock_event_device) {
+		.features		   = (CLOCK_EVT_FEAT_ONESHOT |
+					      CLOCK_EVT_FEAT_DYNIRQ),
+		.name			   = "arch_mem_timer",
+		.rating			   = 400,
+		.cpumask		   = cpu_possible_mask,
+		.irq 			   = irq,
+		.set_next_event		   = arch_timer_mmio_set_next_event,
+		.set_state_oneshot_stopped = arch_timer_mmio_shutdown,
+		.set_state_shutdown	   = arch_timer_mmio_shutdown,
+	};
Quoted 8 lines

drivers/clocksource/arm_arch_timer_mmio.c line 255

Quoted 4 lines
drivers/clocksource/arm_arch_timer_mmio.c new file +4 -0
+
+	at->evt.set_state_shutdown(&at->evt);
+
+	clockevents_config_and_register(&at->evt, at->rate, 0xf, CLOCKSOURCE_MASK(56));

This doesn't work on 32 bit - clockevents_config_and_register()'s final argument is an unsigned long, and a 56 bit mask doesn't fit. This triggers a compiler warning:

Already reported, see [email protected].

Quoted 3 lines

Possible this should really be min(CLOCKSOURCE_MASK(56), ULONG_MAX)? But I'm not familiar enough with this code. Most likely it's dead code on a 32 bit platform.

Quoted 6 lines

No, this definitely exists on 32bit crap, since it has been part of the architecture from the ARMv7+VE days.

I think this is more of an impedance mismatch between the CLOCKSOURCE_MASK() helper and the clockevents_config_and_register(), and a (unsigned long) cast would do the trick.

Of course not. That would just result in a big fat zero.

Quoted 3 lines

But it also means that the per-cpu timer also gets truncated the same way, and that has interesting impacts on how often the timer is reprogrammed.

That question still stand, and I wonder whether we have ugly bugs lurking on 32bit platforms because of that... I'll try and have a look.

	M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
#8 lore raw

Re: [PATCH 2/4] clocksource/drivers/arm_arch_timer: Add standalone MMIO driver

Quoted 49 lines · Marc Zyngier
Quoted 47 lines · Marc Zyngier
Quoted 29 lines · Steven Price
Quoted 13 lines · 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 | 420 ++++++++++++++++++++++
 2 files changed, 421 insertions(+)
 create mode 100644 drivers/clocksource/arm_arch_timer_mmio.c

[...]

Quoted 13 lines
drivers/clocksource/arm_arch_timer_mmio.c new file +13 -0
+static void arch_timer_mmio_setup(struct arch_timer *at, int irq)
+{
+	at->evt = (struct clock_event_device) {
+		.features		   = (CLOCK_EVT_FEAT_ONESHOT |
+					      CLOCK_EVT_FEAT_DYNIRQ),
+		.name			   = "arch_mem_timer",
+		.rating			   = 400,
+		.cpumask		   = cpu_possible_mask,
+		.irq 			   = irq,
+		.set_next_event		   = arch_timer_mmio_set_next_event,
+		.set_state_oneshot_stopped = arch_timer_mmio_shutdown,
+		.set_state_shutdown	   = arch_timer_mmio_shutdown,
+	};
Quoted 8 lines

drivers/clocksource/arm_arch_timer_mmio.c line 255

Quoted 4 lines
drivers/clocksource/arm_arch_timer_mmio.c new file +4 -0
+
+	at->evt.set_state_shutdown(&at->evt);
+
+	clockevents_config_and_register(&at->evt, at->rate, 0xf, CLOCKSOURCE_MASK(56));

This doesn't work on 32 bit - clockevents_config_and_register()'s final argument is an unsigned long, and a 56 bit mask doesn't fit. This triggers a compiler warning:

Already reported, see [email protected].

Quoted 3 lines

Possible this should really be min(CLOCKSOURCE_MASK(56), ULONG_MAX)? But I'm not familiar enough with this code. Most likely it's dead code on a 32 bit platform.

Quoted 8 lines
Quoted 6 lines

No, this definitely exists on 32bit crap, since it has been part of the architecture from the ARMv7+VE days.

I think this is more of an impedance mismatch between the CLOCKSOURCE_MASK() helper and the clockevents_config_and_register(), and a (unsigned long) cast would do the trick.

Of course not. That would just result in a big fat zero.

No - CLOCKSOURCE_MASK() turns into GENMASK_ULL so a simple truncation to unsigned long would be ULONG_MAX (all 1s). So an unsigned long cast should be fine. My suggestion of MIN(, ULONG_MAX) was just to make that more clear.

Quoted 7 lines
Quoted 3 lines

But it also means that the per-cpu timer also gets truncated the same way, and that has interesting impacts on how often the timer is reprogrammed.

That question still stand, and I wonder whether we have ugly bugs lurking on 32bit platforms because of that... I'll try and have a look.

I don't know whether there are other bugs due to the capping to ULONG_MAX, but I don't think there's an (additional) bug here, it's "just" a ugly warning.

Thanks,
Steve

author maintainer
#9 lore raw

Re: [PATCH 2/4] clocksource/drivers/arm_arch_timer: Add standalone MMIO driver

Quoted 5 lines · Steven Price

No - CLOCKSOURCE_MASK() turns into GENMASK_ULL so a simple truncation to unsigned long would be ULONG_MAX (all 1s). So an unsigned long cast should be fine. My suggestion of MIN(, ULONG_MAX) was just to make that more clear.

Huh. yes, Can't think today. Sorry for the noise.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

But it also means that the per-cpu timer also gets truncated the same way, and that has interesting impacts on how often the timer is reprogrammed.

Quoted 7 lines
Quoted 3 lines

That question still stand, and I wonder whether we have ugly bugs lurking on 32bit platforms because of that... I'll try and have a look.

I don't know whether there are other bugs due to the capping to ULONG_MAX, but I don't think there's an (additional) bug here, it's "just" a ugly warning.

It really isn't about the warning, but how we express the maximum deadline to the core infrastructure. The MMIO driver is the least of our worries, and the CPU timer is the thing I'm concerned about, as it uses the same construct.

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

Re: [PATCH 2/4] clocksource/drivers/arm_arch_timer: Add standalone MMIO driver

In reply to Marc Zyngier
Quoted 53 lines · Marc Zyngier
Quoted 29 lines · Steven Price
Quoted 13 lines · 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 | 420 ++++++++++++++++++++++
  2 files changed, 421 insertions(+)

create mode 100644 drivers/clocksource/arm_arch_timer_mmio.c

[...]

Quoted 13 lines
drivers/clocksource/arm_arch_timer_mmio.c new file +13 -0
+static void arch_timer_mmio_setup(struct arch_timer *at, int irq)
+{
+	at->evt = (struct clock_event_device) {
+		.features		   = (CLOCK_EVT_FEAT_ONESHOT |
+					      CLOCK_EVT_FEAT_DYNIRQ),
+		.name			   = "arch_mem_timer",
+		.rating			   = 400,
+		.cpumask		   = cpu_possible_mask,
+		.irq 			   = irq,
+		.set_next_event		   = arch_timer_mmio_set_next_event,
+		.set_state_oneshot_stopped = arch_timer_mmio_shutdown,
+		.set_state_shutdown	   = arch_timer_mmio_shutdown,
+	};
Quoted 8 lines

drivers/clocksource/arm_arch_timer_mmio.c line 255

Quoted 4 lines
drivers/clocksource/arm_arch_timer_mmio.c new file +4 -0
+
+	at->evt.set_state_shutdown(&at->evt);
+
+	clockevents_config_and_register(&at->evt, at->rate, 0xf, CLOCKSOURCE_MASK(56));

This doesn't work on 32 bit - clockevents_config_and_register()'s final argument is an unsigned long, and a 56 bit mask doesn't fit. This triggers a compiler warning:

Already reported, see [email protected].

Quoted 3 lines

Possible this should really be min(CLOCKSOURCE_MASK(56), ULONG_MAX)? But I'm not familiar enough with this code. Most likely it's dead code on a 32 bit platform.

No, this definitely exists on 32bit crap, since it has been part of the architecture from the ARMv7+VE days.

I think this is more of an impedance mismatch between the CLOCKSOURCE_MASK() helper and the clockevents_config_and_register(), and a (unsigned long) cast would do the trick.

Quoted 5 lines

But it also means that the per-cpu timer also gets truncated the same way, and that has interesting impacts on how often the timer is reprogrammed.

Daniel, do you want a patch on top or a new series?

A new series please

Thanks

Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
author maintainer
#11 lore raw

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

In reply to Marc Zyngier

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
author maintainer
#12 lore raw

PATCH 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 fa7294ab609ce..f88e960c24006 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);
@@ -255,6 +264,16 @@ static void arch_timer_mmio_setup(struct arch_timer *at, int irq)
 	clockevents_config_and_register(&at->evt, at->rate, 0xf, 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
Tested-by Reviewed-by on the v1 series
#13 lore raw
In reply to Marc Zyngier

+Alexandru

Quoted 12 lines · Marc Zyngier

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 have 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.

Quoted 4 lines

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.

(Tested it on Juno R2 and FVP in both DT and ACPI boot)

Alexandru found it useful(avoids some unexpected hang IIUC) in his setup based on bootwrapper which doesn't initialise MMIO timers.

Signature · 2 lines
Regards,
Sudeep
#14 lore raw

Hello,

Quoted 20 lines · Sudeep Holla

+Alexandru

Quoted 12 lines · Marc Zyngier

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 have 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.

Quoted 4 lines

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.

Quoted 7 lines

(Tested it on Juno R2 and FVP in both DT and ACPI boot)

Alexandru found it useful(avoids some unexpected hang IIUC) in his setup based on bootwrapper which doesn't initialise MMIO timers.

Just FYI, this is the testing that I did.

Without this series, if firmware (boot-wrapper-aarch64 in my testing) doesn't configure access to the memory-mapped timer:

[    0.000000] arch_timer: Unable to find a suitable frame in timer @ 0x000000002a810000
[    0.000000] Failed to initialize '/timer@2a810000': -22
..
[    0.528000] kvm [1]: kvm_arch_timer: uninitialized timecounter
..
# ls /dev/kvm
ls: cannot access '/dev/kvm': No such file or directory

With this series, if firmware doesn't configure access to the memory-mapped timer:

[    0.549399] kvm [1]: Hyp nVHE mode initialized successfully
..
[    2.018050] arch-timer-mmio 2a810000.timer: Unable to find a suitable frame in timer @ 0x000000002a810000
[    2.018123] arch-timer-mmio 2a810000.timer: probe with driver arch-timer-mmio failed with error -22
..
# ls /dev/kvm
/dev/kvm

Thanks,
Alex

author maintainer
#15 lore raw
Quoted 28 lines · Alexandru Elisei

Hello,

Quoted 24 lines · Sudeep Holla

+Alexandru

Quoted 12 lines · Marc Zyngier

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 have 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.

Quoted 4 lines

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.

(Tested it on Juno R2 and FVP in both DT and ACPI boot)

Thanks!

Quoted 6 lines
Quoted 3 lines

Alexandru found it useful(avoids some unexpected hang IIUC) in his setup based on bootwrapper which doesn't initialise MMIO timers.

Just FYI, this is the testing that I did.

Quoted 8 lines

Without this series, if firmware (boot-wrapper-aarch64 in my testing) doesn't configure access to the memory-mapped timer:

[    0.000000] arch_timer: Unable to find a suitable frame in timer @ 0x000000002a810000
[    0.000000] Failed to initialize '/timer@2a810000': -22
..
[    0.528000] kvm [1]: kvm_arch_timer: uninitialized timecounter

..

Right, that's one of the many problems with the tight coupling between sysreg and MMIO timers -- if one fails, they both fail, and you're pretty lucky if you manage to limp along after that.

Quoted 10 lines
# ls /dev/kvm
ls: cannot access '/dev/kvm': No such file or directory

With this series, if firmware doesn't configure access to the memory-mapped timer:

[    0.549399] kvm [1]: Hyp nVHE mode initialized successfully
..
[    2.018050] arch-timer-mmio 2a810000.timer: Unable to find a suitable frame in timer @ 0x000000002a810000
[    2.018123] arch-timer-mmio 2a810000.timer: probe with driver arch-timer-mmio failed with error -22

Ah, you have managed to test the error path. Thanks for that!

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#16 lore raw
Quoted 2 lines · Marc Zyngier

On Wed, 13 Aug 2025 12:35:31 +0100, Alexandru Elisei <[email protected]> wrote:

[ ... ]

Quoted 11 lines
Quoted 9 lines

ls: cannot access '/dev/kvm': No such file or directory

With this series, if firmware doesn't configure access to the memory-mapped timer:

[    0.549399] kvm [1]: Hyp nVHE mode initialized successfully
..
[    2.018050] arch-timer-mmio 2a810000.timer: Unable to find a suitable frame in timer @ 0x000000002a810000
[    2.018123] arch-timer-mmio 2a810000.timer: probe with driver arch-timer-mmio failed with error -22

Ah, you have managed to test the error path. Thanks for that!

Applied (including patch 1/4)

Thanks

Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#17 lore raw
In reply to Alexandru Elisei
Quoted 47 lines · Alexandru Elisei

Hello,

Quoted 20 lines · Sudeep Holla

+Alexandru

Quoted 12 lines · Marc Zyngier

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 have 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.

Quoted 4 lines

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.

Quoted 7 lines

(Tested it on Juno R2 and FVP in both DT and ACPI boot)

Alexandru found it useful(avoids some unexpected hang IIUC) in his setup based on bootwrapper which doesn't initialise MMIO timers.

Just FYI, this is the testing that I did.

Without this series, if firmware (boot-wrapper-aarch64 in my testing) doesn't configure access to the memory-mapped timer:

[    0.000000] arch_timer: Unable to find a suitable frame in timer @ 0x000000002a810000
[    0.000000] Failed to initialize '/timer@2a810000': -22
..
[    0.528000] kvm [1]: kvm_arch_timer: uninitialized timecounter
..
# ls /dev/kvm
ls: cannot access '/dev/kvm': No such file or directory

With this series, if firmware doesn't configure access to the memory-mapped timer:

Quoted 8 lines
[    0.549399] kvm [1]: Hyp nVHE mode initialized successfully
..
[    2.018050] arch-timer-mmio 2a810000.timer: Unable to find a suitable frame in timer @ 0x000000002a810000
[    2.018123] arch-timer-mmio 2a810000.timer: probe with driver arch-timer-mmio failed with error -22
..
# ls /dev/kvm
/dev/kvm

Thanks for the details. I misunderstood as some VM boot hang and was wondering why I couldn't reproduce it(was failing gracefully though no KVM and hence VM fails to launch).

Signature · 2 lines
Regards,
Sudeep