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.
7 #include <linux/device.h>
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <uapi/linux/kvm_para.h>
13 #include <asm/kvm_para.h>
14 #include <asm/pvclock.h>
15 #include <asm/kvmclock.h>
16 #include <uapi/asm/kvm_para.h>
18 #include <linux/ptp_clock_kernel.h>
20 struct kvm_ptp_clock {
21 struct ptp_clock *ptp_clock;
22 struct ptp_clock_info caps;
25 DEFINE_SPINLOCK(kvm_ptp_lock);
27 static struct pvclock_vsyscall_time_info *hv_clock;
29 static struct kvm_clock_pairing clock_pair;
30 static phys_addr_t clock_pair_gpa;
32 static int ptp_kvm_get_time_fn(ktime_t *device_time,
33 struct system_counterval_t *system_counter,
37 struct timespec64 tspec;
40 struct pvclock_vcpu_time_info *src;
42 spin_lock(&kvm_ptp_lock);
44 preempt_disable_notrace();
45 cpu = smp_processor_id();
46 src = &hv_clock[cpu].pvti;
50 * We are using a TSC value read in the hosts
51 * kvm_hc_clock_pairing handling.
52 * So any changes to tsc_to_system_mul
53 * and tsc_shift or any other pvclock
54 * data invalidate that measurement.
56 version = pvclock_read_begin(src);
58 ret = kvm_hypercall2(KVM_HC_CLOCK_PAIRING,
60 KVM_CLOCK_PAIRING_WALLCLOCK);
62 pr_err_ratelimited("clock pairing hypercall ret %lu\n", ret);
63 spin_unlock(&kvm_ptp_lock);
64 preempt_enable_notrace();
68 tspec.tv_sec = clock_pair.sec;
69 tspec.tv_nsec = clock_pair.nsec;
70 ret = __pvclock_read_cycles(src, clock_pair.tsc);
71 } while (pvclock_read_retry(src, version));
73 preempt_enable_notrace();
75 system_counter->cycles = ret;
76 system_counter->cs = &kvm_clock;
78 *device_time = timespec64_to_ktime(tspec);
80 spin_unlock(&kvm_ptp_lock);
85 static int ptp_kvm_getcrosststamp(struct ptp_clock_info *ptp,
86 struct system_device_crosststamp *xtstamp)
88 return get_device_system_crosststamp(ptp_kvm_get_time_fn, NULL,
93 * PTP clock operations
96 static int ptp_kvm_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
101 static int ptp_kvm_adjtime(struct ptp_clock_info *ptp, s64 delta)
106 static int ptp_kvm_settime(struct ptp_clock_info *ptp,
107 const struct timespec64 *ts)
112 static int ptp_kvm_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
115 struct timespec64 tspec;
117 spin_lock(&kvm_ptp_lock);
119 ret = kvm_hypercall2(KVM_HC_CLOCK_PAIRING,
121 KVM_CLOCK_PAIRING_WALLCLOCK);
123 pr_err_ratelimited("clock offset hypercall ret %lu\n", ret);
124 spin_unlock(&kvm_ptp_lock);
128 tspec.tv_sec = clock_pair.sec;
129 tspec.tv_nsec = clock_pair.nsec;
130 spin_unlock(&kvm_ptp_lock);
132 memcpy(ts, &tspec, sizeof(struct timespec64));
137 static int ptp_kvm_enable(struct ptp_clock_info *ptp,
138 struct ptp_clock_request *rq, int on)
143 static const struct ptp_clock_info ptp_kvm_caps = {
144 .owner = THIS_MODULE,
145 .name = "KVM virtual PTP",
150 .adjfreq = ptp_kvm_adjfreq,
151 .adjtime = ptp_kvm_adjtime,
152 .gettime64 = ptp_kvm_gettime,
153 .settime64 = ptp_kvm_settime,
154 .enable = ptp_kvm_enable,
155 .getcrosststamp = ptp_kvm_getcrosststamp,
158 /* module operations */
160 static struct kvm_ptp_clock kvm_ptp_clock;
162 static void __exit ptp_kvm_exit(void)
164 ptp_clock_unregister(kvm_ptp_clock.ptp_clock);
167 static int __init ptp_kvm_init(void)
171 if (!kvm_para_available())
174 clock_pair_gpa = slow_virt_to_phys(&clock_pair);
175 hv_clock = pvclock_get_pvti_cpu0_va();
180 ret = kvm_hypercall2(KVM_HC_CLOCK_PAIRING, clock_pair_gpa,
181 KVM_CLOCK_PAIRING_WALLCLOCK);
182 if (ret == -KVM_ENOSYS || ret == -KVM_EOPNOTSUPP)
185 kvm_ptp_clock.caps = ptp_kvm_caps;
187 kvm_ptp_clock.ptp_clock = ptp_clock_register(&kvm_ptp_clock.caps, NULL);
189 return PTR_ERR_OR_ZERO(kvm_ptp_clock.ptp_clock);
192 module_init(ptp_kvm_init);
193 module_exit(ptp_kvm_exit);
195 MODULE_AUTHOR("Marcelo Tosatti <mtosatti@redhat.com>");
196 MODULE_DESCRIPTION("PTP clock using KVMCLOCK");
197 MODULE_LICENSE("GPL");