Skip to content

Commit 4ad1aa5

Browse files
anirudhrbliuw
authored andcommitted
clocksource/drivers/hyperv: add data structure for reference TSC MSR
Add a data structure to represent the reference TSC MSR similar to other MSRs. This simplifies the code for updating the MSR. Signed-off-by: Anirudh Rayabharam <anrayabh@linux.microsoft.com> Reviewed-by: Michael Kelley <mikelley@microsoft.com> Link: https://lore.kernel.org/r/20221027095729.1676394-2-anrayabh@linux.microsoft.com Signed-off-by: Wei Liu <wei.liu@kernel.org>
1 parent 99632e3 commit 4ad1aa5

2 files changed

Lines changed: 24 additions & 14 deletions

File tree

drivers/clocksource/hyperv_timer.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <linux/interrupt.h>
2222
#include <linux/irq.h>
2323
#include <linux/acpi.h>
24+
#include <linux/hyperv.h>
2425
#include <clocksource/hyperv_timer.h>
2526
#include <asm/hyperv-tlfs.h>
2627
#include <asm/mshyperv.h>
@@ -395,25 +396,25 @@ static u64 notrace read_hv_sched_clock_tsc(void)
395396

396397
static void suspend_hv_clock_tsc(struct clocksource *arg)
397398
{
398-
u64 tsc_msr;
399+
union hv_reference_tsc_msr tsc_msr;
399400

400401
/* Disable the TSC page */
401-
tsc_msr = hv_get_register(HV_REGISTER_REFERENCE_TSC);
402-
tsc_msr &= ~BIT_ULL(0);
403-
hv_set_register(HV_REGISTER_REFERENCE_TSC, tsc_msr);
402+
tsc_msr.as_uint64 = hv_get_register(HV_REGISTER_REFERENCE_TSC);
403+
tsc_msr.enable = 0;
404+
hv_set_register(HV_REGISTER_REFERENCE_TSC, tsc_msr.as_uint64);
404405
}
405406

406407

407408
static void resume_hv_clock_tsc(struct clocksource *arg)
408409
{
409410
phys_addr_t phys_addr = virt_to_phys(&tsc_pg);
410-
u64 tsc_msr;
411+
union hv_reference_tsc_msr tsc_msr;
411412

412413
/* Re-enable the TSC page */
413-
tsc_msr = hv_get_register(HV_REGISTER_REFERENCE_TSC);
414-
tsc_msr &= GENMASK_ULL(11, 0);
415-
tsc_msr |= BIT_ULL(0) | (u64)phys_addr;
416-
hv_set_register(HV_REGISTER_REFERENCE_TSC, tsc_msr);
414+
tsc_msr.as_uint64 = hv_get_register(HV_REGISTER_REFERENCE_TSC);
415+
tsc_msr.enable = 1;
416+
tsc_msr.pfn = HVPFN_DOWN(phys_addr);
417+
hv_set_register(HV_REGISTER_REFERENCE_TSC, tsc_msr.as_uint64);
417418
}
418419

419420
#ifdef HAVE_VDSO_CLOCKMODE_HVCLOCK
@@ -495,7 +496,7 @@ static __always_inline void hv_setup_sched_clock(void *sched_clock) {}
495496

496497
static bool __init hv_init_tsc_clocksource(void)
497498
{
498-
u64 tsc_msr;
499+
union hv_reference_tsc_msr tsc_msr;
499500
phys_addr_t phys_addr;
500501

501502
if (!(ms_hyperv.features & HV_MSR_REFERENCE_TSC_AVAILABLE))
@@ -530,10 +531,10 @@ static bool __init hv_init_tsc_clocksource(void)
530531
* (which already has at least the low 12 bits set to zero since
531532
* it is page aligned). Also set the "enable" bit, which is bit 0.
532533
*/
533-
tsc_msr = hv_get_register(HV_REGISTER_REFERENCE_TSC);
534-
tsc_msr &= GENMASK_ULL(11, 0);
535-
tsc_msr = tsc_msr | 0x1 | (u64)phys_addr;
536-
hv_set_register(HV_REGISTER_REFERENCE_TSC, tsc_msr);
534+
tsc_msr.as_uint64 = hv_get_register(HV_REGISTER_REFERENCE_TSC);
535+
tsc_msr.enable = 1;
536+
tsc_msr.pfn = HVPFN_DOWN(phys_addr);
537+
hv_set_register(HV_REGISTER_REFERENCE_TSC, tsc_msr.as_uint64);
537538

538539
clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
539540

include/asm-generic/hyperv-tlfs.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ struct ms_hyperv_tsc_page {
102102
volatile s64 tsc_offset;
103103
} __packed;
104104

105+
union hv_reference_tsc_msr {
106+
u64 as_uint64;
107+
struct {
108+
u64 enable:1;
109+
u64 reserved:11;
110+
u64 pfn:52;
111+
} __packed;
112+
};
113+
105114
/*
106115
* The guest OS needs to register the guest ID with the hypervisor.
107116
* The guest ID is a 64 bit entity and the structure of this ID is

0 commit comments

Comments
 (0)