1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Virtual PTP 1588 clock for use with KVM guests
5 * Copyright (C) 2017 Red Hat Inc.
8 #include <linux/device.h>
9 #include <linux/kernel.h>
10 #include <asm/pvclock.h>
11 #include <asm/kvmclock.h>
12 #include <linux/module.h>
13 #include <uapi/asm/kvm_para.h>
14 #include <uapi/linux/kvm_para.h>
15 #include <linux/ptp_clock_kernel.h>
16 #include <linux/ptp_kvm.h>
18 struct pvclock_vsyscall_time_info *hv_clock;
20 static phys_addr_t clock_pair_gpa;
21 static struct kvm_clock_pairing clock_pair;
23 int kvm_arch_ptp_init(void)
27 if (!kvm_para_available())
30 clock_pair_gpa = slow_virt_to_phys(&clock_pair);
31 hv_clock = pvclock_get_pvti_cpu0_va();
35 ret = kvm_hypercall2(KVM_HC_CLOCK_PAIRING, clock_pair_gpa,
36 KVM_CLOCK_PAIRING_WALLCLOCK);
37 if (ret == -KVM_ENOSYS || ret == -KVM_EOPNOTSUPP)
43 int kvm_arch_ptp_get_clock(struct timespec64 *ts)
47 ret = kvm_hypercall2(KVM_HC_CLOCK_PAIRING,
49 KVM_CLOCK_PAIRING_WALLCLOCK);
51 pr_err_ratelimited("clock offset hypercall ret %lu\n", ret);
55 ts->tv_sec = clock_pair.sec;
56 ts->tv_nsec = clock_pair.nsec;
61 int kvm_arch_ptp_get_crosststamp(u64 *cycle, struct timespec64 *tspec,
62 struct clocksource **cs)
64 struct pvclock_vcpu_time_info *src;
69 cpu = smp_processor_id();
70 src = &hv_clock[cpu].pvti;
74 * We are using a TSC value read in the hosts
75 * kvm_hc_clock_pairing handling.
76 * So any changes to tsc_to_system_mul
77 * and tsc_shift or any other pvclock
78 * data invalidate that measurement.
80 version = pvclock_read_begin(src);
82 ret = kvm_hypercall2(KVM_HC_CLOCK_PAIRING,
84 KVM_CLOCK_PAIRING_WALLCLOCK);
86 pr_err_ratelimited("clock pairing hypercall ret %lu\n", ret);
89 tspec->tv_sec = clock_pair.sec;
90 tspec->tv_nsec = clock_pair.nsec;
91 *cycle = __pvclock_read_cycles(src, clock_pair.tsc);
92 } while (pvclock_read_retry(src, version));