Release 4.14 arch/x86/xen/time.c
// SPDX-License-Identifier: GPL-2.0
/*
* Xen time implementation.
*
* This is implemented in terms of a clocksource driver which uses
* the hypervisor clock as a nanosecond timebase, and a clockevent
* driver which uses the hypervisor's timer mechanism.
*
* Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
*/
#include <linux/kernel.h>
#include <linux/interrupt.h>
#include <linux/clocksource.h>
#include <linux/clockchips.h>
#include <linux/gfp.h>
#include <linux/slab.h>
#include <linux/pvclock_gtod.h>
#include <linux/timekeeper_internal.h>
#include <asm/pvclock.h>
#include <asm/xen/hypervisor.h>
#include <asm/xen/hypercall.h>
#include <xen/events.h>
#include <xen/features.h>
#include <xen/interface/xen.h>
#include <xen/interface/vcpu.h>
#include "xen-ops.h"
/* Xen may fire a timer up to this many ns early */
#define TIMER_SLOP 100000
/* Get the TSC speed from Xen */
static unsigned long xen_tsc_khz(void)
{
struct pvclock_vcpu_time_info *info =
&HYPERVISOR_shared_info->vcpu_info[0].time;
return pvclock_tsc_khz(info);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Jeremy Fitzhardinge | 25 | 83.33% | 1 | 20.00% |
Glauber de Oliveira Costa | 2 | 6.67% | 1 | 20.00% |
Gerd Hoffmann | 1 | 3.33% | 1 | 20.00% |
Alok N Kataria | 1 | 3.33% | 1 | 20.00% |
Stefano Stabellini | 1 | 3.33% | 1 | 20.00% |
Total | 30 | 100.00% | 5 | 100.00% |
u64 xen_clocksource_read(void)
{
struct pvclock_vcpu_time_info *src;
u64 ret;
preempt_disable_notrace();
src = &__this_cpu_read(xen_vcpu)->time;
ret = pvclock_clocksource_read(src);
preempt_enable_notrace();
return ret;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Jeremy Fitzhardinge | 30 | 73.17% | 3 | 42.86% |
Gerd Hoffmann | 5 | 12.20% | 1 | 14.29% |
Boris Ostrovsky | 3 | 7.32% | 1 | 14.29% |
Thomas Gleixner | 2 | 4.88% | 1 | 14.29% |
Christoph Lameter | 1 | 2.44% | 1 | 14.29% |
Total | 41 | 100.00% | 7 | 100.00% |
static u64 xen_clocksource_get_cycles(struct clocksource *cs)
{
return xen_clocksource_read();
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Magnus Damm | 14 | 93.33% | 1 | 50.00% |
Thomas Gleixner | 1 | 6.67% | 1 | 50.00% |
Total | 15 | 100.00% | 2 | 100.00% |
static void xen_read_wallclock(struct timespec *ts)
{
struct shared_info *s = HYPERVISOR_shared_info;
struct pvclock_wall_clock *wall_clock = &(s->wc);
struct pvclock_vcpu_time_info *vcpu_time;
vcpu_time = &get_cpu_var(xen_vcpu)->time;
pvclock_read_wallclock(wall_clock, vcpu_time, ts);
put_cpu_var(xen_vcpu);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Jeremy Fitzhardinge | 33 | 55.93% | 1 | 50.00% |
Gerd Hoffmann | 26 | 44.07% | 1 | 50.00% |
Total | 59 | 100.00% | 2 | 100.00% |
static void xen_get_wallclock(struct timespec *now)
{
xen_read_wallclock(now);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Jeremy Fitzhardinge | 9 | 56.25% | 1 | 33.33% |
David Vrabel | 6 | 37.50% | 1 | 33.33% |
Stefano Stabellini | 1 | 6.25% | 1 | 33.33% |
Total | 16 | 100.00% | 3 | 100.00% |
static int xen_set_wallclock(const struct timespec *now)
{
return -1;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Jeremy Fitzhardinge | 11 | 68.75% | 1 | 33.33% |
David Vrabel | 4 | 25.00% | 1 | 33.33% |
Stefano Stabellini | 1 | 6.25% | 1 | 33.33% |
Total | 16 | 100.00% | 3 | 100.00% |
static int xen_pvclock_gtod_notify(struct notifier_block *nb,
unsigned long was_set, void *priv)
{
/* Protected by the calling core code serialization */
static struct timespec64 next_sync;
struct xen_platform_op op;
struct timespec64 now;
struct timekeeper *tk = priv;
static bool settime64_supported = true;
int ret;
now.tv_sec = tk->xtime_sec;
now.tv_nsec = (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
/*
* We only take the expensive HV call when the clock was set
* or when the 11 minutes RTC synchronization time elapsed.
*/
if (!was_set && timespec64_compare(&now, &next_sync) < 0)
return NOTIFY_OK;
again:
if (settime64_supported) {
op.cmd = XENPF_settime64;
op.u.settime64.mbz = 0;
op.u.settime64.secs = now.tv_sec;
op.u.settime64.nsecs = now.tv_nsec;
op.u.settime64.system_time = xen_clocksource_read();
} else {
op.cmd = XENPF_settime32;
op.u.settime32.secs = now.tv_sec;
op.u.settime32.nsecs = now.tv_nsec;
op.u.settime32.system_time = xen_clocksource_read();
}
ret = HYPERVISOR_platform_op(&op);
if (ret == -ENOSYS && settime64_supported) {
settime64_supported = false;
goto again;
}
if (ret < 0)
return NOTIFY_BAD;
/*
* Move the next drift compensation time 11 minutes
* ahead. That's emulating the sync_cmos_clock() update for
* the hardware RTC.
*/
next_sync = now;
next_sync.tv_sec += 11 * 60;
return NOTIFY_OK;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Stefano Stabellini | 141 | 55.95% | 4 | 50.00% |
Jeremy Fitzhardinge | 57 | 22.62% | 2 | 25.00% |
David Vrabel | 54 | 21.43% | 2 | 25.00% |
Total | 252 | 100.00% | 8 | 100.00% |
static struct notifier_block xen_pvclock_gtod_notifier = {
.notifier_call = xen_pvclock_gtod_notify,
};
static struct clocksource xen_clocksource __read_mostly = {
.name = "xen",
.rating = 400,
.read = xen_clocksource_get_cycles,
.mask = ~0,
.flags = CLOCK_SOURCE_IS_CONTINUOUS,
};
/*
Xen clockevent implementation
Xen has two clockevent implementations:
The old timer_op one works with all released versions of Xen prior
to version 3.0.4. This version of the hypervisor provides a
single-shot timer with nanosecond resolution. However, sharing the
same event channel is a 100Hz tick which is delivered while the
vcpu is running. We don't care about or use this tick, but it will
cause the core time code to think the timer fired too soon, and
will end up resetting it each time. It could be filtered, but
doing so has complications when the ktime clocksource is not yet
the xen clocksource (ie, at boot time).
The new vcpu_op-based timer interface allows the tick timer period
to be changed or turned off. The tick timer is not useful as a
periodic timer because events are only delivered to running vcpus.
The one-shot timer can report when a timeout is in the past, so
set_next_event is capable of returning -ETIME when appropriate.
This interface is used when available.
*/
/*
Get a hypervisor absolute time. In theory we could maintain an
offset between the kernel's time and the hypervisor's time, and
apply that to a kernel's absolute timeout. Unfortunately the
hypervisor and kernel times can drift even if the kernel is using
the Xen clocksource, because ntp can warp the kernel's clocksource.
*/
static s64 get_abs_timeout(unsigned long delta)
{
return xen_clocksource_read() + delta;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Jeremy Fitzhardinge | 16 | 100.00% | 1 | 100.00% |
Total | 16 | 100.00% | 1 | 100.00% |
static int xen_timerop_shutdown(struct clock_event_device *evt)
{
/* cancel timeout */
HYPERVISOR_set_timer_op(0);
return 0;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Jeremy Fitzhardinge | 14 | 70.00% | 1 | 50.00% |
Viresh Kumar | 6 | 30.00% | 1 | 50.00% |
Total | 20 | 100.00% | 2 | 100.00% |
static int xen_timerop_set_next_event(unsigned long delta,
struct clock_event_device *evt)
{
WARN_ON(!clockevent_state_oneshot(evt));
if (HYPERVISOR_set_timer_op(get_abs_timeout(delta)) < 0)
BUG();
/* We may have missed the deadline, but there's no real way of
knowing for sure. If the event was in the past, then we'll
get an immediate interrupt. */
return 0;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Jeremy Fitzhardinge | 39 | 90.70% | 1 | 50.00% |
Viresh Kumar | 4 | 9.30% | 1 | 50.00% |
Total | 43 | 100.00% | 2 | 100.00% |
static const struct clock_event_device xen_timerop_clockevent = {
.name = "xen",
.features = CLOCK_EVT_FEAT_ONESHOT,
.max_delta_ns = 0xffffffff,
.max_delta_ticks = 0xffffffff,
.min_delta_ns = TIMER_SLOP,
.min_delta_ticks = TIMER_SLOP,
.mult = 1,
.shift = 0,
.rating = 500,
.set_state_shutdown = xen_timerop_shutdown,
.set_next_event = xen_timerop_set_next_event,
};
static int xen_vcpuop_shutdown(struct clock_event_device *evt)
{
int cpu = smp_processor_id();
if (HYPERVISOR_vcpu_op(VCPUOP_stop_singleshot_timer, xen_vcpu_nr(cpu),
NULL) ||
HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, xen_vcpu_nr(cpu),
NULL))
BUG();
return 0;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Jeremy Fitzhardinge | 29 | 59.18% | 1 | 33.33% |
Viresh Kumar | 14 | 28.57% | 1 | 33.33% |
Vitaly Kuznetsov | 6 | 12.24% | 1 | 33.33% |
Total | 49 | 100.00% | 3 | 100.00% |
static int xen_vcpuop_set_oneshot(struct clock_event_device *evt)
{
int cpu = smp_processor_id();
if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, xen_vcpu_nr(cpu),
NULL))
BUG();
return 0;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Viresh Kumar | 19 | 51.35% | 1 | 33.33% |
Jeremy Fitzhardinge | 15 | 40.54% | 1 | 33.33% |
Vitaly Kuznetsov | 3 | 8.11% | 1 | 33.33% |
Total | 37 | 100.00% | 3 | 100.00% |
static int xen_vcpuop_set_next_event(unsigned long delta,
struct clock_event_device *evt)
{
int cpu = smp_processor_id();
struct vcpu_set_singleshot_timer single;
int ret;
WARN_ON(!clockevent_state_oneshot(evt));
single.timeout_abs_ns = get_abs_timeout(delta);
/* Get an event anyway, even if the timeout is already expired */
single.flags = 0;
ret = HYPERVISOR_vcpu_op(VCPUOP_set_singleshot_timer, xen_vcpu_nr(cpu),
&single);
BUG_ON(ret != 0);
return ret;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Jeremy Fitzhardinge | 69 | 88.46% | 1 | 25.00% |
Viresh Kumar | 4 | 5.13% | 1 | 25.00% |
Vitaly Kuznetsov | 3 | 3.85% | 1 | 25.00% |
Stefano Stabellini | 2 | 2.56% | 1 | 25.00% |
Total | 78 | 100.00% | 4 | 100.00% |
static const struct clock_event_device xen_vcpuop_clockevent = {
.name = "xen",
.features = CLOCK_EVT_FEAT_ONESHOT,
.max_delta_ns = 0xffffffff,
.max_delta_ticks = 0xffffffff,
.min_delta_ns = TIMER_SLOP,
.min_delta_ticks = TIMER_SLOP,
.mult = 1,
.shift = 0,
.rating = 500,
.set_state_shutdown = xen_vcpuop_shutdown,
.set_state_oneshot = xen_vcpuop_set_oneshot,
.set_next_event = xen_vcpuop_set_next_event,
};
static const struct clock_event_device *xen_clockevent =
&xen_timerop_clockevent;
struct xen_clock_event_device {
struct clock_event_device evt;
char name[16];
};
static DEFINE_PER_CPU(struct xen_clock_event_device, xen_clock_events) = { .evt.irq = -1 };
static irqreturn_t xen_timer_interrupt(int irq, void *dev_id)
{
struct clock_event_device *evt = this_cpu_ptr(&xen_clock_events.evt);
irqreturn_t ret;
ret = IRQ_NONE;
if (evt->event_handler) {
evt->event_handler(evt);
ret = IRQ_HANDLED;
}
return ret;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Jeremy Fitzhardinge | 50 | 90.91% | 1 | 33.33% |
Christoph Lameter | 3 | 5.45% | 1 | 33.33% |
Konrad Rzeszutek Wilk | 2 | 3.64% | 1 | 33.33% |
Total | 55 | 100.00% | 3 | 100.00% |
void xen_teardown_timer(int cpu)
{
struct clock_event_device *evt;
evt = &per_cpu(xen_clock_events, cpu).evt;
if (evt->irq >= 0) {
unbind_from_irqhandler(evt->irq, NULL);
evt->irq = -1;
}
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Konrad Rzeszutek Wilk | 51 | 100.00% | 1 | 100.00% |
Total | 51 | 100.00% | 1 | 100.00% |
void xen_setup_timer(int cpu)
{
struct xen_clock_event_device *xevt = &per_cpu(xen_clock_events, cpu);
struct clock_event_device *evt = &xevt->evt;
int irq;
WARN(evt->irq >= 0, "IRQ%d for CPU%d is already allocated\n", evt->irq, cpu);
if (evt->irq >= 0)
xen_teardown_timer(cpu);
printk(KERN_INFO "installing Xen timer for CPU %d\n", cpu);
snprintf(xevt->name, sizeof(xevt->name), "timer%d", cpu);
irq = bind_virq_to_irqhandler(VIRQ_TIMER, cpu, xen_timer_interrupt,
IRQF_PERCPU|IRQF_NOBALANCING|IRQF_TIMER|
IRQF_FORCE_RESUME|IRQF_EARLY_RESUME,
xevt->name, NULL);
(void)xen_set_irq_priority(irq, XEN_IRQ_PRIORITY_MAX);
memcpy(evt, xen_clockevent, sizeof(*evt));
evt->cpumask = cpumask_of(cpu);
evt->irq = irq;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Jeremy Fitzhardinge | 72 | 46.45% | 2 | 18.18% |
Konrad Rzeszutek Wilk | 38 | 24.52% | 3 | 27.27% |
Vitaly Kuznetsov | 28 | 18.06% | 1 | 9.09% |
David Vrabel | 12 | 7.74% | 2 | 18.18% |
Ian Campbell | 4 | 2.58% | 2 | 18.18% |
Rusty Russell | 1 | 0.65% | 1 | 9.09% |
Total | 155 | 100.00% | 11 | 100.00% |
void xen_setup_cpu_clockevents(void)
{
clockevents_register_device(this_cpu_ptr(&xen_clock_events.evt));
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Jeremy Fitzhardinge | 13 | 72.22% | 2 | 50.00% |
Christoph Lameter | 3 | 16.67% | 1 | 25.00% |
Konrad Rzeszutek Wilk | 2 | 11.11% | 1 | 25.00% |
Total | 18 | 100.00% | 4 | 100.00% |
void xen_timer_resume(void)
{
int cpu;
pvclock_resume();
if (xen_clockevent != &xen_vcpuop_clockevent)
return;
for_each_online_cpu(cpu) {
if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer,
xen_vcpu_nr(cpu), NULL))
BUG();
}
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Jeremy Fitzhardinge | 41 | 93.18% | 2 | 66.67% |
Vitaly Kuznetsov | 3 | 6.82% | 1 | 33.33% |
Total | 44 | 100.00% | 3 | 100.00% |
static const struct pv_time_ops xen_time_ops __initconst = {
.sched_clock = xen_clocksource_read,
.steal_clock = xen_steal_clock,
};
static void __init xen_time_init(void)
{
int cpu = smp_processor_id();
struct timespec tp;
/* As Dom0 is never moved, no penalty on using TSC there */
if (xen_initial_domain())
xen_clocksource.rating = 275;
clocksource_register_hz(&xen_clocksource, NSEC_PER_SEC);
if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, xen_vcpu_nr(cpu),
NULL) == 0) {
/* Successfully turned off 100Hz tick, so we have the
vcpuop-based timer interface */
printk(KERN_DEBUG "Xen: using vcpuop timer interface\n");
xen_clockevent = &xen_vcpuop_clockevent;
}
/* Set initial system time with full resolution */
xen_read_wallclock(&tp);
do_settimeofday(&tp);
setup_force_cpu_cap(X86_FEATURE_TSC);
xen_setup_runstate_info(cpu);
xen_setup_timer(cpu);
xen_setup_cpu_clockevents();
xen_time_setup_guest();
if (xen_initial_domain())
pvclock_gtod_register_notifier(&xen_pvclock_gtod_notifier);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Jeremy Fitzhardinge | 64 | 56.14% | 3 | 23.08% |
Imre Palik | 12 | 10.53% | 1 | 7.69% |
David Vrabel | 11 | 9.65% | 1 | 7.69% |
John Stultz | 10 | 8.77% | 2 | 15.38% |
Ian Campbell | 5 | 4.39% | 1 | 7.69% |
Andi Kleen | 4 | 3.51% | 1 | 7.69% |
Vitaly Kuznetsov | 3 | 2.63% | 1 | 7.69% |
Juergen Gross | 3 | 2.63% | 1 | 7.69% |
Stefano Stabellini | 1 | 0.88% | 1 | 7.69% |
Daniel Kiper | 1 | 0.88% | 1 | 7.69% |
Total | 114 | 100.00% | 13 | 100.00% |
void __ref xen_init_time_ops(void)
{
pv_time_ops = xen_time_ops;
x86_init.timers.timer_init = xen_time_init;
x86_init.timers.setup_percpu_clockev = x86_init_noop;
x86_cpuinit.setup_percpu_clockev = x86_init_noop;
x86_platform.calibrate_tsc = xen_tsc_khz;
x86_platform.get_wallclock = xen_get_wallclock;
/* Dom0 uses the native method to set the hardware RTC. */
if (!xen_initial_domain())
x86_platform.set_wallclock = xen_set_wallclock;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Stefano Stabellini | 51 | 86.44% | 1 | 33.33% |
David Vrabel | 7 | 11.86% | 1 | 33.33% |
Boris Ostrovsky | 1 | 1.69% | 1 | 33.33% |
Total | 59 | 100.00% | 3 | 100.00% |
#ifdef CONFIG_XEN_PVHVM
static void xen_hvm_setup_cpu_clockevents(void)
{
int cpu = smp_processor_id();
xen_setup_runstate_info(cpu);
/*
* xen_setup_timer(cpu) - snprintf is bad in atomic context. Hence
* doing it xen_hvm_cpu_notify (which gets called by smp_init during
* early bootup and also during CPU hotplug events).
*/
xen_setup_cpu_clockevents();
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Stefano Stabellini | 22 | 95.65% | 1 | 50.00% |
Konrad Rzeszutek Wilk | 1 | 4.35% | 1 | 50.00% |
Total | 23 | 100.00% | 2 | 100.00% |
void __init xen_hvm_init_time_ops(void)
{
/*
* vector callback is needed otherwise we cannot receive interrupts
* on cpu > 0 and at this point we don't know how many cpus are
* available.
*/
if (!xen_have_vector_callback)
return;
if (!xen_feature(XENFEAT_hvm_safe_pvclock)) {
printk(KERN_INFO "Xen doesn't support pvclock on HVM,"
"disable pv timer\n");
return;
}
pv_time_ops = xen_time_ops;
x86_init.timers.setup_percpu_clockev = xen_time_init;
x86_cpuinit.setup_percpu_clockev = xen_hvm_setup_cpu_clockevents;
x86_platform.calibrate_tsc = xen_tsc_khz;
x86_platform.get_wallclock = xen_get_wallclock;
x86_platform.set_wallclock = xen_set_wallclock;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Stefano Stabellini | 61 | 88.41% | 1 | 33.33% |
Boris Ostrovsky | 7 | 10.14% | 1 | 33.33% |
Daniel Kiper | 1 | 1.45% | 1 | 33.33% |
Total | 69 | 100.00% | 3 | 100.00% |
#endif
Overall Contributors
Person | Tokens | Prop | Commits | CommitProp |
Jeremy Fitzhardinge | 779 | 49.84% | 8 | 15.69% |
Stefano Stabellini | 304 | 19.45% | 7 | 13.73% |
Konrad Rzeszutek Wilk | 120 | 7.68% | 5 | 9.80% |
David Vrabel | 109 | 6.97% | 5 | 9.80% |
Viresh Kumar | 56 | 3.58% | 1 | 1.96% |
Vitaly Kuznetsov | 49 | 3.13% | 2 | 3.92% |
Gerd Hoffmann | 35 | 2.24% | 1 | 1.96% |
Nicolai Stange | 20 | 1.28% | 1 | 1.96% |
Magnus Damm | 15 | 0.96% | 1 | 1.96% |
Imre Palik | 12 | 0.77% | 1 | 1.96% |
Boris Ostrovsky | 11 | 0.70% | 3 | 5.88% |
John Stultz | 10 | 0.64% | 2 | 3.92% |
Ian Campbell | 9 | 0.58% | 3 | 5.88% |
Juergen Gross | 8 | 0.51% | 2 | 3.92% |
Christoph Lameter | 7 | 0.45% | 1 | 1.96% |
Andi Kleen | 4 | 0.26% | 1 | 1.96% |
Thomas Gleixner | 3 | 0.19% | 1 | 1.96% |
Tejun Heo | 3 | 0.19% | 1 | 1.96% |
Daniel Kiper | 3 | 0.19% | 1 | 1.96% |
Glauber de Oliveira Costa | 2 | 0.13% | 1 | 1.96% |
Alok N Kataria | 2 | 0.13% | 1 | 1.96% |
Rusty Russell | 1 | 0.06% | 1 | 1.96% |
Greg Kroah-Hartman | 1 | 0.06% | 1 | 1.96% |
Total | 1563 | 100.00% | 51 | 100.00% |
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.