| 1 |
|
| 2 |
|
| 3 | |
| 4 | * Definitions for the clocksource provided by the Hyper-V
|
| 5 | * hypervisor to guest VMs, as described in the Hyper-V Top
|
| 6 | * Level Functional Spec (TLFS).
|
| 7 | *
|
| 8 | * Copyright (C) 2019, Microsoft, Inc.
|
| 9 | *
|
| 10 | * Author: Michael Kelley <[email protected]>
|
| 11 |
|
| 12 |
|
| 13 | #ifndef __CLKSOURCE_HYPERV_TIMER_H
|
| 14 | #define __CLKSOURCE_HYPERV_TIMER_H
|
| 15 |
|
| 16 | #include <linux/clocksource.h>
|
| 17 | #include <linux/math64.h>
|
| 18 | #include <hyperv/hvhdk.h>
|
| 19 |
|
| 20 | #define HV_MAX_MAX_DELTA_TICKS 0xffffffff
|
| 21 | #define HV_MIN_DELTA_TICKS 1
|
| 22 |
|
| 23 | #ifdef CONFIG_HYPERV_TIMER
|
| 24 |
|
| 25 | #include <asm/hyperv_timer.h>
|
| 26 |
|
| 27 |
|
| 28 | extern int hv_stimer_alloc(bool have_percpu_irqs);
|
| 29 | extern int hv_stimer_cleanup(unsigned int cpu);
|
| 30 | extern void hv_stimer_global_cleanup(void);
|
| 31 |
|
| 32 | extern void hv_init_clocksource(void);
|
| 33 | extern void hv_remap_tsc_clocksource(void);
|
| 34 |
|
| 35 | extern unsigned long hv_get_tsc_pfn(void);
|
| 36 | extern struct ms_hyperv_tsc_page *hv_get_tsc_page(void);
|
| 37 |
|
| 38 | extern void hv_adj_sched_clock_offset(u64 offset);
|
| 39 |
|
| 40 | static __always_inline bool
|
| 41 | hv_read_tsc_page_tsc(const struct ms_hyperv_tsc_page *tsc_pg,
|
| 42 | u64 *cur_tsc, u64 *time)
|
| 43 | {
|
| 44 | u64 scale, offset;
|
| 45 | u32 sequence;
|
| 46 |
|
| 47 | |
| 48 | * The protocol for reading Hyper-V TSC page is specified in Hypervisor
|
| 49 | * Top-Level Functional Specification ver. 3.0 and above. To get the
|
| 50 | * reference time we must do the following:
|
| 51 | * - READ ReferenceTscSequence
|
| 52 | * A special '0' value indicates the time source is unreliable and we
|
| 53 | * need to use something else. The currently published specification
|
| 54 | * versions (up to 4.0b) contain a mistake and wrongly claim '-1'
|
| 55 | * instead of '0' as the special value, see commit c35b82ef0294.
|
| 56 | * - ReferenceTime =
|
| 57 | * ((RDTSC() * ReferenceTscScale) >> 64) + ReferenceTscOffset
|
| 58 | * - READ ReferenceTscSequence again. In case its value has changed
|
| 59 | * since our first reading we need to discard ReferenceTime and repeat
|
| 60 | * the whole sequence as the hypervisor was updating the page in
|
| 61 | * between.
|
| 62 |
|
| 63 | do {
|
| 64 | sequence = READ_ONCE(tsc_pg->tsc_sequence);
|
| 65 | if (!sequence)
|
| 66 | return false;
|
| 67 | |
| 68 | * Make sure we read sequence before we read other values from
|
| 69 | * TSC page.
|
| 70 |
|
| 71 | smp_rmb();
|
| 72 |
|
| 73 | scale = READ_ONCE(tsc_pg->tsc_scale);
|
| 74 | offset = READ_ONCE(tsc_pg->tsc_offset);
|
| 75 | *cur_tsc = hv_get_raw_timer();
|
| 76 |
|
| 77 | |
| 78 | * Make sure we read sequence after we read all other values
|
| 79 | * from TSC page.
|
| 80 |
|
| 81 | smp_rmb();
|
| 82 |
|
| 83 | } while (READ_ONCE(tsc_pg->tsc_sequence) != sequence);
|
| 84 |
|
| 85 | *time = mul_u64_u64_shr(*cur_tsc, scale, 64) + offset;
|
| 86 | return true;
|
| 87 | }
|
| 88 |
|
| 89 | #else
|
| 90 | static inline unsigned long hv_get_tsc_pfn(void)
|
| 91 | {
|
| 92 | return 0;
|
| 93 | }
|
| 94 |
|
| 95 | static inline struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
|
| 96 | {
|
| 97 | return NULL;
|
| 98 | }
|
| 99 |
|
| 100 | static __always_inline bool
|
| 101 | hv_read_tsc_page_tsc(const struct ms_hyperv_tsc_page *tsc_pg, u64 *cur_tsc, u64 *time)
|
| 102 | {
|
| 103 | return false;
|
| 104 | }
|
| 105 |
|
| 106 | static inline int hv_stimer_cleanup(unsigned int cpu) { return 0; }
|
| 107 | static inline void hv_stimer_global_cleanup(void) {}
|
| 108 |
|
| 109 | #endif
|
| 110 |
|
| 111 | #endif
|