Skip to contentSkip to conversation

arm: arch_timer cleanups and niceties for 3.10

PATCH New Mark Rutland 4 participants 14 messages Mar 27, 2013 – Apr 4, 2013

Subsystems (7)
lore
Share & tools
lore

Mark Rutland

author maintainer
#0 lore raw

cover arm: arch_timer cleanups and niceties for 3.10

The following patches clean up some (harmless) fallout from the changes in 3.9, and make the driver always use the virtual counter, eliminating problems relating to choosing which counters to use (racy initialisation, branches in hot paths).

The patches are based on v3.9-rc4, and have been tested on a TC2 with KVM.

The patches can also be found in my public git tree:
git://linux-arm.org/git/linux-mr.git clocks/arch-timer-cleanups

Rob, patch 3 "clocksource: arch_timer: use virtual counters" clashes with your CLKSRC_OF patches, but only by the fact nearby lines are alterered. The resolution is trivial and I'm happy to rebase this series as necessary.

Thanks,
Mark.

Marc Zyngier (2):
ARM: hyp: initialize CNTVOFF to zero
ARM: arch_timer: stop virtual timer when booted in HYP mode

Mark Rutland (3):
ARM: KVM: arch_timers: zero CNTVOFF upon return to host
clocksource: arch_timer: use virtual counters
arm: fix up ARM_ARCH_TIMER selects

 arch/arm/include/asm/arch_timer.h    |  9 ---------
 arch/arm/kernel/hyp-stub.S           |  7 +++++++
 arch/arm/kvm/interrupts_head.S       |  4 ++++
 arch/arm/mach-exynos/Kconfig         |  2 +-
 arch/arm/mach-tegra/Kconfig          |  2 +-
 arch/arm/mach-virt/Kconfig           |  2 +-
 arch/arm64/include/asm/arch_timer.h  | 10 ----------
 drivers/clocksource/arm_arch_timer.c | 23 +++++------------------
 include/clocksource/arm_arch_timer.h |  2 +-
 9 files changed, 20 insertions(+), 41 deletions(-)
Signature · 1 line
1.8.1.1
author maintainer
#1 lore raw

PATCH 1/5 ARM: hyp: initialize CNTVOFF to zero

From: Marc Zyngier <[email protected]>

In order to be able to use the virtual counter in a safe way, make sure it is initialized to zero before dropping to SVC.

---
 arch/arm/kernel/hyp-stub.S | 2 ++
 1 file changed, 2 insertions(+)
arch/arm/kernel/hyp-stub.S +2 -0
index 1315c4c..dbe2110 100644
@@ -153,6 +153,8 @@ THUMB(	orr	r7, #(1 << 30)	)	@ HSCTLR.TE
 	mrc	p15, 4, r7, c14, c1, 0	@ CNTHCTL
 	orr	r7, r7, #3		@ PL1PCEN | PL1PCTEN
 	mcr	p15, 4, r7, c14, c1, 0	@ CNTHCTL
+	mov	r7, #0
+	mcrr	p15, 4, r7, r7, c14	@ CNTVOFF
 1:
 #endif
 
Signature · 1 line
1.8.1.1
author maintainer
#2 lore raw

PATCH 2/5 ARM: KVM: arch_timers: zero CNTVOFF upon return to host

In reply to Mark Rutland

To use the virtual counters from the host, we need to ensure that CNTVOFF doesn't change unexpectedly. When we change to a guest, we replace the host's CNTVOFF, but we don't restore it when returning to the host.

As the host sets CNTVOFF to zero, and never changes it, we can simply zero CNTVOFF when returning to the host. This patch adds said zeroing to the return to host path.

---
 arch/arm/kvm/interrupts_head.S | 4 ++++
 1 file changed, 4 insertions(+)
arch/arm/kvm/interrupts_head.S +4 -0
index 3c8f2f0..d43cfb5 100644
@@ -497,6 +497,10 @@ vcpu	.req	r0		@ vcpu pointer always in r0
 	add	r5, vcpu, r4
 	strd	r2, r3, [r5]
 
+	@ Ensure host CNTVCT == CNTPCT
+	mov	r2, #0
+	mcrr	p15, 4, r2, r2, c14	@ CNTVOFF
+
 1:
 #endif
 	@ Allow physical timer/counter access for the host
Signature · 1 line
1.8.1.1
#3 lore raw

PATCH 2/5 ARM: KVM: arch_timers: zero CNTVOFF upon return to host

Quoted 32 lines · Mark Rutland

To use the virtual counters from the host, we need to ensure that CNTVOFF doesn't change unexpectedly. When we change to a guest, we replace the host's CNTVOFF, but we don't restore it when returning to the host.

As the host sets CNTVOFF to zero, and never changes it, we can simply zero CNTVOFF when returning to the host. This patch adds said zeroing to the return to host path.

---
 arch/arm/kvm/interrupts_head.S | 4 ++++
 1 file changed, 4 insertions(+)
arch/arm/kvm/interrupts_head.S +4 -1
index 3c8f2f0..d43cfb5 100644
@@ -497,6 +497,10 @@ vcpu       .req    r0              @ vcpu pointer always in r0
        add     r5, vcpu, r4
        strd    r2, r3, [r5]
 
+       @ Ensure host CNTVCT == CNTPCT
+       mov     r2, #0
+       mcrr    p15, 4, r2, r2, c14     @ CNTVOFF
+
 1:
 #endif
        @ Allow physical timer/counter access for the host
--
Quoted 3 lines

1.8.1.1

looks good to me.

Merged into kvm-arm-fixes.

-Christoffer

author maintainer
#4 lore raw

PATCH 2/5 ARM: KVM: arch_timers: zero CNTVOFF upon return to host

Hi Christoffer,

Quoted 39 lines · Christoffer Dall
Quoted 32 lines · Mark Rutland

To use the virtual counters from the host, we need to ensure that CNTVOFF doesn't change unexpectedly. When we change to a guest, we replace the host's CNTVOFF, but we don't restore it when returning to the host.

As the host sets CNTVOFF to zero, and never changes it, we can simply zero CNTVOFF when returning to the host. This patch adds said zeroing to the return to host path.

---
 arch/arm/kvm/interrupts_head.S | 4 ++++
 1 file changed, 4 insertions(+)
arch/arm/kvm/interrupts_head.S +4 -1
index 3c8f2f0..d43cfb5 100644
@@ -497,6 +497,10 @@ vcpu       .req    r0              @ vcpu pointer always in r0
        add     r5, vcpu, r4
        strd    r2, r3, [r5]
 
+       @ Ensure host CNTVCT == CNTPCT
+       mov     r2, #0
+       mcrr    p15, 4, r2, r2, c14     @ CNTVOFF
+
 1:
 #endif
        @ Allow physical timer/counter access for the host
--
Quoted 3 lines

1.8.1.1

looks good to me.

Quoted 1 line

Merged into kvm-arm-fixes.

As this patch depends on the previous patch (which sets CNTVOFF to 0 in the hyp stub), and the rest of the patches depend on both this patch and the previous for correct operation, the series really needs to be taken together.

Would you be able to give your ack instead?

This patch is only required because the later patches in this series need it to allow the host to use virtual counters, nothing else requires the host's CNTVOFF to be 0 at the moment as both physical counters and timers are used by the host.

Thanks,
Mark.

#5 lore raw

PATCH 2/5 ARM: KVM: arch_timers: zero CNTVOFF upon return to host

Quoted 42 lines · Mark Rutland

Hi Christoffer,

Quoted 39 lines · Christoffer Dall
Quoted 32 lines · Mark Rutland

To use the virtual counters from the host, we need to ensure that CNTVOFF doesn't change unexpectedly. When we change to a guest, we replace the host's CNTVOFF, but we don't restore it when returning to the host.

As the host sets CNTVOFF to zero, and never changes it, we can simply zero CNTVOFF when returning to the host. This patch adds said zeroing to the return to host path.

---
 arch/arm/kvm/interrupts_head.S | 4 ++++
 1 file changed, 4 insertions(+)
arch/arm/kvm/interrupts_head.S +4 -1
index 3c8f2f0..d43cfb5 100644
@@ -497,6 +497,10 @@ vcpu       .req    r0              @ vcpu pointer always in r0
        add     r5, vcpu, r4
        strd    r2, r3, [r5]
 
+       @ Ensure host CNTVCT == CNTPCT
+       mov     r2, #0
+       mcrr    p15, 4, r2, r2, c14     @ CNTVOFF
+
 1:
 #endif
        @ Allow physical timer/counter access for the host
--
Quoted 3 lines

1.8.1.1

looks good to me.

Quoted 6 lines
Quoted 1 line

Merged into kvm-arm-fixes.

As this patch depends on the previous patch (which sets CNTVOFF to 0 in the hyp stub), and the rest of the patches depend on both this patch and the previous for correct operation, the series really needs to be taken together.

I don't see why this patch depends on the prior patches, I see it the other way around.

Quoted 1 line

Would you be able to give your ack instead?

But sure, I definitely ack the patch.

Quoted 6 lines

This patch is only required because the later patches in this series need it to allow the host to use virtual counters, nothing else requires the host's CNTVOFF to be 0 at the moment as both physical counters and timers are used by the host.

I understand, it doesn't hurt either though.

-Christoffer

author maintainer
#6 lore raw

PATCH 2/5 ARM: KVM: arch_timers: zero CNTVOFF upon return to host

Quoted 45 lines · Christoffer Dall
Quoted 44 lines · Mark Rutland

Hi Christoffer,

Quoted 40 lines · Christoffer Dall
Quoted 32 lines · Mark Rutland

To use the virtual counters from the host, we need to ensure that CNTVOFF doesn't change unexpectedly. When we change to a guest, we replace the host's CNTVOFF, but we don't restore it when returning to the host.

As the host sets CNTVOFF to zero, and never changes it, we can simply zero CNTVOFF when returning to the host. This patch adds said zeroing to the return to host path.

---
 arch/arm/kvm/interrupts_head.S | 4 ++++
 1 file changed, 4 insertions(+)
arch/arm/kvm/interrupts_head.S +4 -1
index 3c8f2f0..d43cfb5 100644
@@ -497,6 +497,10 @@ vcpu       .req    r0              @ vcpu pointer always in r0
        add     r5, vcpu, r4
        strd    r2, r3, [r5]
 
+       @ Ensure host CNTVCT == CNTPCT
+       mov     r2, #0
+       mcrr    p15, 4, r2, r2, c14     @ CNTVOFF
+
 1:
 #endif
        @ Allow physical timer/counter access for the host
--
Quoted 3 lines

1.8.1.1

looks good to me.

Merged into kvm-arm-fixes.

Quoted 7 lines
Quoted 4 lines

As this patch depends on the previous patch (which sets CNTVOFF to 0 in the hyp stub), and the rest of the patches depend on both this patch and the previous for correct operation, the series really needs to be taken together.

I don't see why this patch depends on the prior patches, I see it the other way around.

The patch itself doesn't stricly depend on the others, but the description
does: "As the host sets CNTVOFF to zero" - this is only true once the previous patch has been applied. As the later patches have a dependence on this patch, I don't think it makes sense for it to go in on its own -- if anything it makes it more difficult to apply the rest of the series as it now has a dependence on or conflict with kvm-arm-fixes.

Quoted 4 lines
Quoted 2 lines

Would you be able to give your ack instead?

But sure, I definitely ack the patch.

Cheers.

Mark.

author maintainer
#7 lore raw

PATCH 3/5 clocksource: arch_timer: use virtual counters

In reply to Mark Rutland

Switching between reading the virtual or physical counters is problematic, as some core code wants a view of time before we're fully set up. Using a function pointer and switching the source after the first read can make time appear to go backwards, and having a check in the read function is an unfortunate block on what we want to be a fast path.

Instead, this patch makes us always use the virtual counters. If we're a guest, or don't have hyp mode, we'll use the virtual timers, and as such don't care about CNTVOFF as long as it doesn't change in such a way as to make time travel backwards. If we do have hyp mode, and might be a KVM host, we have to use the physical timers, and require CNTVOFF to be zero so as to have a consistent view of time between the physical timers and virtual counters.

---
 arch/arm/include/asm/arch_timer.h    |  9 ---------
 arch/arm64/include/asm/arch_timer.h  | 10 ----------
 drivers/clocksource/arm_arch_timer.c | 23 +++++------------------
 include/clocksource/arm_arch_timer.h |  2 +-
 4 files changed, 6 insertions(+), 38 deletions(-)
arch/arm/include/asm/arch_timer.h +0 -9
index 7ade91d..35fea17 100644
@@ -81,15 +81,6 @@ static inline u32 arch_timer_get_cntfrq(void)
 	return val;
 }
 
-static inline u64 arch_counter_get_cntpct(void)
-{
-	u64 cval;
-
-	isb();
-	asm volatile("mrrc p15, 0, %Q0, %R0, c14" : "=r" (cval));
-	return cval;
-}
-
 static inline u64 arch_counter_get_cntvct(void)
 {
 	u64 cval;
arch/arm64/include/asm/arch_timer.h +0 -10
index 91e2a6a..5307737 100644
@@ -110,16 +110,6 @@ static inline void __cpuinit arch_counter_set_user_access(void)
 	asm volatile("msr	cntkctl_el1, %0" : : "r" (cntkctl));
 }
 
-static inline u64 arch_counter_get_cntpct(void)
-{
-	u64 cval;
-
-	isb();
-	asm volatile("mrs %0, cntpct_el0" : "=r" (cval));
-
-	return cval;
-}
-
 static inline u64 arch_counter_get_cntvct(void)
 {
 	u64 cval;
drivers/clocksource/arm_arch_timer.c +5 -18
index d7ad425..2abb861 100644
@@ -186,27 +186,19 @@ u32 arch_timer_get_rate(void)
 	return arch_timer_rate;
 }
 
-/*
- * Some external users of arch_timer_read_counter (e.g. sched_clock) may try to
- * call it before it has been initialised. Rather than incur a performance
- * penalty checking for initialisation, provide a default implementation that
- * won't lead to time appearing to jump backwards.
- */
-static u64 arch_timer_read_zero(void)
+u64 arch_timer_read_counter(void)
 {
-	return 0;
+	return arch_counter_get_cntvct();
 }
 
-u64 (*arch_timer_read_counter)(void) = arch_timer_read_zero;
-
 static cycle_t arch_counter_read(struct clocksource *cs)
 {
-	return arch_timer_read_counter();
+	return arch_counter_get_cntvct();
 }
 
 static cycle_t arch_counter_read_cc(const struct cyclecounter *cc)
 {
-	return arch_timer_read_counter();
+	return arch_counter_get_cntvct();
 }
 
 static struct clocksource clocksource_counter = {
@@ -285,7 +277,7 @@ static int __init arch_timer_register(void)
 	cyclecounter.mult = clocksource_counter.mult;
 	cyclecounter.shift = clocksource_counter.shift;
 	timecounter_init(&timecounter, &cyclecounter,
-			 arch_counter_get_cntpct());
+			 arch_counter_get_cntvct());
 
 	if (arch_timer_use_virtual) {
 		ppi = arch_timer_ppi[VIRT_PPI];
@@ -382,10 +374,5 @@ int __init arch_timer_init(void)
 		}
 	}
 
-	if (arch_timer_use_virtual)
-		arch_timer_read_counter = arch_counter_get_cntvct;
-	else
-		arch_timer_read_counter = arch_counter_get_cntpct;
-
 	return arch_timer_register();
 }
include/clocksource/arm_arch_timer.h +1 -1
index 2603267..3883812 100644
@@ -33,7 +33,7 @@
 
 extern int arch_timer_init(void);
 extern u32 arch_timer_get_rate(void);
-extern u64 (*arch_timer_read_counter)(void);
+extern u64 arch_timer_read_counter(void);
 extern struct timecounter *arch_timer_get_timecounter(void);
 
 #else
Signature · 1 line
1.8.1.1
author maintainer
#8 lore raw

PATCH 4/5 arm: fix up ARM_ARCH_TIMER selects

In reply to Mark Rutland

In 8a4da6e: "arm: arch_timer: move core to drivers/clocksource", the selection of ARM_ARCH_TIMER was indirected via HAVE_ARM_ARCH_TIMER, though mach-exynos's selection of ARM_ARCH_TIMER was missed, and since then mach-virt and mach-tegra have begun selecting ARM_ARCH_TIMER. This can lead to architected timer support erroneously appearing to not be selected in menuconfig.

This patch fixes up the Kconfigs for those platforms to select HAVE_ARM_ARCH_TIMER.

Cc Kukjin Kim <[email protected]>
Cc Stephen Warren <[email protected]>
Cc Marc Zyngier <[email protected]>
---
 arch/arm/mach-exynos/Kconfig | 2 +-
 arch/arm/mach-tegra/Kconfig  | 2 +-
 arch/arm/mach-virt/Kconfig   | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
arch/arm/mach-exynos/Kconfig +1 -1
index 70f94c8..fd45ec0 100644
@@ -72,7 +72,7 @@ config SOC_EXYNOS5440
 	bool "SAMSUNG EXYNOS5440"
 	default y
 	depends on ARCH_EXYNOS5
-	select ARM_ARCH_TIMER
+	select HAVE_ARM_ARCH_TIMER
 	select AUTO_ZRELADDR
 	select PINCTRL
 	select PINCTRL_EXYNOS5440
arch/arm/mach-tegra/Kconfig +1 -1
index d1c4893..2a4c9b8 100644
@@ -45,7 +45,7 @@ config ARCH_TEGRA_3x_SOC
 
 config ARCH_TEGRA_114_SOC
 	bool "Enable support for Tegra114 family"
-	select ARM_ARCH_TIMER
+	select HAVE_ARM_ARCH_TIMER
 	select ARM_GIC
 	select ARM_L1_CACHE_SHIFT_6
 	select CPU_V7
arch/arm/mach-virt/Kconfig +1 -1
index 8958f0d..081d469 100644
@@ -2,7 +2,7 @@ config ARCH_VIRT
 	bool "Dummy Virtual Machine" if ARCH_MULTI_V7
 	select ARCH_WANT_OPTIONAL_GPIOLIB
 	select ARM_GIC
-	select ARM_ARCH_TIMER
+	select HAVE_ARM_ARCH_TIMER
 	select ARM_PSCI
 	select HAVE_SMP
 	select CPU_V7
Signature · 1 line
1.8.1.1
#9 lore raw

PATCH 4/5 arm: fix up ARM_ARCH_TIMER selects

Quoted 9 lines · Mark Rutland

In 8a4da6e: "arm: arch_timer: move core to drivers/clocksource", the selection of ARM_ARCH_TIMER was indirected via HAVE_ARM_ARCH_TIMER, though mach-exynos's selection of ARM_ARCH_TIMER was missed, and since then mach-virt and mach-tegra have begun selecting ARM_ARCH_TIMER. This can lead to architected timer support erroneously appearing to not be selected in menuconfig.

This patch fixes up the Kconfigs for those platforms to select HAVE_ARM_ARCH_TIMER.

I assume you'll take this patch through some tree of yours.

author maintainer
#10 lore raw

PATCH 4/5 arm: fix up ARM_ARCH_TIMER selects

Quoted 8 lines · Stephen Warren
Quoted 7 lines · Mark Rutland

In 8a4da6e: "arm: arch_timer: move core to drivers/clocksource", the selection of ARM_ARCH_TIMER was indirected via HAVE_ARM_ARCH_TIMER, though mach-exynos's selection of ARM_ARCH_TIMER was missed, and since then mach-virt and mach-tegra have begun selecting ARM_ARCH_TIMER. This can lead to architected timer support erroneously appearing to not be selected in menuconfig.

Quoted 4 lines
Quoted 2 lines

This patch fixes up the Kconfigs for those platforms to select HAVE_ARM_ARCH_TIMER.

Cheers.

Quoted 3 lines

I assume you'll take this patch through some tree of yours.

Yup, that's the plan.

Mark.

author maintainer
#11 lore raw

PATCH 5/5 ARM: arch_timer: stop virtual timer when booted in HYP mode

In reply to Mark Rutland

From: Marc Zyngier <[email protected]>

When booting the kernel, a bootloader could have left the virtual timer ticking away, potentially generating interrupts. This could be troublesome if the user of the virtual timer is not careful when enabling the interrupt.

In order to avoid any surprise, stop the virtual timer from interrupting us when booted in HYP mode, as we'll use the physical timer in this case.

---
 arch/arm/kernel/hyp-stub.S | 5 +++++
 1 file changed, 5 insertions(+)
arch/arm/kernel/hyp-stub.S +5 -0
index dbe2110..4910232 100644
@@ -155,6 +155,11 @@ THUMB(	orr	r7, #(1 << 30)	)	@ HSCTLR.TE
 	mcr	p15, 4, r7, c14, c1, 0	@ CNTHCTL
 	mov	r7, #0
 	mcrr	p15, 4, r7, r7, c14	@ CNTVOFF
+
+	@ Disable virtual timer in case it was counting
+	mrc	p15, 0, r7, c14, c3, 1	@ CNTV_CTL
+	bic	r7, #1			@ Clear ENABLE
+	mcr	p15, 0, r7, c14, c3, 1	@ CNTV_CTL
 1:
 #endif
 
Signature · 1 line
1.8.1.1
#12 lore raw

cover arm: arch_timer cleanups and niceties for 3.10

In reply to Mark Rutland

Mark,

Quoted 8 lines · Mark Rutland

The following patches clean up some (harmless) fallout from the changes in 3.9, and make the driver always use the virtual counter, eliminating problems relating to choosing which counters to use (racy initialisation, branches in hot paths).

The patches are based on v3.9-rc4, and have been tested on a TC2 with KVM.

Quoted 8 lines

The patches can also be found in my public git tree:
git://linux-arm.org/git/linux-mr.git clocks/arch-timer-cleanups

Rob, patch 3 "clocksource: arch_timer: use virtual counters" clashes with your CLKSRC_OF patches, but only by the fact nearby lines are alterered. The resolution is trivial and I'm happy to rebase this series as necessary.

After scanning the patches, most of them I see as fixes rather than clean-up. Patches as such looks fine to me.
FWIW,

author maintainer
#13 lore raw

cover arm: arch_timer cleanups and niceties for 3.10

Quoted 19 lines · Santosh Shilimkar

Mark,

Quoted 8 lines · Mark Rutland

The following patches clean up some (harmless) fallout from the changes in 3.9, and make the driver always use the virtual counter, eliminating problems relating to choosing which counters to use (racy initialisation, branches in hot paths).

The patches are based on v3.9-rc4, and have been tested on a TC2 with KVM.

Quoted 8 lines

The patches can also be found in my public git tree:
git://linux-arm.org/git/linux-mr.git clocks/arch-timer-cleanups

Rob, patch 3 "clocksource: arch_timer: use virtual counters" clashes with your CLKSRC_OF patches, but only by the fact nearby lines are alterered. The resolution is trivial and I'm happy to rebase this series as necessary.

Quoted 4 lines

After scanning the patches, most of them I see as fixes rather than clean-up. Patches as such looks fine to me.
FWIW,

Cheers.

Mark.