hyperv_timer.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Clocksource driver for the synthetic counter and timers
  4. * provided by the Hyper-V hypervisor to guest VMs, as described
  5. * in the Hyper-V Top Level Functional Spec (TLFS). This driver
  6. * is instruction set architecture independent.
  7. *
  8. * Copyright (C) 2019, Microsoft, Inc.
  9. *
  10. * Author: Michael Kelley <mikelley@microsoft.com>
  11. */
  12. #include <linux/percpu.h>
  13. #include <linux/cpumask.h>
  14. #include <linux/clockchips.h>
  15. #include <linux/clocksource.h>
  16. #include <linux/sched_clock.h>
  17. #include <linux/mm.h>
  18. #include <linux/cpuhotplug.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/irq.h>
  21. #include <linux/acpi.h>
  22. #include <linux/hyperv.h>
  23. #include <clocksource/hyperv_timer.h>
  24. #include <asm/hyperv-tlfs.h>
  25. #include <asm/mshyperv.h>
  26. static struct clock_event_device __percpu *hv_clock_event;
  27. /* Note: offset can hold negative values after hibernation. */
  28. static u64 hv_sched_clock_offset __read_mostly;
  29. /*
  30. * If false, we're using the old mechanism for stimer0 interrupts
  31. * where it sends a VMbus message when it expires. The old
  32. * mechanism is used when running on older versions of Hyper-V
  33. * that don't support Direct Mode. While Hyper-V provides
  34. * four stimer's per CPU, Linux uses only stimer0.
  35. *
  36. * Because Direct Mode does not require processing a VMbus
  37. * message, stimer interrupts can be enabled earlier in the
  38. * process of booting a CPU, and consistent with when timer
  39. * interrupts are enabled for other clocksource drivers.
  40. * However, for legacy versions of Hyper-V when Direct Mode
  41. * is not enabled, setting up stimer interrupts must be
  42. * delayed until VMbus is initialized and can process the
  43. * interrupt message.
  44. */
  45. static bool direct_mode_enabled;
  46. static int stimer0_irq = -1;
  47. static int stimer0_message_sint;
  48. static __maybe_unused DEFINE_PER_CPU(long, stimer0_evt);
  49. /*
  50. * Common code for stimer0 interrupts coming via Direct Mode or
  51. * as a VMbus message.
  52. */
  53. void hv_stimer0_isr(void)
  54. {
  55. struct clock_event_device *ce;
  56. ce = this_cpu_ptr(hv_clock_event);
  57. ce->event_handler(ce);
  58. }
  59. EXPORT_SYMBOL_GPL(hv_stimer0_isr);
  60. /*
  61. * stimer0 interrupt handler for architectures that support
  62. * per-cpu interrupts, which also implies Direct Mode.
  63. */
  64. static irqreturn_t __maybe_unused hv_stimer0_percpu_isr(int irq, void *dev_id)
  65. {
  66. hv_stimer0_isr();
  67. return IRQ_HANDLED;
  68. }
  69. static int hv_ce_set_next_event(unsigned long delta,
  70. struct clock_event_device *evt)
  71. {
  72. u64 current_tick;
  73. current_tick = hv_read_reference_counter();
  74. current_tick += delta;
  75. hv_set_msr(HV_MSR_STIMER0_COUNT, current_tick);
  76. return 0;
  77. }
  78. static int hv_ce_shutdown(struct clock_event_device *evt)
  79. {
  80. hv_set_msr(HV_MSR_STIMER0_COUNT, 0);
  81. hv_set_msr(HV_MSR_STIMER0_CONFIG, 0);
  82. if (direct_mode_enabled && stimer0_irq >= 0)
  83. disable_percpu_irq(stimer0_irq);
  84. return 0;
  85. }
  86. static int hv_ce_set_oneshot(struct clock_event_device *evt)
  87. {
  88. union hv_stimer_config timer_cfg;
  89. timer_cfg.as_uint64 = 0;
  90. timer_cfg.enable = 1;
  91. timer_cfg.auto_enable = 1;
  92. if (direct_mode_enabled) {
  93. /*
  94. * When it expires, the timer will directly interrupt
  95. * on the specified hardware vector/IRQ.
  96. */
  97. timer_cfg.direct_mode = 1;
  98. timer_cfg.apic_vector = HYPERV_STIMER0_VECTOR;
  99. if (stimer0_irq >= 0)
  100. enable_percpu_irq(stimer0_irq, IRQ_TYPE_NONE);
  101. } else {
  102. /*
  103. * When it expires, the timer will generate a VMbus message,
  104. * to be handled by the normal VMbus interrupt handler.
  105. */
  106. timer_cfg.direct_mode = 0;
  107. timer_cfg.sintx = stimer0_message_sint;
  108. }
  109. hv_set_msr(HV_MSR_STIMER0_CONFIG, timer_cfg.as_uint64);
  110. return 0;
  111. }
  112. /*
  113. * hv_stimer_init - Per-cpu initialization of the clockevent
  114. */
  115. static int hv_stimer_init(unsigned int cpu)
  116. {
  117. struct clock_event_device *ce;
  118. if (!hv_clock_event)
  119. return 0;
  120. ce = per_cpu_ptr(hv_clock_event, cpu);
  121. ce->name = "Hyper-V clockevent";
  122. ce->features = CLOCK_EVT_FEAT_ONESHOT;
  123. ce->cpumask = cpumask_of(cpu);
  124. /*
  125. * Lower the rating of the Hyper-V timer in a TDX VM without paravisor,
  126. * so the local APIC timer (lapic_clockevent) is the default timer in
  127. * such a VM. The Hyper-V timer is not preferred in such a VM because
  128. * it depends on the slow VM Reference Counter MSR (the Hyper-V TSC
  129. * page is not enbled in such a VM because the VM uses Invariant TSC
  130. * as a better clocksource and it's challenging to mark the Hyper-V
  131. * TSC page shared in very early boot).
  132. */
  133. if (!ms_hyperv.paravisor_present && hv_isolation_type_tdx())
  134. ce->rating = 90;
  135. else
  136. ce->rating = 1000;
  137. ce->set_state_shutdown = hv_ce_shutdown;
  138. ce->set_state_oneshot = hv_ce_set_oneshot;
  139. ce->set_next_event = hv_ce_set_next_event;
  140. clockevents_config_and_register(ce,
  141. HV_CLOCK_HZ,
  142. HV_MIN_DELTA_TICKS,
  143. HV_MAX_MAX_DELTA_TICKS);
  144. return 0;
  145. }
  146. /*
  147. * hv_stimer_cleanup - Per-cpu cleanup of the clockevent
  148. */
  149. int hv_stimer_cleanup(unsigned int cpu)
  150. {
  151. struct clock_event_device *ce;
  152. if (!hv_clock_event)
  153. return 0;
  154. /*
  155. * In the legacy case where Direct Mode is not enabled
  156. * (which can only be on x86/64), stimer cleanup happens
  157. * relatively early in the CPU offlining process. We
  158. * must unbind the stimer-based clockevent device so
  159. * that the LAPIC timer can take over until clockevents
  160. * are no longer needed in the offlining process. Note
  161. * that clockevents_unbind_device() eventually calls
  162. * hv_ce_shutdown().
  163. *
  164. * The unbind should not be done when Direct Mode is
  165. * enabled because we may be on an architecture where
  166. * there are no other clockevent devices to fallback to.
  167. */
  168. ce = per_cpu_ptr(hv_clock_event, cpu);
  169. if (direct_mode_enabled)
  170. hv_ce_shutdown(ce);
  171. else
  172. clockevents_unbind_device(ce, cpu);
  173. return 0;
  174. }
  175. EXPORT_SYMBOL_GPL(hv_stimer_cleanup);
  176. /*
  177. * These placeholders are overridden by arch specific code on
  178. * architectures that need special setup of the stimer0 IRQ because
  179. * they don't support per-cpu IRQs (such as x86/x64).
  180. */
  181. void __weak hv_setup_stimer0_handler(void (*handler)(void))
  182. {
  183. };
  184. void __weak hv_remove_stimer0_handler(void)
  185. {
  186. };
  187. #ifdef CONFIG_ACPI
  188. /* Called only on architectures with per-cpu IRQs (i.e., not x86/x64) */
  189. static int hv_setup_stimer0_irq(void)
  190. {
  191. int ret;
  192. ret = acpi_register_gsi(NULL, HYPERV_STIMER0_VECTOR,
  193. ACPI_EDGE_SENSITIVE, ACPI_ACTIVE_HIGH);
  194. if (ret < 0) {
  195. pr_err("Can't register Hyper-V stimer0 GSI. Error %d", ret);
  196. return ret;
  197. }
  198. stimer0_irq = ret;
  199. ret = request_percpu_irq(stimer0_irq, hv_stimer0_percpu_isr,
  200. "Hyper-V stimer0", &stimer0_evt);
  201. if (ret) {
  202. pr_err("Can't request Hyper-V stimer0 IRQ %d. Error %d",
  203. stimer0_irq, ret);
  204. acpi_unregister_gsi(stimer0_irq);
  205. stimer0_irq = -1;
  206. }
  207. return ret;
  208. }
  209. static void hv_remove_stimer0_irq(void)
  210. {
  211. if (stimer0_irq == -1) {
  212. hv_remove_stimer0_handler();
  213. } else {
  214. free_percpu_irq(stimer0_irq, &stimer0_evt);
  215. acpi_unregister_gsi(stimer0_irq);
  216. stimer0_irq = -1;
  217. }
  218. }
  219. #else
  220. static int hv_setup_stimer0_irq(void)
  221. {
  222. return 0;
  223. }
  224. static void hv_remove_stimer0_irq(void)
  225. {
  226. }
  227. #endif
  228. /* hv_stimer_alloc - Global initialization of the clockevent and stimer0 */
  229. int hv_stimer_alloc(bool have_percpu_irqs)
  230. {
  231. int ret;
  232. /*
  233. * Synthetic timers are always available except on old versions of
  234. * Hyper-V on x86. In that case, return as error as Linux will use a
  235. * clockevent based on emulated LAPIC timer hardware.
  236. */
  237. if (!(ms_hyperv.features & HV_MSR_SYNTIMER_AVAILABLE))
  238. return -EINVAL;
  239. hv_clock_event = alloc_percpu(struct clock_event_device);
  240. if (!hv_clock_event)
  241. return -ENOMEM;
  242. direct_mode_enabled = ms_hyperv.misc_features &
  243. HV_STIMER_DIRECT_MODE_AVAILABLE;
  244. /*
  245. * If Direct Mode isn't enabled, the remainder of the initialization
  246. * is done later by hv_stimer_legacy_init()
  247. */
  248. if (!direct_mode_enabled)
  249. return 0;
  250. if (have_percpu_irqs) {
  251. ret = hv_setup_stimer0_irq();
  252. if (ret)
  253. goto free_clock_event;
  254. } else {
  255. hv_setup_stimer0_handler(hv_stimer0_isr);
  256. }
  257. /*
  258. * Since we are in Direct Mode, stimer initialization
  259. * can be done now with a CPUHP value in the same range
  260. * as other clockevent devices.
  261. */
  262. ret = cpuhp_setup_state(CPUHP_AP_HYPERV_TIMER_STARTING,
  263. "clockevents/hyperv/stimer:starting",
  264. hv_stimer_init, hv_stimer_cleanup);
  265. if (ret < 0) {
  266. hv_remove_stimer0_irq();
  267. goto free_clock_event;
  268. }
  269. return ret;
  270. free_clock_event:
  271. free_percpu(hv_clock_event);
  272. hv_clock_event = NULL;
  273. return ret;
  274. }
  275. EXPORT_SYMBOL_GPL(hv_stimer_alloc);
  276. /*
  277. * hv_stimer_legacy_init -- Called from the VMbus driver to handle
  278. * the case when Direct Mode is not enabled, and the stimer
  279. * must be initialized late in the CPU onlining process.
  280. *
  281. */
  282. void hv_stimer_legacy_init(unsigned int cpu, int sint)
  283. {
  284. if (direct_mode_enabled)
  285. return;
  286. /*
  287. * This function gets called by each vCPU, so setting the
  288. * global stimer_message_sint value each time is conceptually
  289. * not ideal, but the value passed in is always the same and
  290. * it avoids introducing yet another interface into this
  291. * clocksource driver just to set the sint in the legacy case.
  292. */
  293. stimer0_message_sint = sint;
  294. (void)hv_stimer_init(cpu);
  295. }
  296. EXPORT_SYMBOL_GPL(hv_stimer_legacy_init);
  297. /*
  298. * hv_stimer_legacy_cleanup -- Called from the VMbus driver to
  299. * handle the case when Direct Mode is not enabled, and the
  300. * stimer must be cleaned up early in the CPU offlining
  301. * process.
  302. */
  303. void hv_stimer_legacy_cleanup(unsigned int cpu)
  304. {
  305. if (direct_mode_enabled)
  306. return;
  307. (void)hv_stimer_cleanup(cpu);
  308. }
  309. EXPORT_SYMBOL_GPL(hv_stimer_legacy_cleanup);
  310. /*
  311. * Do a global cleanup of clockevents for the cases of kexec and
  312. * vmbus exit
  313. */
  314. void hv_stimer_global_cleanup(void)
  315. {
  316. int cpu;
  317. /*
  318. * hv_stime_legacy_cleanup() will stop the stimer if Direct
  319. * Mode is not enabled, and fallback to the LAPIC timer.
  320. */
  321. for_each_present_cpu(cpu) {
  322. hv_stimer_legacy_cleanup(cpu);
  323. }
  324. if (!hv_clock_event)
  325. return;
  326. if (direct_mode_enabled) {
  327. cpuhp_remove_state(CPUHP_AP_HYPERV_TIMER_STARTING);
  328. hv_remove_stimer0_irq();
  329. stimer0_irq = -1;
  330. }
  331. free_percpu(hv_clock_event);
  332. hv_clock_event = NULL;
  333. }
  334. EXPORT_SYMBOL_GPL(hv_stimer_global_cleanup);
  335. static __always_inline u64 read_hv_clock_msr(void)
  336. {
  337. /*
  338. * Read the partition counter to get the current tick count. This count
  339. * is set to 0 when the partition is created and is incremented in 100
  340. * nanosecond units.
  341. *
  342. * Use hv_raw_get_msr() because this function is used from
  343. * noinstr. Notable; while HV_MSR_TIME_REF_COUNT is a synthetic
  344. * register it doesn't need the GHCB path.
  345. */
  346. return hv_raw_get_msr(HV_MSR_TIME_REF_COUNT);
  347. }
  348. /*
  349. * Code and definitions for the Hyper-V clocksources. Two
  350. * clocksources are defined: one that reads the Hyper-V defined MSR, and
  351. * the other that uses the TSC reference page feature as defined in the
  352. * TLFS. The MSR version is for compatibility with old versions of
  353. * Hyper-V and 32-bit x86. The TSC reference page version is preferred.
  354. */
  355. static union {
  356. struct ms_hyperv_tsc_page page;
  357. u8 reserved[PAGE_SIZE];
  358. } tsc_pg __bss_decrypted __aligned(PAGE_SIZE);
  359. static struct ms_hyperv_tsc_page *tsc_page = &tsc_pg.page;
  360. static unsigned long tsc_pfn;
  361. unsigned long hv_get_tsc_pfn(void)
  362. {
  363. return tsc_pfn;
  364. }
  365. EXPORT_SYMBOL_GPL(hv_get_tsc_pfn);
  366. struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
  367. {
  368. return tsc_page;
  369. }
  370. EXPORT_SYMBOL_GPL(hv_get_tsc_page);
  371. static __always_inline u64 read_hv_clock_tsc(void)
  372. {
  373. u64 cur_tsc, time;
  374. /*
  375. * The Hyper-V Top-Level Function Spec (TLFS), section Timers,
  376. * subsection Refererence Counter, guarantees that the TSC and MSR
  377. * times are in sync and monotonic. Therefore we can fall back
  378. * to the MSR in case the TSC page indicates unavailability.
  379. */
  380. if (!hv_read_tsc_page_tsc(tsc_page, &cur_tsc, &time))
  381. time = read_hv_clock_msr();
  382. return time;
  383. }
  384. static u64 notrace read_hv_clock_tsc_cs(struct clocksource *arg)
  385. {
  386. return read_hv_clock_tsc();
  387. }
  388. static u64 noinstr read_hv_sched_clock_tsc(void)
  389. {
  390. return (read_hv_clock_tsc() - hv_sched_clock_offset) *
  391. (NSEC_PER_SEC / HV_CLOCK_HZ);
  392. }
  393. static void suspend_hv_clock_tsc(struct clocksource *arg)
  394. {
  395. union hv_reference_tsc_msr tsc_msr;
  396. /* Disable the TSC page */
  397. tsc_msr.as_uint64 = hv_get_msr(HV_MSR_REFERENCE_TSC);
  398. tsc_msr.enable = 0;
  399. hv_set_msr(HV_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
  400. }
  401. static void resume_hv_clock_tsc(struct clocksource *arg)
  402. {
  403. union hv_reference_tsc_msr tsc_msr;
  404. /* Re-enable the TSC page */
  405. tsc_msr.as_uint64 = hv_get_msr(HV_MSR_REFERENCE_TSC);
  406. tsc_msr.enable = 1;
  407. tsc_msr.pfn = tsc_pfn;
  408. hv_set_msr(HV_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
  409. }
  410. /*
  411. * Called during resume from hibernation, from overridden
  412. * x86_platform.restore_sched_clock_state routine. This is to adjust offsets
  413. * used to calculate time for hv tsc page based sched_clock, to account for
  414. * time spent before hibernation.
  415. */
  416. void hv_adj_sched_clock_offset(u64 offset)
  417. {
  418. hv_sched_clock_offset -= offset;
  419. }
  420. #ifdef HAVE_VDSO_CLOCKMODE_HVCLOCK
  421. static int hv_cs_enable(struct clocksource *cs)
  422. {
  423. vclocks_set_used(VDSO_CLOCKMODE_HVCLOCK);
  424. return 0;
  425. }
  426. #endif
  427. static struct clocksource hyperv_cs_tsc = {
  428. .name = "hyperv_clocksource_tsc_page",
  429. .rating = 500,
  430. .read = read_hv_clock_tsc_cs,
  431. .mask = CLOCKSOURCE_MASK(64),
  432. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  433. .suspend= suspend_hv_clock_tsc,
  434. .resume = resume_hv_clock_tsc,
  435. #ifdef HAVE_VDSO_CLOCKMODE_HVCLOCK
  436. .enable = hv_cs_enable,
  437. .vdso_clock_mode = VDSO_CLOCKMODE_HVCLOCK,
  438. #else
  439. .vdso_clock_mode = VDSO_CLOCKMODE_NONE,
  440. #endif
  441. };
  442. static u64 notrace read_hv_clock_msr_cs(struct clocksource *arg)
  443. {
  444. return read_hv_clock_msr();
  445. }
  446. static struct clocksource hyperv_cs_msr = {
  447. .name = "hyperv_clocksource_msr",
  448. .rating = 495,
  449. .read = read_hv_clock_msr_cs,
  450. .mask = CLOCKSOURCE_MASK(64),
  451. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  452. };
  453. /*
  454. * Reference to pv_ops must be inline so objtool
  455. * detection of noinstr violations can work correctly.
  456. */
  457. #ifdef CONFIG_GENERIC_SCHED_CLOCK
  458. static __always_inline void hv_setup_sched_clock(void *sched_clock)
  459. {
  460. /*
  461. * We're on an architecture with generic sched clock (not x86/x64).
  462. * The Hyper-V sched clock read function returns nanoseconds, not
  463. * the normal 100ns units of the Hyper-V synthetic clock.
  464. */
  465. sched_clock_register(sched_clock, 64, NSEC_PER_SEC);
  466. }
  467. #elif defined CONFIG_PARAVIRT
  468. static __always_inline void hv_setup_sched_clock(void *sched_clock)
  469. {
  470. /* We're on x86/x64 *and* using PV ops */
  471. paravirt_set_sched_clock(sched_clock);
  472. }
  473. #else /* !CONFIG_GENERIC_SCHED_CLOCK && !CONFIG_PARAVIRT */
  474. static __always_inline void hv_setup_sched_clock(void *sched_clock) {}
  475. #endif /* CONFIG_GENERIC_SCHED_CLOCK */
  476. static void __init hv_init_tsc_clocksource(void)
  477. {
  478. union hv_reference_tsc_msr tsc_msr;
  479. /*
  480. * If Hyper-V offers TSC_INVARIANT, then the virtualized TSC correctly
  481. * handles frequency and offset changes due to live migration,
  482. * pause/resume, and other VM management operations. So lower the
  483. * Hyper-V Reference TSC rating, causing the generic TSC to be used.
  484. * TSC_INVARIANT is not offered on ARM64, so the Hyper-V Reference
  485. * TSC will be preferred over the virtualized ARM64 arch counter.
  486. */
  487. if (ms_hyperv.features & HV_ACCESS_TSC_INVARIANT) {
  488. hyperv_cs_tsc.rating = 250;
  489. hyperv_cs_msr.rating = 245;
  490. }
  491. if (!(ms_hyperv.features & HV_MSR_REFERENCE_TSC_AVAILABLE))
  492. return;
  493. hv_read_reference_counter = read_hv_clock_tsc;
  494. /*
  495. * TSC page mapping works differently in root compared to guest.
  496. * - In guest partition the guest PFN has to be passed to the
  497. * hypervisor.
  498. * - In root partition it's other way around: it has to map the PFN
  499. * provided by the hypervisor.
  500. * But it can't be mapped right here as it's too early and MMU isn't
  501. * ready yet. So, we only set the enable bit here and will remap the
  502. * page later in hv_remap_tsc_clocksource().
  503. *
  504. * It worth mentioning, that TSC clocksource read function
  505. * (read_hv_clock_tsc) has a MSR-based fallback mechanism, used when
  506. * TSC page is zeroed (which is the case until the PFN is remapped) and
  507. * thus TSC clocksource will work even without the real TSC page
  508. * mapped.
  509. */
  510. tsc_msr.as_uint64 = hv_get_msr(HV_MSR_REFERENCE_TSC);
  511. if (hv_root_partition)
  512. tsc_pfn = tsc_msr.pfn;
  513. else
  514. tsc_pfn = HVPFN_DOWN(virt_to_phys(tsc_page));
  515. tsc_msr.enable = 1;
  516. tsc_msr.pfn = tsc_pfn;
  517. hv_set_msr(HV_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
  518. clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
  519. /*
  520. * If TSC is invariant, then let it stay as the sched clock since it
  521. * will be faster than reading the TSC page. But if not invariant, use
  522. * the TSC page so that live migrations across hosts with different
  523. * frequencies is handled correctly.
  524. */
  525. if (!(ms_hyperv.features & HV_ACCESS_TSC_INVARIANT)) {
  526. hv_sched_clock_offset = hv_read_reference_counter();
  527. hv_setup_sched_clock(read_hv_sched_clock_tsc);
  528. }
  529. }
  530. void __init hv_init_clocksource(void)
  531. {
  532. /*
  533. * Try to set up the TSC page clocksource, then the MSR clocksource.
  534. * At least one of these will always be available except on very old
  535. * versions of Hyper-V on x86. In that case we won't have a Hyper-V
  536. * clocksource, but Linux will still run with a clocksource based
  537. * on the emulated PIT or LAPIC timer.
  538. *
  539. * Never use the MSR clocksource as sched clock. It's too slow.
  540. * Better to use the native sched clock as the fallback.
  541. */
  542. hv_init_tsc_clocksource();
  543. if (ms_hyperv.features & HV_MSR_TIME_REF_COUNT_AVAILABLE)
  544. clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100);
  545. }
  546. void __init hv_remap_tsc_clocksource(void)
  547. {
  548. if (!(ms_hyperv.features & HV_MSR_REFERENCE_TSC_AVAILABLE))
  549. return;
  550. if (!hv_root_partition) {
  551. WARN(1, "%s: attempt to remap TSC page in guest partition\n",
  552. __func__);
  553. return;
  554. }
  555. tsc_page = memremap(tsc_pfn << HV_HYP_PAGE_SHIFT, sizeof(tsc_pg),
  556. MEMREMAP_WB);
  557. if (!tsc_page)
  558. pr_err("Failed to remap Hyper-V TSC page.\n");
  559. }