hyperv_timer.h 3.3 KB

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