Skip to content

Commit dd2cb34

Browse files
kelleymhKAGA-KOKO
authored andcommitted
clocksource/drivers: Continue making Hyper-V clocksource ISA agnostic
Continue consolidating Hyper-V clock and timer code into an ISA independent Hyper-V clocksource driver. Move the existing clocksource code under drivers/hv and arch/x86 to the new clocksource driver while separating out the ISA dependencies. Update Hyper-V initialization to call initialization and cleanup routines since the Hyper-V synthetic clock is not independently enumerated in ACPI. Update Hyper-V clocksource users in KVM and VDSO to get definitions from the new include file. No behavior is changed and no new functionality is added. Suggested-by: Marc Zyngier <[email protected]> Signed-off-by: Michael Kelley <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Vitaly Kuznetsov <[email protected]> Cc: "[email protected]" <[email protected]> Cc: "[email protected]" <[email protected]> Cc: "[email protected]" <[email protected]> Cc: "[email protected]" <[email protected]> Cc: "[email protected]" <[email protected]> Cc: "[email protected]" <[email protected]> Cc: "[email protected]" <[email protected]> Cc: "[email protected]" <[email protected]> Cc: "[email protected]" <[email protected]> Cc: "[email protected]" <[email protected]> Cc: "[email protected]" <[email protected]> Cc: Sunil Muthuswamy <[email protected]> Cc: KY Srinivasan <[email protected]> Cc: "[email protected]" <[email protected]> Cc: "[email protected]" <[email protected]> Cc: "[email protected]" <[email protected]> Cc: "[email protected]" <[email protected]> Cc: "[email protected]" <[email protected]> Cc: "[email protected]" <[email protected]> Cc: "[email protected]" <[email protected]> Cc: "[email protected]" <[email protected]> Cc: "[email protected]" <[email protected]> Cc: "[email protected]" <[email protected]> Cc: "[email protected]" <[email protected]> Cc: "[email protected]" <[email protected]> Cc: "[email protected]" <[email protected]> Cc: "[email protected]" <[email protected]> Cc: "[email protected]" <[email protected]> Cc: "[email protected]" <[email protected]> Cc: "[email protected]" <[email protected]> Cc: "[email protected]" <[email protected]> Cc: "[email protected]" <[email protected]> Cc: "[email protected]" <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent fd1fea6 commit dd2cb34

File tree

8 files changed

+237
-160
lines changed

8 files changed

+237
-160
lines changed

arch/x86/entry/vdso/vma.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include <asm/page.h>
2323
#include <asm/desc.h>
2424
#include <asm/cpufeature.h>
25-
#include <asm/mshyperv.h>
25+
#include <clocksource/hyperv_timer.h>
2626

2727
#if defined(CONFIG_X86_64)
2828
unsigned int __read_mostly vdso64_enabled = 1;

arch/x86/hyperv/hv_init.c

Lines changed: 3 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -17,64 +17,13 @@
1717
#include <linux/version.h>
1818
#include <linux/vmalloc.h>
1919
#include <linux/mm.h>
20-
#include <linux/clockchips.h>
2120
#include <linux/hyperv.h>
2221
#include <linux/slab.h>
2322
#include <linux/cpuhotplug.h>
24-
25-
#ifdef CONFIG_HYPERV_TSCPAGE
26-
27-
static struct ms_hyperv_tsc_page *tsc_pg;
28-
29-
struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
30-
{
31-
return tsc_pg;
32-
}
33-
EXPORT_SYMBOL_GPL(hv_get_tsc_page);
34-
35-
static u64 read_hv_clock_tsc(struct clocksource *arg)
36-
{
37-
u64 current_tick = hv_read_tsc_page(tsc_pg);
38-
39-
if (current_tick == U64_MAX)
40-
rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
41-
42-
return current_tick;
43-
}
44-
45-
static struct clocksource hyperv_cs_tsc = {
46-
.name = "hyperv_clocksource_tsc_page",
47-
.rating = 400,
48-
.read = read_hv_clock_tsc,
49-
.mask = CLOCKSOURCE_MASK(64),
50-
.flags = CLOCK_SOURCE_IS_CONTINUOUS,
51-
};
52-
#endif
53-
54-
static u64 read_hv_clock_msr(struct clocksource *arg)
55-
{
56-
u64 current_tick;
57-
/*
58-
* Read the partition counter to get the current tick count. This count
59-
* is set to 0 when the partition is created and is incremented in
60-
* 100 nanosecond units.
61-
*/
62-
rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
63-
return current_tick;
64-
}
65-
66-
static struct clocksource hyperv_cs_msr = {
67-
.name = "hyperv_clocksource_msr",
68-
.rating = 400,
69-
.read = read_hv_clock_msr,
70-
.mask = CLOCKSOURCE_MASK(64),
71-
.flags = CLOCK_SOURCE_IS_CONTINUOUS,
72-
};
23+
#include <clocksource/hyperv_timer.h>
7324

7425
void *hv_hypercall_pg;
7526
EXPORT_SYMBOL_GPL(hv_hypercall_pg);
76-
struct clocksource *hyperv_cs;
77-
EXPORT_SYMBOL_GPL(hyperv_cs);
7827

7928
u32 *hv_vp_index;
8029
EXPORT_SYMBOL_GPL(hv_vp_index);
@@ -343,42 +292,8 @@ void __init hyperv_init(void)
343292

344293
x86_init.pci.arch_init = hv_pci_init;
345294

346-
/*
347-
* Register Hyper-V specific clocksource.
348-
*/
349-
#ifdef CONFIG_HYPERV_TSCPAGE
350-
if (ms_hyperv.features & HV_MSR_REFERENCE_TSC_AVAILABLE) {
351-
union hv_x64_msr_hypercall_contents tsc_msr;
352-
353-
tsc_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
354-
if (!tsc_pg)
355-
goto register_msr_cs;
356-
357-
hyperv_cs = &hyperv_cs_tsc;
358-
359-
rdmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
360-
361-
tsc_msr.enable = 1;
362-
tsc_msr.guest_physical_address = vmalloc_to_pfn(tsc_pg);
363-
364-
wrmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
365-
366-
hyperv_cs_tsc.archdata.vclock_mode = VCLOCK_HVCLOCK;
367-
368-
clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
369-
return;
370-
}
371-
register_msr_cs:
372-
#endif
373-
/*
374-
* For 32 bit guests just use the MSR based mechanism for reading
375-
* the partition counter.
376-
*/
377-
378-
hyperv_cs = &hyperv_cs_msr;
379-
if (ms_hyperv.features & HV_MSR_TIME_REF_COUNT_AVAILABLE)
380-
clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100);
381-
295+
/* Register Hyper-V specific clocksource */
296+
hv_init_clocksource();
382297
return;
383298

384299
remove_cpuhp_state:

arch/x86/include/asm/mshyperv.h

Lines changed: 11 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ static inline void vmbus_signal_eom(struct hv_message *msg, u32 old_msg_type)
105105
#define hv_get_crash_ctl(val) \
106106
rdmsrl(HV_X64_MSR_CRASH_CTL, val)
107107

108+
#define hv_get_time_ref_count(val) \
109+
rdmsrl(HV_X64_MSR_TIME_REF_COUNT, val)
110+
111+
#define hv_get_reference_tsc(val) \
112+
rdmsrl(HV_X64_MSR_REFERENCE_TSC, val)
113+
#define hv_set_reference_tsc(val) \
114+
wrmsrl(HV_X64_MSR_REFERENCE_TSC, val)
115+
#define hv_set_clocksource_vdso(val) \
116+
((val).archdata.vclock_mode = VCLOCK_HVCLOCK)
117+
#define hv_get_raw_timer() rdtsc_ordered()
118+
108119
void hyperv_callback_vector(void);
109120
void hyperv_reenlightenment_vector(void);
110121
#ifdef CONFIG_TRACING
@@ -133,7 +144,6 @@ static inline void hv_disable_stimer0_percpu_irq(int irq) {}
133144

134145

135146
#if IS_ENABLED(CONFIG_HYPERV)
136-
extern struct clocksource *hyperv_cs;
137147
extern void *hv_hypercall_pg;
138148
extern void __percpu **hyperv_pcpu_input_arg;
139149

@@ -387,73 +397,4 @@ static inline int hyperv_flush_guest_mapping_range(u64 as,
387397
}
388398
#endif /* CONFIG_HYPERV */
389399

390-
#ifdef CONFIG_HYPERV_TSCPAGE
391-
struct ms_hyperv_tsc_page *hv_get_tsc_page(void);
392-
static inline u64 hv_read_tsc_page_tsc(const struct ms_hyperv_tsc_page *tsc_pg,
393-
u64 *cur_tsc)
394-
{
395-
u64 scale, offset;
396-
u32 sequence;
397-
398-
/*
399-
* The protocol for reading Hyper-V TSC page is specified in Hypervisor
400-
* Top-Level Functional Specification ver. 3.0 and above. To get the
401-
* reference time we must do the following:
402-
* - READ ReferenceTscSequence
403-
* A special '0' value indicates the time source is unreliable and we
404-
* need to use something else. The currently published specification
405-
* versions (up to 4.0b) contain a mistake and wrongly claim '-1'
406-
* instead of '0' as the special value, see commit c35b82ef0294.
407-
* - ReferenceTime =
408-
* ((RDTSC() * ReferenceTscScale) >> 64) + ReferenceTscOffset
409-
* - READ ReferenceTscSequence again. In case its value has changed
410-
* since our first reading we need to discard ReferenceTime and repeat
411-
* the whole sequence as the hypervisor was updating the page in
412-
* between.
413-
*/
414-
do {
415-
sequence = READ_ONCE(tsc_pg->tsc_sequence);
416-
if (!sequence)
417-
return U64_MAX;
418-
/*
419-
* Make sure we read sequence before we read other values from
420-
* TSC page.
421-
*/
422-
smp_rmb();
423-
424-
scale = READ_ONCE(tsc_pg->tsc_scale);
425-
offset = READ_ONCE(tsc_pg->tsc_offset);
426-
*cur_tsc = rdtsc_ordered();
427-
428-
/*
429-
* Make sure we read sequence after we read all other values
430-
* from TSC page.
431-
*/
432-
smp_rmb();
433-
434-
} while (READ_ONCE(tsc_pg->tsc_sequence) != sequence);
435-
436-
return mul_u64_u64_shr(*cur_tsc, scale, 64) + offset;
437-
}
438-
439-
static inline u64 hv_read_tsc_page(const struct ms_hyperv_tsc_page *tsc_pg)
440-
{
441-
u64 cur_tsc;
442-
443-
return hv_read_tsc_page_tsc(tsc_pg, &cur_tsc);
444-
}
445-
446-
#else
447-
static inline struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
448-
{
449-
return NULL;
450-
}
451-
452-
static inline u64 hv_read_tsc_page_tsc(const struct ms_hyperv_tsc_page *tsc_pg,
453-
u64 *cur_tsc)
454-
{
455-
BUG();
456-
return U64_MAX;
457-
}
458-
#endif
459400
#endif

arch/x86/include/asm/vdso/gettimeofday.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include <asm/unistd.h>
1919
#include <asm/msr.h>
2020
#include <asm/pvclock.h>
21-
#include <asm/mshyperv.h>
21+
#include <clocksource/hyperv_timer.h>
2222

2323
#define __vdso_data (VVAR(_vdso_data))
2424

arch/x86/kvm/x86.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
#include <asm/mshyperv.h>
6868
#include <asm/hypervisor.h>
6969
#include <asm/intel_pt.h>
70+
#include <clocksource/hyperv_timer.h>
7071

7172
#define CREATE_TRACE_POINTS
7273
#include "trace.h"

drivers/clocksource/hyperv_timer.c

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include <linux/percpu.h>
1515
#include <linux/cpumask.h>
1616
#include <linux/clockchips.h>
17+
#include <linux/clocksource.h>
18+
#include <linux/sched_clock.h>
1719
#include <linux/mm.h>
1820
#include <clocksource/hyperv_timer.h>
1921
#include <asm/hyperv-tlfs.h>
@@ -198,3 +200,140 @@ void hv_stimer_global_cleanup(void)
198200
hv_stimer_free();
199201
}
200202
EXPORT_SYMBOL_GPL(hv_stimer_global_cleanup);
203+
204+
/*
205+
* Code and definitions for the Hyper-V clocksources. Two
206+
* clocksources are defined: one that reads the Hyper-V defined MSR, and
207+
* the other that uses the TSC reference page feature as defined in the
208+
* TLFS. The MSR version is for compatibility with old versions of
209+
* Hyper-V and 32-bit x86. The TSC reference page version is preferred.
210+
*/
211+
212+
struct clocksource *hyperv_cs;
213+
EXPORT_SYMBOL_GPL(hyperv_cs);
214+
215+
#ifdef CONFIG_HYPERV_TSCPAGE
216+
217+
static struct ms_hyperv_tsc_page *tsc_pg;
218+
219+
struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
220+
{
221+
return tsc_pg;
222+
}
223+
EXPORT_SYMBOL_GPL(hv_get_tsc_page);
224+
225+
static u64 notrace read_hv_sched_clock_tsc(void)
226+
{
227+
u64 current_tick = hv_read_tsc_page(tsc_pg);
228+
229+
if (current_tick == U64_MAX)
230+
hv_get_time_ref_count(current_tick);
231+
232+
return current_tick;
233+
}
234+
235+
static u64 read_hv_clock_tsc(struct clocksource *arg)
236+
{
237+
return read_hv_sched_clock_tsc();
238+
}
239+
240+
static struct clocksource hyperv_cs_tsc = {
241+
.name = "hyperv_clocksource_tsc_page",
242+
.rating = 400,
243+
.read = read_hv_clock_tsc,
244+
.mask = CLOCKSOURCE_MASK(64),
245+
.flags = CLOCK_SOURCE_IS_CONTINUOUS,
246+
};
247+
#endif
248+
249+
static u64 notrace read_hv_sched_clock_msr(void)
250+
{
251+
u64 current_tick;
252+
/*
253+
* Read the partition counter to get the current tick count. This count
254+
* is set to 0 when the partition is created and is incremented in
255+
* 100 nanosecond units.
256+
*/
257+
hv_get_time_ref_count(current_tick);
258+
return current_tick;
259+
}
260+
261+
static u64 read_hv_clock_msr(struct clocksource *arg)
262+
{
263+
return read_hv_sched_clock_msr();
264+
}
265+
266+
static struct clocksource hyperv_cs_msr = {
267+
.name = "hyperv_clocksource_msr",
268+
.rating = 400,
269+
.read = read_hv_clock_msr,
270+
.mask = CLOCKSOURCE_MASK(64),
271+
.flags = CLOCK_SOURCE_IS_CONTINUOUS,
272+
};
273+
274+
#ifdef CONFIG_HYPERV_TSCPAGE
275+
static bool __init hv_init_tsc_clocksource(void)
276+
{
277+
u64 tsc_msr;
278+
phys_addr_t phys_addr;
279+
280+
if (!(ms_hyperv.features & HV_MSR_REFERENCE_TSC_AVAILABLE))
281+
return false;
282+
283+
tsc_pg = vmalloc(PAGE_SIZE);
284+
if (!tsc_pg)
285+
return false;
286+
287+
hyperv_cs = &hyperv_cs_tsc;
288+
phys_addr = page_to_phys(vmalloc_to_page(tsc_pg));
289+
290+
/*
291+
* The Hyper-V TLFS specifies to preserve the value of reserved
292+
* bits in registers. So read the existing value, preserve the
293+
* low order 12 bits, and add in the guest physical address
294+
* (which already has at least the low 12 bits set to zero since
295+
* it is page aligned). Also set the "enable" bit, which is bit 0.
296+
*/
297+
hv_get_reference_tsc(tsc_msr);
298+
tsc_msr &= GENMASK_ULL(11, 0);
299+
tsc_msr = tsc_msr | 0x1 | (u64)phys_addr;
300+
hv_set_reference_tsc(tsc_msr);
301+
302+
hv_set_clocksource_vdso(hyperv_cs_tsc);
303+
clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
304+
305+
/* sched_clock_register is needed on ARM64 but is a no-op on x86 */
306+
sched_clock_register(read_hv_sched_clock_tsc, 64, HV_CLOCK_HZ);
307+
return true;
308+
}
309+
#else
310+
static bool __init hv_init_tsc_clocksource(void)
311+
{
312+
return false;
313+
}
314+
#endif
315+
316+
317+
void __init hv_init_clocksource(void)
318+
{
319+
/*
320+
* Try to set up the TSC page clocksource. If it succeeds, we're
321+
* done. Otherwise, set up the MSR clocksoruce. At least one of
322+
* these will always be available except on very old versions of
323+
* Hyper-V on x86. In that case we won't have a Hyper-V
324+
* clocksource, but Linux will still run with a clocksource based
325+
* on the emulated PIT or LAPIC timer.
326+
*/
327+
if (hv_init_tsc_clocksource())
328+
return;
329+
330+
if (!(ms_hyperv.features & HV_MSR_TIME_REF_COUNT_AVAILABLE))
331+
return;
332+
333+
hyperv_cs = &hyperv_cs_msr;
334+
clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100);
335+
336+
/* sched_clock_register is needed on ARM64 but is a no-op on x86 */
337+
sched_clock_register(read_hv_sched_clock_msr, 64, HV_CLOCK_HZ);
338+
}
339+
EXPORT_SYMBOL_GPL(hv_init_clocksource);

drivers/hv/hv_util.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <linux/hyperv.h>
1818
#include <linux/clockchips.h>
1919
#include <linux/ptp_clock_kernel.h>
20+
#include <clocksource/hyperv_timer.h>
2021
#include <asm/mshyperv.h>
2122

2223
#include "hyperv_vmbus.h"

0 commit comments

Comments
 (0)