Skip to content

Commit e6d68b0

Browse files
Christoffer Dallchazy
authored andcommitted
arm64: Use physical counter for in-kernel reads when booted in EL2
Using the physical counter allows KVM to retain the offset between the virtual and physical counter as long as it is actively running a VCPU. As soon as a VCPU is released, another thread is scheduled or we start running userspace applications, we reset the offset to 0, so that userspace accessing the virtual timer can still read the virtual counter and get the same view of time as the kernel. This opens up potential improvements for KVM performance, but we have to make a few adjustments to preserve system consistency. Currently get_cycles() is hardwired to arch_counter_get_cntvct() on arm64, but as we move to using the physical timer for the in-kernel time-keeping on systems that boot in EL2, we should use the same counter for get_cycles() as for other in-kernel timekeeping operations. Similarly, implementations of arch_timer_set_next_event_phys() is modified to use the counter specific to the timer being programmed. VHE kernels or kernels continuing to use the virtual timer are unaffected. Cc: Will Deacon <will.deacon@arm.com> Cc: Mark Rutland <mark.rutland@arm.com> Acked-by: Catalin Marinas <catalin.marinas@arm.com> Acked-by: Marc Zyngier <marc.zyngier@arm.com> Signed-off-by: Christoffer Dall <cdall@linaro.org>
1 parent f2e600c commit e6d68b0

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

arch/arm64/include/asm/timex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* Use the current timer as a cycle counter since this is what we use for
2323
* the delay loop.
2424
*/
25-
#define get_cycles() arch_counter_get_cntvct()
25+
#define get_cycles() arch_timer_read_counter()
2626

2727
#include <asm-generic/timex.h>
2828

drivers/clocksource/arm_arch_timer.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ u32 arch_timer_reg_read(int access, enum arch_timer_reg reg,
158158
* if we don't have the cp15 accessors we won't have a problem.
159159
*/
160160
u64 (*arch_timer_read_counter)(void) = arch_counter_get_cntvct;
161+
EXPORT_SYMBOL_GPL(arch_timer_read_counter);
161162

162163
static u64 arch_counter_read(struct clocksource *cs)
163164
{
@@ -329,16 +330,19 @@ static void erratum_set_next_event_tval_generic(const int access, unsigned long
329330
struct clock_event_device *clk)
330331
{
331332
unsigned long ctrl;
332-
u64 cval = evt + arch_counter_get_cntvct();
333+
u64 cval;
333334

334335
ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
335336
ctrl |= ARCH_TIMER_CTRL_ENABLE;
336337
ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
337338

338-
if (access == ARCH_TIMER_PHYS_ACCESS)
339+
if (access == ARCH_TIMER_PHYS_ACCESS) {
340+
cval = evt + arch_counter_get_cntpct();
339341
write_sysreg(cval, cntp_cval_el0);
340-
else
342+
} else {
343+
cval = evt + arch_counter_get_cntvct();
341344
write_sysreg(cval, cntv_cval_el0);
345+
}
342346

343347
arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
344348
}
@@ -913,7 +917,7 @@ static void __init arch_counter_register(unsigned type)
913917

914918
/* Register the CP15 based counter if we have one */
915919
if (type & ARCH_TIMER_TYPE_CP15) {
916-
if (IS_ENABLED(CONFIG_ARM64) ||
920+
if ((IS_ENABLED(CONFIG_ARM64) && !is_hyp_mode_available()) ||
917921
arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI)
918922
arch_timer_read_counter = arch_counter_get_cntvct;
919923
else

0 commit comments

Comments
 (0)