Skip to content

Commit 0d651e4

Browse files
committed
clocksource: arch_timer: use virtual counters
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 appear to travel backwards. As the guest will use the virtual timers, a (potential) KVM host must use the physical timers (which can wake up the host even if they fire while a guest is executing), and hence a host must have CNTVOFF set to zero so as to have a consistent view of time between the physical timers and virtual counters. Signed-off-by: Mark Rutland <mark.rutland@arm.com> Acked-by: Catalin Marinas <catalin.marinas@arm.com> Acked-by: Marc Zyngier <marc.zyngier@arm.com> Acked-by: Santosh Shilimkar <santosh.shilimkar@ti.com> Cc: Rob Herring <rob.herring@calxeda.com>
1 parent f793c23 commit 0d651e4

File tree

4 files changed

+6
-38
lines changed

4 files changed

+6
-38
lines changed

arch/arm/include/asm/arch_timer.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,6 @@ static inline u32 arch_timer_get_cntfrq(void)
8080
return val;
8181
}
8282

83-
static inline u64 arch_counter_get_cntpct(void)
84-
{
85-
u64 cval;
86-
87-
isb();
88-
asm volatile("mrrc p15, 0, %Q0, %R0, c14" : "=r" (cval));
89-
return cval;
90-
}
91-
9283
static inline u64 arch_counter_get_cntvct(void)
9384
{
9485
u64 cval;

arch/arm64/include/asm/arch_timer.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,6 @@ static inline void __cpuinit arch_counter_set_user_access(void)
110110
asm volatile("msr cntkctl_el1, %0" : : "r" (cntkctl));
111111
}
112112

113-
static inline u64 arch_counter_get_cntpct(void)
114-
{
115-
u64 cval;
116-
117-
isb();
118-
asm volatile("mrs %0, cntpct_el0" : "=r" (cval));
119-
120-
return cval;
121-
}
122-
123113
static inline u64 arch_counter_get_cntvct(void)
124114
{
125115
u64 cval;

drivers/clocksource/arm_arch_timer.c

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -186,27 +186,19 @@ u32 arch_timer_get_rate(void)
186186
return arch_timer_rate;
187187
}
188188

189-
/*
190-
* Some external users of arch_timer_read_counter (e.g. sched_clock) may try to
191-
* call it before it has been initialised. Rather than incur a performance
192-
* penalty checking for initialisation, provide a default implementation that
193-
* won't lead to time appearing to jump backwards.
194-
*/
195-
static u64 arch_timer_read_zero(void)
189+
u64 arch_timer_read_counter(void)
196190
{
197-
return 0;
191+
return arch_counter_get_cntvct();
198192
}
199193

200-
u64 (*arch_timer_read_counter)(void) = arch_timer_read_zero;
201-
202194
static cycle_t arch_counter_read(struct clocksource *cs)
203195
{
204-
return arch_timer_read_counter();
196+
return arch_counter_get_cntvct();
205197
}
206198

207199
static cycle_t arch_counter_read_cc(const struct cyclecounter *cc)
208200
{
209-
return arch_timer_read_counter();
201+
return arch_counter_get_cntvct();
210202
}
211203

212204
static struct clocksource clocksource_counter = {
@@ -287,7 +279,7 @@ static int __init arch_timer_register(void)
287279
cyclecounter.mult = clocksource_counter.mult;
288280
cyclecounter.shift = clocksource_counter.shift;
289281
timecounter_init(&timecounter, &cyclecounter,
290-
arch_counter_get_cntpct());
282+
arch_counter_get_cntvct());
291283

292284
if (arch_timer_use_virtual) {
293285
ppi = arch_timer_ppi[VIRT_PPI];
@@ -376,11 +368,6 @@ static void __init arch_timer_init(struct device_node *np)
376368
}
377369
}
378370

379-
if (arch_timer_use_virtual)
380-
arch_timer_read_counter = arch_counter_get_cntvct;
381-
else
382-
arch_timer_read_counter = arch_counter_get_cntpct;
383-
384371
arch_timer_register();
385372
arch_timer_arch_init();
386373
}

include/clocksource/arm_arch_timer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#ifdef CONFIG_ARM_ARCH_TIMER
3333

3434
extern u32 arch_timer_get_rate(void);
35-
extern u64 (*arch_timer_read_counter)(void);
35+
extern u64 arch_timer_read_counter(void);
3636
extern struct timecounter *arch_timer_get_timecounter(void);
3737

3838
#else

0 commit comments

Comments
 (0)