Skip to contentSkip to conversation

clocksource/drivers/arm_arch_timer: Disable timer before programming CVAL

PATCH Applied [email protected] 5 participants 6 messages Jul 17, 2023 – Aug 31, 2023

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

PATCH 1/1 clocksource/drivers/arm_arch_timer: Disable timer before programming CVAL

From: Walter Chang <[email protected]>

Due to the fact that the use of writeq_relaxed() to program CVAL is not guaranteed to be atomic, it is necessary to disable the timer before programming CVAL.

However, if the MMIO timer is already enabled and has not yet expired, there is a possibility of unexpected behavior occurring: when the CPU enters the idle state during this period, and if the CPU's local event is earlier than the broadcast event, the following process occurs:

tick_broadcast_enter()
tick_broadcast_oneshot_control(TICK_BROADCAST_ENTER)
__tick_broadcast_oneshot_control()
___tick_broadcast_oneshot_control()
tick_broadcast_set_event()
clockevents_program_event()
set_next_event_mem()

During this process, the MMIO timer remains enabled while programming CVAL. To prevent such behavior, disable timer explicitly prior to programming CVAL.

Fixes 8b82c4f883a7 ("clocksource/drivers/arm_arch_timer: Move MMIO timer programming over to CVAL")
---
 drivers/clocksource/arm_arch_timer.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/clocksource/arm_arch_timer.c +7 -0
index e733a2a1927a..7dd2c615bce2 100644
@@ -792,6 +792,13 @@ static __always_inline void set_next_event_mem(const int access, unsigned long e
 	u64 cnt;
 
 	ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
+
+	/* Timer must be disabled before programming CVAL */
+	if (ctrl & ARCH_TIMER_CTRL_ENABLE) {
+		ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
+		arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
+	}
+
 	ctrl |= ARCH_TIMER_CTRL_ENABLE;
 	ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
 
Signature · 1 line
2.18.0
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 20 lines · On Mon, 17 Jul 2023 10:07:34 +0100, <[email protected]> wrote:

From: Walter Chang <[email protected]>

Due to the fact that the use of writeq_relaxed() to program CVAL is not guaranteed to be atomic, it is necessary to disable the timer before programming CVAL.

However, if the MMIO timer is already enabled and has not yet expired, there is a possibility of unexpected behavior occurring: when the CPU enters the idle state during this period, and if the CPU's local event is earlier than the broadcast event, the following process occurs:

tick_broadcast_enter()
tick_broadcast_oneshot_control(TICK_BROADCAST_ENTER)
__tick_broadcast_oneshot_control()
___tick_broadcast_oneshot_control()
tick_broadcast_set_event()
clockevents_program_event()
set_next_event_mem()

Quoted 7 lines

During this process, the MMIO timer remains enabled while programming CVAL. To prevent such behavior, disable timer explicitly prior to programming CVAL.

Fixes 8b82c4f883a7 ("clocksource/drivers/arm_arch_timer: Move MMIO timer programming over to CVAL")
	M.
Signature · 6 lines
Without deviation from the norm, progress is not possible.

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to [email protected]

Il 17/07/23 11:07, [email protected] ha scritto:

Quoted 19 lines

From: Walter Chang <[email protected]>

Due to the fact that the use of writeq_relaxed() to program CVAL is not guaranteed to be atomic, it is necessary to disable the timer before programming CVAL.

However, if the MMIO timer is already enabled and has not yet expired, there is a possibility of unexpected behavior occurring: when the CPU enters the idle state during this period, and if the CPU's local event is earlier than the broadcast event, the following process occurs:

tick_broadcast_enter()
tick_broadcast_oneshot_control(TICK_BROADCAST_ENTER)
__tick_broadcast_oneshot_control()
___tick_broadcast_oneshot_control()
tick_broadcast_set_event()
clockevents_program_event()
set_next_event_mem()

Quoted 7 lines

During this process, the MMIO timer remains enabled while programming CVAL. To prevent such behavior, disable timer explicitly prior to programming CVAL.

Fixes 8b82c4f883a7 ("clocksource/drivers/arm_arch_timer: Move MMIO timer programming over to CVAL")
Quoted 12 lines
---
  drivers/clocksource/arm_arch_timer.c | 7 +++++++
  1 file changed, 7 insertions(+)
drivers/clocksource/arm_arch_timer.c +0 -0
index e733a2a1927a..7dd2c615bce2 100644
@@ -792,6 +792,13 @@ static __always_inline void set_next_event_mem(const int access, unsigned long e
  	u64 cnt;
 
  	ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);

drivers/clocksource/arm_arch_timer.c static __always_inline void set_next_event_mem(const int access, unsigned long e · line 804

Quoted 10 lines
drivers/clocksource/arm_arch_timer.c +7 -0
+
+	/* Timer must be disabled before programming CVAL */
+	if (ctrl & ARCH_TIMER_CTRL_ENABLE) {
+		ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
+		arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
+	}
+
  	ctrl |= ARCH_TIMER_CTRL_ENABLE;
  	ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
 

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#3 lore raw
In reply to [email protected]
Quoted 51 lines · On Mon, 2023-07-17 at 17:07 +0800, [email protected] wrote:

From: Walter Chang <[email protected]>

Due to the fact that the use of writeq_relaxed() to program CVAL is not guaranteed to be atomic, it is necessary to disable the timer before
programming CVAL.

However, if the MMIO timer is already enabled and has not yet expired,
there is a possibility of unexpected behavior occurring: when the CPU enters the idle state during this period, and if the CPU's local event
is earlier than the broadcast event, the following process occurs:

tick_broadcast_enter()
tick_broadcast_oneshot_control(TICK_BROADCAST_ENTER)
__tick_broadcast_oneshot_control()
___tick_broadcast_oneshot_control()
tick_broadcast_set_event()
clockevents_program_event()
set_next_event_mem()

During this process, the MMIO timer remains enabled while programming CVAL. To prevent such behavior, disable timer explicitly prior to programming CVAL.

Fixes 8b82c4f883a7 ("clocksource/drivers/arm_arch_timer: Move MMIO

timer programming over to CVAL")

---
 drivers/clocksource/arm_arch_timer.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/clocksource/arm_arch_timer.c +0 -0
index e733a2a1927a..7dd2c615bce2 100644
@@ -792,6 +792,13 @@ static __always_inline void

set_next_event_mem(const int access, unsigned long e

drivers/clocksource/arm_arch_timer.c +5 -0
 	u64 cnt;
 
 	ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
+
+	/* Timer must be disabled before programming CVAL */
+	if (ctrl & ARCH_TIMER_CTRL_ENABLE) {
+		ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
+		arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl,

clk);

drivers/clocksource/arm_arch_timer.c static __always_inline void set_next_event_mem(const int access, unsigned long e · line 803

Quoted 4 lines
drivers/clocksource/arm_arch_timer.c +2 -0
+	}
+
 	ctrl |= ARCH_TIMER_CTRL_ENABLE;
 	ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;

Gentle ping for this patch.

Thanks,
Walter Chang

#4 lore raw
Quoted 44 lines · Walter Chang (張維哲)
Quoted 43 lines · On Mon, 2023-07-17 at 17:07 +0800, [email protected] wrote:

From: Walter Chang <[email protected]>

Due to the fact that the use of writeq_relaxed() to program CVAL is not guaranteed to be atomic, it is necessary to disable the timer before
programming CVAL.

However, if the MMIO timer is already enabled and has not yet expired,
there is a possibility of unexpected behavior occurring: when the CPU enters the idle state during this period, and if the CPU's local event
is earlier than the broadcast event, the following process occurs:

tick_broadcast_enter()
tick_broadcast_oneshot_control(TICK_BROADCAST_ENTER)
__tick_broadcast_oneshot_control()
___tick_broadcast_oneshot_control()
tick_broadcast_set_event()
clockevents_program_event()
set_next_event_mem()

During this process, the MMIO timer remains enabled while programming CVAL. To prevent such behavior, disable timer explicitly prior to programming CVAL.

Fixes 8b82c4f883a7 ("clocksource/drivers/arm_arch_timer: Move MMIO

timer programming over to CVAL")

---
 drivers/clocksource/arm_arch_timer.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/clocksource/arm_arch_timer.c +0 -0
index e733a2a1927a..7dd2c615bce2 100644
@@ -792,6 +792,13 @@ static __always_inline void

set_next_event_mem(const int access, unsigned long e
u64 cnt;

Quoted 14 lines

drivers/clocksource/arm_arch_timer.c static __always_inline void set_next_event_mem(const int access, unsigned long e · line 803

Quoted 12 lines

ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);

drivers/clocksource/arm_arch_timer.c +5 -0
+
+/* Timer must be disabled before programming CVAL */
+if (ctrl & ARCH_TIMER_CTRL_ENABLE) {
+ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
+arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl,

clk);

drivers/clocksource/arm_arch_timer.c +2 -0
+}
+
 ctrl |= ARCH_TIMER_CTRL_ENABLE;
 ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;

Gentle ping for this patch.

Applied, 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
merge bot Applied
#5 lore raw
In reply to [email protected]
The following commit has been merged into the timers/core branch of tip:

The following commit has been merged into the timers/core branch of tip:

Commit-ID:     e7d65e40ab5a5940785c5922f317602d0268caaf
Gitweb:        https://git.kernel.org/tip/e7d65e40ab5a5940785c5922f317602d0268caaf
Author:        Walter Chang <[email protected]>
AuthorDate:    Mon, 17 Jul 2023 17:07:34 +08:00
Committer:     Daniel Lezcano <[email protected]>
CommitterDate: Fri, 18 Aug 2023 12:06:16 +02:00

clocksource/drivers/arm_arch_timer: Disable timer before programming CVAL

Due to the fact that the use of writeq_relaxed() to program CVAL is not guaranteed to be atomic, it is necessary to disable the timer before programming CVAL.

However, if the MMIO timer is already enabled and has not yet expired, there is a possibility of unexpected behavior occurring: when the CPU enters the idle state during this period, and if the CPU's local event is earlier than the broadcast event, the following process occurs:

tick_broadcast_enter()
tick_broadcast_oneshot_control(TICK_BROADCAST_ENTER)
__tick_broadcast_oneshot_control()
___tick_broadcast_oneshot_control()
tick_broadcast_set_event()
clockevents_program_event()
set_next_event_mem()

During this process, the MMIO timer remains enabled while programming CVAL. To prevent such behavior, disable timer explicitly prior to programming CVAL.

Fixes 8b82c4f883a7 ("clocksource/drivers/arm_arch_timer: Move MMIO timer programming over to CVAL")
---
 drivers/clocksource/arm_arch_timer.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/clocksource/arm_arch_timer.c +7 -0
index e09d442..f6c5f89 100644
@@ -774,6 +774,13 @@ static __always_inline void set_next_event_mem(const int access, unsigned long e
 	u64 cnt;
 
 	ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
+
+	/* Timer must be disabled before programming CVAL */
+	if (ctrl & ARCH_TIMER_CTRL_ENABLE) {
+		ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
+		arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
+	}
+
 	ctrl |= ARCH_TIMER_CTRL_ENABLE;
 	ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;