ARM virtual counter supports event stream, it can only trigger an event
when the trigger bit (the value of CNTKCTL_EL1.EVNTI) of CNTVCT_EL0 changes,
so the actual period of event stream is 2^(cntkctl_evnti + 1). For example,
when the trigger bit is 0, then virtual counter trigger an event for every
two cycles.

Fixes: 037f637767a8 ("drivers: clocksource: add support for ARM architected 
timer event stream")
Suggested-by: Marc Zyngier <m...@kernel.org>
Signed-off-by: Keqian Zhu <zhukeqi...@huawei.com>
---
 drivers/clocksource/arm_arch_timer.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)

diff --git a/drivers/clocksource/arm_arch_timer.c 
b/drivers/clocksource/arm_arch_timer.c
index 777d38cb39b0..d0177824c518 100644
--- a/drivers/clocksource/arm_arch_timer.c
+++ b/drivers/clocksource/arm_arch_timer.c
@@ -822,15 +822,24 @@ static void arch_timer_evtstrm_enable(int divider)
 
 static void arch_timer_configure_evtstream(void)
 {
-       int evt_stream_div, pos;
+       int evt_stream_div, lsb;
+
+       /*
+        * As the event stream can at most be generated at half the frequency
+        * of the counter, use half the frequency when computing the divider.
+        */
+       evt_stream_div = arch_timer_rate / ARCH_TIMER_EVT_STREAM_FREQ / 2;
+
+       /*
+        * Find the closest power of two to the divisor. If the adjacent bit
+        * of lsb (last set bit, starts from 0) is set, then we use (lsb + 1).
+        */
+       lsb = fls(evt_stream_div) - 1;
+       if (lsb > 0 && (evt_stream_div & BIT(lsb - 1)))
+               lsb++;
 
-       /* Find the closest power of two to the divisor */
-       evt_stream_div = arch_timer_rate / ARCH_TIMER_EVT_STREAM_FREQ;
-       pos = fls(evt_stream_div);
-       if (pos > 1 && !(evt_stream_div & (1 << (pos - 2))))
-               pos--;
        /* enable event stream */
-       arch_timer_evtstrm_enable(min(pos, 15));
+       arch_timer_evtstrm_enable(max(0, min(lsb, 15)));
 }
 
 static void arch_counter_set_user_access(void)
-- 
2.23.0

Reply via email to