From: Marcelo Tosatti <mtosatti@redhat.com>
To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Paolo Bonzini <pbonzini@redhat.com>,
Radim Krcmar <rkrcmar@redhat.com>,
Richard Cochran <richardcochran@gmail.com>,
Miroslav Lichvar <mlichvar@redhat.com>,
Marcelo Tosatti <mtosatti@redhat.com>
Subject: [patch 4/5] PTP: add PTP_SYS_OFFSET emulation via cross timestamps infrastructure
Date: Fri, 20 Jan 2017 12:51:18 -0200 [thread overview]
Message-ID: <20170120145456.411891603@redhat.com> (raw)
In-Reply-To: 20170120145114.010318134@redhat.com
[-- Attachment #1: ptp-add-sysoffset-callback --]
[-- Type: text/plain, Size: 3825 bytes --]
Emulate PTP_SYS_OFFSET by using an arithmetic mean of the
realtime samples from ->getcrosststamp callback.
Note: if the realtime clock changes during cross timestamp sampling,
then the mean will return a value that does not exist. Given the
nature of PTP_SYS_OFFSET users (time synchronization),
that should not be a problem.
Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
---
drivers/ptp/ptp_chardev.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++
drivers/ptp/ptp_clock.c | 10 ++++++
2 files changed, 83 insertions(+)
v2: drop timekeeper spinlock, move back to drivers/ptp/ptp_chardev.c (Paolo)
ptp_clock_gettime: support drivers with crosstimestamp but not
gettime64 callbacks
Index: kvm-ptpdriver/drivers/ptp/ptp_chardev.c
===================================================================
--- kvm-ptpdriver.orig/drivers/ptp/ptp_chardev.c 2017-01-20 12:48:14.201126341 -0200
+++ kvm-ptpdriver/drivers/ptp/ptp_chardev.c 2017-01-20 12:48:45.270207972 -0200
@@ -116,6 +116,74 @@
return 0;
}
+static void mean_sys_realtime(struct system_device_crosststamp *xtstamp,
+ struct system_device_crosststamp *n_xtstamp,
+ struct ptp_clock_time *pct)
+{
+ ktime_t sys_realtime, n_sys_realtime;
+ struct timespec ts;
+ u64 ns;
+
+ /* estimate realtime with arithmetic mean of two crosststamp samples */
+ sys_realtime = xtstamp->sys_realtime;
+ n_sys_realtime = n_xtstamp->sys_realtime;
+ ns = ktime_divns(ktime_add(sys_realtime, n_sys_realtime), 2);
+ ts = ktime_to_timespec(ns_to_ktime(ns));
+ pct->sec = ts.tv_sec;
+ pct->nsec = ts.tv_nsec;
+}
+
+static int emulate_ptp_sys_offset(struct ptp_clock_info *info,
+ struct ptp_sys_offset *sysoff,
+ unsigned long arg)
+{
+ int i, err, len;
+ int n_samples;
+ struct system_device_crosststamp *xtstamps, *xtstamp, *n_xtstamp;
+ struct ptp_clock_time *pct;
+
+ n_samples = sysoff->n_samples + 2;
+
+ len = sizeof(struct system_device_crosststamp);
+ xtstamps = kzalloc(len * n_samples, GFP_KERNEL);
+ if (xtstamps == NULL)
+ return -ENOMEM;
+
+ xtstamp = xtstamps;
+ for (i = 0; i < n_samples; i++) {
+ err = info->getcrosststamp(info, xtstamp);
+ if (err) {
+ kfree(xtstamps);
+ return err;
+ }
+ xtstamp++;
+ }
+
+ pct = &sysoff->ts[0];
+ xtstamp = n_xtstamp = xtstamps;
+ n_xtstamp++;
+ for (i = 0; i < sysoff->n_samples; i++) {
+ struct timespec ts;
+
+ mean_sys_realtime(xtstamp, n_xtstamp, pct);
+ pct++;
+
+ ts = ktime_to_timespec(n_xtstamp->device);
+ pct->sec = ts.tv_sec;
+ pct->nsec = ts.tv_nsec;
+ pct++;
+ xtstamp++;
+ n_xtstamp++;
+ }
+
+ mean_sys_realtime(xtstamp, n_xtstamp, pct);
+ kfree(xtstamps);
+ if (copy_to_user((void __user *)arg, sysoff, sizeof(*sysoff)))
+ return -EFAULT;
+
+ return 0;
+}
+
long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
{
struct ptp_clock_caps caps;
@@ -219,6 +287,11 @@
err = -EINVAL;
break;
}
+
+ if (!ptp->info->gettime64 && ptp->info->getcrosststamp) {
+ err = emulate_ptp_sys_offset(ptp->info, sysoff, arg);
+ break;
+ }
pct = &sysoff->ts[0];
for (i = 0; i < sysoff->n_samples; i++) {
getnstimeofday64(&ts);
Index: kvm-ptpdriver/drivers/ptp/ptp_clock.c
===================================================================
--- kvm-ptpdriver.orig/drivers/ptp/ptp_clock.c 2017-01-20 12:48:14.201126341 -0200
+++ kvm-ptpdriver/drivers/ptp/ptp_clock.c 2017-01-20 12:48:45.271207975 -0200
@@ -118,6 +118,16 @@
struct timespec64 ts;
int err;
+ if (!ptp->info->gettime64 && ptp->info->getcrosststamp) {
+ struct system_device_crosststamp xtstamp;
+
+ err = ptp->info->getcrosststamp(ptp->info, &xtstamp);
+ if (err)
+ return err;
+ *tp = ktime_to_timespec(xtstamp.device);
+ return err;
+ }
+
err = ptp->info->gettime64(ptp->info, &ts);
if (!err)
*tp = timespec64_to_timespec(ts);
next prev parent reply other threads:[~2017-01-20 14:51 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-01-20 14:51 [patch 0/5] KVM virtual PTP driver (v4) Marcelo Tosatti
2017-01-20 14:51 ` [patch 1/5] KVM: x86: provide realtime host clock via vsyscall notifiers Marcelo Tosatti
2017-01-20 14:51 ` [patch 2/5] KVM: x86: add KVM_HC_CLOCK_OFFSET hypercall Marcelo Tosatti
2017-01-20 14:51 ` [patch 3/5] kvmclock: export kvmclock clocksource pointer Marcelo Tosatti
2017-01-20 14:51 ` Marcelo Tosatti [this message]
2017-01-20 14:51 ` [patch 5/5] PTP: add kvm PTP driver Marcelo Tosatti
2017-01-20 15:03 ` [patch 5/5] PTP: add kvm PTP driver (v2) Marcelo Tosatti
-- strict thread matches above, loose matches on Subject: below --
2017-01-20 12:20 [patch 0/5] KVM virtual PTP driver (v3) Marcelo Tosatti
2017-01-20 12:20 ` [patch 4/5] PTP: add PTP_SYS_OFFSET emulation via cross timestamps infrastructure Marcelo Tosatti
2017-01-20 12:55 ` Paolo Bonzini
2017-01-20 13:07 ` Marcelo Tosatti
2017-01-20 13:36 ` Paolo Bonzini
2017-01-20 13:52 ` Marcelo Tosatti
2017-01-20 14:02 ` Radim Krcmar
2017-01-20 14:23 ` Paolo Bonzini
2017-01-20 14:31 ` Miroslav Lichvar
2017-01-20 18:30 ` Radim Krcmar
2017-01-20 20:25 ` Richard Cochran
2017-01-23 13:19 ` Marcelo Tosatti
2017-01-23 18:44 ` Richard Cochran
2017-01-23 19:44 ` Paolo Bonzini
2017-01-24 5:43 ` Richard Cochran
2017-01-24 11:23 ` Marcelo Tosatti
2017-01-24 11:35 ` Richard Cochran
2017-01-23 23:06 ` Marcelo Tosatti
2017-01-24 5:32 ` Richard Cochran
2017-01-24 8:15 ` Miroslav Lichvar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170120145456.411891603@redhat.com \
--to=mtosatti@redhat.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mlichvar@redhat.com \
--cc=pbonzini@redhat.com \
--cc=richardcochran@gmail.com \
--cc=rkrcmar@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).