2 * Virtual PTP 1588 clock for use with KVM guests
4 * Copyright (C) 2017 Red Hat Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/device.h>
18 #include <linux/err.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <uapi/linux/kvm_para.h>
23 #include <asm/kvm_para.h>
24 #include <asm/pvclock.h>
25 #include <asm/kvmclock.h>
26 #include <uapi/asm/kvm_para.h>
28 #include <linux/ptp_clock_kernel.h>
30 struct kvm_ptp_clock {
31 struct ptp_clock *ptp_clock;
32 struct ptp_clock_info caps;
35 DEFINE_SPINLOCK(kvm_ptp_lock);
37 static struct pvclock_vsyscall_time_info *hv_clock;
39 static struct kvm_clock_pairing clock_pair;
40 static phys_addr_t clock_pair_gpa;
42 static int ptp_kvm_get_time_fn(ktime_t *device_time,
43 struct system_counterval_t *system_counter,
47 struct timespec64 tspec;
50 struct pvclock_vcpu_time_info *src;
52 spin_lock(&kvm_ptp_lock);
54 preempt_disable_notrace();
55 cpu = smp_processor_id();
56 src = &hv_clock[cpu].pvti;
60 * We are using a TSC value read in the hosts
61 * kvm_hc_clock_pairing handling.
62 * So any changes to tsc_to_system_mul
63 * and tsc_shift or any other pvclock
64 * data invalidate that measurement.
66 version = pvclock_read_begin(src);
68 ret = kvm_hypercall2(KVM_HC_CLOCK_PAIRING,
70 KVM_CLOCK_PAIRING_WALLCLOCK);
72 pr_err_ratelimited("clock pairing hypercall ret %lu\n", ret);
73 spin_unlock(&kvm_ptp_lock);
74 preempt_enable_notrace();
78 tspec.tv_sec = clock_pair.sec;
79 tspec.tv_nsec = clock_pair.nsec;
80 ret = __pvclock_read_cycles(src, clock_pair.tsc);
81 } while (pvclock_read_retry(src, version));
83 preempt_enable_notrace();
85 system_counter->cycles = ret;
86 system_counter->cs = &kvm_clock;
88 *device_time = timespec64_to_ktime(tspec);
90 spin_unlock(&kvm_ptp_lock);
95 static int ptp_kvm_getcrosststamp(struct ptp_clock_info *ptp,
96 struct system_device_crosststamp *xtstamp)
98 return get_device_system_crosststamp(ptp_kvm_get_time_fn, NULL,
103 * PTP clock operations
106 static int ptp_kvm_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
111 static int ptp_kvm_adjtime(struct ptp_clock_info *ptp, s64 delta)
116 static int ptp_kvm_settime(struct ptp_clock_info *ptp,
117 const struct timespec64 *ts)
122 static int ptp_kvm_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
125 struct timespec64 tspec;
127 spin_lock(&kvm_ptp_lock);
129 ret = kvm_hypercall2(KVM_HC_CLOCK_PAIRING,
131 KVM_CLOCK_PAIRING_WALLCLOCK);
133 pr_err_ratelimited("clock offset hypercall ret %lu\n", ret);
134 spin_unlock(&kvm_ptp_lock);
138 tspec.tv_sec = clock_pair.sec;
139 tspec.tv_nsec = clock_pair.nsec;
140 spin_unlock(&kvm_ptp_lock);
142 memcpy(ts, &tspec, sizeof(struct timespec64));
147 static int ptp_kvm_enable(struct ptp_clock_info *ptp,
148 struct ptp_clock_request *rq, int on)
153 static const struct ptp_clock_info ptp_kvm_caps = {
154 .owner = THIS_MODULE,
155 .name = "KVM virtual PTP",
160 .adjfreq = ptp_kvm_adjfreq,
161 .adjtime = ptp_kvm_adjtime,
162 .gettime64 = ptp_kvm_gettime,
163 .settime64 = ptp_kvm_settime,
164 .enable = ptp_kvm_enable,
165 .getcrosststamp = ptp_kvm_getcrosststamp,
168 /* module operations */
170 static struct kvm_ptp_clock kvm_ptp_clock;
172 static void __exit ptp_kvm_exit(void)
174 ptp_clock_unregister(kvm_ptp_clock.ptp_clock);
177 static int __init ptp_kvm_init(void)
181 clock_pair_gpa = slow_virt_to_phys(&clock_pair);
182 hv_clock = pvclock_pvti_cpu0_va();
187 ret = kvm_hypercall2(KVM_HC_CLOCK_PAIRING, clock_pair_gpa,
188 KVM_CLOCK_PAIRING_WALLCLOCK);
189 if (ret == -KVM_ENOSYS || ret == -KVM_EOPNOTSUPP)
192 kvm_ptp_clock.caps = ptp_kvm_caps;
194 kvm_ptp_clock.ptp_clock = ptp_clock_register(&kvm_ptp_clock.caps, NULL);
196 return PTR_ERR_OR_ZERO(kvm_ptp_clock.ptp_clock);
199 module_init(ptp_kvm_init);
200 module_exit(ptp_kvm_exit);
202 MODULE_AUTHOR("Marcelo Tosatti <mtosatti@redhat.com>");
203 MODULE_DESCRIPTION("PTP clock using KVMCLOCK");
204 MODULE_LICENSE("GPL");