mtd: rawnand: marvell: pass ms delay to wait_op
[linux/fpc-iii.git] / drivers / ptp / ptp_kvm.c
blobc67dd11e08b1f5033edd14856172270f08cf5fad
1 /*
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,
44 void *ctx)
46 unsigned long ret;
47 struct timespec64 tspec;
48 unsigned version;
49 int cpu;
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;
58 do {
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,
69 clock_pair_gpa,
70 KVM_CLOCK_PAIRING_WALLCLOCK);
71 if (ret != 0) {
72 pr_err_ratelimited("clock pairing hypercall ret %lu\n", ret);
73 spin_unlock(&kvm_ptp_lock);
74 preempt_enable_notrace();
75 return -EOPNOTSUPP;
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);
92 return 0;
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,
99 NULL, xtstamp);
103 * PTP clock operations
106 static int ptp_kvm_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
108 return -EOPNOTSUPP;
111 static int ptp_kvm_adjtime(struct ptp_clock_info *ptp, s64 delta)
113 return -EOPNOTSUPP;
116 static int ptp_kvm_settime(struct ptp_clock_info *ptp,
117 const struct timespec64 *ts)
119 return -EOPNOTSUPP;
122 static int ptp_kvm_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
124 unsigned long ret;
125 struct timespec64 tspec;
127 spin_lock(&kvm_ptp_lock);
129 ret = kvm_hypercall2(KVM_HC_CLOCK_PAIRING,
130 clock_pair_gpa,
131 KVM_CLOCK_PAIRING_WALLCLOCK);
132 if (ret != 0) {
133 pr_err_ratelimited("clock offset hypercall ret %lu\n", ret);
134 spin_unlock(&kvm_ptp_lock);
135 return -EOPNOTSUPP;
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));
144 return 0;
147 static int ptp_kvm_enable(struct ptp_clock_info *ptp,
148 struct ptp_clock_request *rq, int on)
150 return -EOPNOTSUPP;
153 static const struct ptp_clock_info ptp_kvm_caps = {
154 .owner = THIS_MODULE,
155 .name = "KVM virtual PTP",
156 .max_adj = 0,
157 .n_ext_ts = 0,
158 .n_pins = 0,
159 .pps = 0,
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)
179 long ret;
181 if (!kvm_para_available())
182 return -ENODEV;
184 clock_pair_gpa = slow_virt_to_phys(&clock_pair);
185 hv_clock = pvclock_get_pvti_cpu0_va();
187 if (!hv_clock)
188 return -ENODEV;
190 ret = kvm_hypercall2(KVM_HC_CLOCK_PAIRING, clock_pair_gpa,
191 KVM_CLOCK_PAIRING_WALLCLOCK);
192 if (ret == -KVM_ENOSYS || ret == -KVM_EOPNOTSUPP)
193 return -ENODEV;
195 kvm_ptp_clock.caps = ptp_kvm_caps;
197 kvm_ptp_clock.ptp_clock = ptp_clock_register(&kvm_ptp_clock.caps, NULL);
199 return PTR_ERR_OR_ZERO(kvm_ptp_clock.ptp_clock);
202 module_init(ptp_kvm_init);
203 module_exit(ptp_kvm_exit);
205 MODULE_AUTHOR("Marcelo Tosatti <mtosatti@redhat.com>");
206 MODULE_DESCRIPTION("PTP clock using KVMCLOCK");
207 MODULE_LICENSE("GPL");