Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / ptp / ptp_kvm.c
blob658d33fc31952f732bd7715441984215f6b3bea0
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Virtual PTP 1588 clock for use with KVM guests
5 * Copyright (C) 2017 Red Hat Inc.
6 */
7 #include <linux/device.h>
8 #include <linux/err.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 static 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,
34 void *ctx)
36 unsigned long ret;
37 struct timespec64 tspec;
38 unsigned version;
39 int cpu;
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;
48 do {
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,
59 clock_pair_gpa,
60 KVM_CLOCK_PAIRING_WALLCLOCK);
61 if (ret != 0) {
62 pr_err_ratelimited("clock pairing hypercall ret %lu\n", ret);
63 spin_unlock(&kvm_ptp_lock);
64 preempt_enable_notrace();
65 return -EOPNOTSUPP;
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);
82 return 0;
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,
89 NULL, xtstamp);
93 * PTP clock operations
96 static int ptp_kvm_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
98 return -EOPNOTSUPP;
101 static int ptp_kvm_adjtime(struct ptp_clock_info *ptp, s64 delta)
103 return -EOPNOTSUPP;
106 static int ptp_kvm_settime(struct ptp_clock_info *ptp,
107 const struct timespec64 *ts)
109 return -EOPNOTSUPP;
112 static int ptp_kvm_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
114 unsigned long ret;
115 struct timespec64 tspec;
117 spin_lock(&kvm_ptp_lock);
119 ret = kvm_hypercall2(KVM_HC_CLOCK_PAIRING,
120 clock_pair_gpa,
121 KVM_CLOCK_PAIRING_WALLCLOCK);
122 if (ret != 0) {
123 pr_err_ratelimited("clock offset hypercall ret %lu\n", ret);
124 spin_unlock(&kvm_ptp_lock);
125 return -EOPNOTSUPP;
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));
134 return 0;
137 static int ptp_kvm_enable(struct ptp_clock_info *ptp,
138 struct ptp_clock_request *rq, int on)
140 return -EOPNOTSUPP;
143 static const struct ptp_clock_info ptp_kvm_caps = {
144 .owner = THIS_MODULE,
145 .name = "KVM virtual PTP",
146 .max_adj = 0,
147 .n_ext_ts = 0,
148 .n_pins = 0,
149 .pps = 0,
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)
169 long ret;
171 if (!kvm_para_available())
172 return -ENODEV;
174 clock_pair_gpa = slow_virt_to_phys(&clock_pair);
175 hv_clock = pvclock_get_pvti_cpu0_va();
177 if (!hv_clock)
178 return -ENODEV;
180 ret = kvm_hypercall2(KVM_HC_CLOCK_PAIRING, clock_pair_gpa,
181 KVM_CLOCK_PAIRING_WALLCLOCK);
182 if (ret == -KVM_ENOSYS || ret == -KVM_EOPNOTSUPP)
183 return -ENODEV;
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");