Skip to content

Commit c1153d5

Browse files
ouptondlezcano
authored andcommitted
clocksource/drivers/arm_arch_timer: Fix masking for high freq counters
Unfortunately, the architecture provides no means to determine the bit width of the system counter. However, we do know the following from the specification: - the system counter is at least 56 bits wide - Roll-over time of not less than 40 years To date, the arch timer driver has depended on the first property, assuming any system counter to be 56 bits wide and masking off the rest. However, combining a narrow clocksource mask with a high frequency counter could result in prematurely wrapping the system counter by a significant margin. For example, a 56 bit wide, 1GHz system counter would wrap in a mere 2.28 years! This is a problem for two reasons: v8.6+ implementations are required to provide a 64 bit, 1GHz system counter. Furthermore, before v8.6, implementers may select a counter frequency of their choosing. Fix the issue by deriving a valid clock mask based on the second property from above. Set the floor at 56 bits, since we know no system counter is narrower than that. [maz: fixed width computation not to lose the last bit, added max delta generation for the timer] Suggested-by: Marc Zyngier <[email protected]> Signed-off-by: Oliver Upton <[email protected]> Reviewed-by: Linus Walleij <[email protected]> Signed-off-by: Marc Zyngier <[email protected]> Link: https://lore.kernel.org/r/[email protected] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Daniel Lezcano <[email protected]>
1 parent ec8f7f3 commit c1153d5

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

drivers/clocksource/arm_arch_timer.c

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@
5252
#define CNTV_CVAL_LO 0x30
5353
#define CNTV_CTL 0x3c
5454

55+
/*
56+
* The minimum amount of time a generic counter is guaranteed to not roll over
57+
* (40 years)
58+
*/
59+
#define MIN_ROLLOVER_SECS (40ULL * 365 * 24 * 3600)
60+
5561
static unsigned arch_timers_present __initdata;
5662

5763
struct arch_timer {
@@ -95,6 +101,22 @@ static int __init early_evtstrm_cfg(char *buf)
95101
}
96102
early_param("clocksource.arm_arch_timer.evtstrm", early_evtstrm_cfg);
97103

104+
/*
105+
* Makes an educated guess at a valid counter width based on the Generic Timer
106+
* specification. Of note:
107+
* 1) the system counter is at least 56 bits wide
108+
* 2) a roll-over time of not less than 40 years
109+
*
110+
* See 'ARM DDI 0487G.a D11.1.2 ("The system counter")' for more details.
111+
*/
112+
static int arch_counter_get_width(void)
113+
{
114+
u64 min_cycles = MIN_ROLLOVER_SECS * arch_timer_rate;
115+
116+
/* guarantee the returned width is within the valid range */
117+
return clamp_val(ilog2(min_cycles - 1) + 1, 56, 64);
118+
}
119+
98120
/*
99121
* Architected system timer support.
100122
*/
@@ -212,13 +234,11 @@ static struct clocksource clocksource_counter = {
212234
.id = CSID_ARM_ARCH_COUNTER,
213235
.rating = 400,
214236
.read = arch_counter_read,
215-
.mask = CLOCKSOURCE_MASK(56),
216237
.flags = CLOCK_SOURCE_IS_CONTINUOUS,
217238
};
218239

219240
static struct cyclecounter cyclecounter __ro_after_init = {
220241
.read = arch_counter_read_cc,
221-
.mask = CLOCKSOURCE_MASK(56),
222242
};
223243

224244
struct ate_acpi_oem_info {
@@ -790,7 +810,7 @@ static u64 __arch_timer_check_delta(void)
790810
return CLOCKSOURCE_MASK(32);
791811
}
792812
#endif
793-
return CLOCKSOURCE_MASK(56);
813+
return CLOCKSOURCE_MASK(arch_counter_get_width());
794814
}
795815

796816
static void __arch_timer_setup(unsigned type,
@@ -1035,6 +1055,7 @@ struct arch_timer_kvm_info *arch_timer_get_kvm_info(void)
10351055
static void __init arch_counter_register(unsigned type)
10361056
{
10371057
u64 start_count;
1058+
int width;
10381059

10391060
/* Register the CP15 based counter if we have one */
10401061
if (type & ARCH_TIMER_TYPE_CP15) {
@@ -1059,6 +1080,10 @@ static void __init arch_counter_register(unsigned type)
10591080
arch_timer_read_counter = arch_counter_get_cntvct_mem;
10601081
}
10611082

1083+
width = arch_counter_get_width();
1084+
clocksource_counter.mask = CLOCKSOURCE_MASK(width);
1085+
cyclecounter.mask = CLOCKSOURCE_MASK(width);
1086+
10621087
if (!arch_counter_suspend_stop)
10631088
clocksource_counter.flags |= CLOCK_SOURCE_SUSPEND_NONSTOP;
10641089
start_count = arch_timer_read_counter();
@@ -1068,8 +1093,7 @@ static void __init arch_counter_register(unsigned type)
10681093
timecounter_init(&arch_timer_kvm_info.timecounter,
10691094
&cyclecounter, start_count);
10701095

1071-
/* 56 bits minimum, so we assume worst case rollover */
1072-
sched_clock_register(arch_timer_read_counter, 56, arch_timer_rate);
1096+
sched_clock_register(arch_timer_read_counter, width, arch_timer_rate);
10731097
}
10741098

10751099
static void arch_timer_stop(struct clock_event_device *clk)

0 commit comments

Comments
 (0)