[RFC PATCH 4/7] clocksource: arm_arch_timer: Export counter type, clocksource

Peter Hilber
2023-06-30T17:33:46Z

Export helper functions to allow other code to

- determine the counter type in use (virtual or physical, CP15 or memory),

- get a pointer to the arm_arch_timer clocksource, which can be compared
with the current clocksource.

The virtio_rtc driver will require the clocksource pointer when using
get_device_system_crosststamp(), and should communicate the actual Arm
counter type to the Virtio RTC device (cf. spec draft [1]).

[1] https://lists.oasis-open.org/archives/virtio-comment/202306/msg00592.html

Signed-off-by: Peter Hilber <peter.hilber@opensynergy.com>
---
drivers/clocksource/arm_arch_timer.c | 16 ++++++++++++++++
include/clocksource/arm_arch_timer.h | 19 +++++++++++++++++++
2 files changed, 35 insertions(+)

diff --git a/drivers/clocksource/arm_arch_timer.c b/drivers/clocksource/arm_arch_timer.c
index e733a2a1927a..cebdc1b2db4c 100644
--- a/drivers/clocksource/arm_arch_timer.c
+++ b/drivers/clocksource/arm_arch_timer.c
@@ -92,6 +92,7 @@ static enum vdso_clock_mode vdso_default = VDSO_CLOCKMODE_ARCHTIMER;
#else
static enum vdso_clock_mode vdso_default = VDSO_CLOCKMODE_NONE;
#endif /* CONFIG_GENERIC_GETTIMEOFDAY */
+static enum arch_timer_counter_type arch_counter_type __ro_after_init = ARCH_COUNTER_CP15_VIRT;

static cpumask_t evtstrm_available = CPU_MASK_NONE;
static bool evtstrm_enable __ro_after_init = IS_ENABLED(CONFIG_ARM_ARCH_TIMER_EVTSTREAM);
@@ -1109,6 +1110,7 @@ static void __init arch_counter_register(unsigned type)
rd = arch_counter_get_cntvct;
scr = arch_counter_get_cntvct;
}
+ arch_counter_type = ARCH_COUNTER_CP15_VIRT;
} else {
if (arch_timer_counter_has_wa()) {
rd = arch_counter_get_cntpct_stable;
@@ -1117,6 +1119,7 @@ static void __init arch_counter_register(unsigned type)
rd = arch_counter_get_cntpct;
scr = arch_counter_get_cntpct;
}
+ arch_counter_type = ARCH_COUNTER_CP15_PHYS;
}

arch_timer_read_counter = rd;
@@ -1124,6 +1127,7 @@ static void __init arch_counter_register(unsigned type)
} else {
arch_timer_read_counter = arch_counter_get_cntvct_mem;
scr = arch_counter_get_cntvct_mem;
+ arch_counter_type = ARCH_COUNTER_MEM_VIRT;
}

width = arch_counter_get_width();
@@ -1777,6 +1781,18 @@ static int __init arch_timer_acpi_init(struct acpi_table_header *table)
TIMER_ACPI_DECLARE(arch_timer, ACPI_SIG_GTDT, arch_timer_acpi_init);
#endif

+enum arch_timer_counter_type arch_timer_counter_get_type(void)
+{
+ return arch_counter_type;
+}
+EXPORT_SYMBOL_GPL(arch_timer_counter_get_type);
+
+struct clocksource *arch_timer_get_cs(void)
+{
+ return &clocksource_counter;
+}
+EXPORT_SYMBOL_GPL(arch_timer_get_cs);
+
int kvm_arch_ptp_get_crosststamp(u64 *cycle, struct timespec64 *ts,
struct clocksource **cs)
{
diff --git a/include/clocksource/arm_arch_timer.h b/include/clocksource/arm_arch_timer.h
index cbbc9a6dc571..b442db0b5ca0 100644
--- a/include/clocksource/arm_arch_timer.h
+++ b/include/clocksource/arm_arch_timer.h
@@ -43,6 +43,13 @@ enum arch_timer_spi_nr {
ARCH_TIMER_MAX_TIMER_SPI
};

+enum arch_timer_counter_type {
+ ARCH_COUNTER_CP15_VIRT,
+ ARCH_COUNTER_CP15_PHYS,
+ ARCH_COUNTER_MEM_VIRT,
+ ARCH_COUNTER_MEM_PHYS,
+};
+
#define ARCH_TIMER_PHYS_ACCESS 0
#define ARCH_TIMER_VIRT_ACCESS 1
#define ARCH_TIMER_MEM_PHYS_ACCESS 2
@@ -89,6 +96,8 @@ extern u32 arch_timer_get_rate(void);
extern u64 (*arch_timer_read_counter)(void);
extern struct arch_timer_kvm_info *arch_timer_get_kvm_info(void);
extern bool arch_timer_evtstrm_available(void);
+extern enum arch_timer_counter_type arch_timer_counter_get_type(void);
+extern struct clocksource *arch_timer_get_cs(void);

#else

@@ -107,6 +116,16 @@ static inline bool arch_timer_evtstrm_available(void)
return false;
}

+static inline enum arch_timer_counter_type arch_timer_counter_get_type(void)
+{
+ return ARCH_COUNTER_CP15_VIRT;
+}
+
+static inline struct clocksource *arch_timer_get_cs(void)
+{
+ return NULL;
+}
+
#endif

#endif
--
2.39.2