Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Xen time implementation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * This is implemented in terms of a clocksource driver which uses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * the hypervisor clock as a nanosecond timebase, and a clockevent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * driver which uses the hypervisor's timer mechanism.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/clocksource.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/clockchips.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/pvclock_gtod.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/timekeeper_internal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <asm/pvclock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <asm/xen/hypervisor.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <asm/xen/hypercall.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <xen/events.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <xen/features.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <xen/interface/xen.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <xen/interface/vcpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include "xen-ops.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) /* Minimum amount of time until next clock event fires */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define TIMER_SLOP	100000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) static u64 xen_sched_clock_offset __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) /* Get the TSC speed from Xen */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) static unsigned long xen_tsc_khz(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	struct pvclock_vcpu_time_info *info =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		&HYPERVISOR_shared_info->vcpu_info[0].time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	return pvclock_tsc_khz(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) static u64 xen_clocksource_read(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)         struct pvclock_vcpu_time_info *src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	u64 ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	preempt_disable_notrace();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	src = &__this_cpu_read(xen_vcpu)->time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	ret = pvclock_clocksource_read(src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	preempt_enable_notrace();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) static u64 xen_clocksource_get_cycles(struct clocksource *cs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	return xen_clocksource_read();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) static u64 xen_sched_clock(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	return xen_clocksource_read() - xen_sched_clock_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) static void xen_read_wallclock(struct timespec64 *ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	struct shared_info *s = HYPERVISOR_shared_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	struct pvclock_wall_clock *wall_clock = &(s->wc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72)         struct pvclock_vcpu_time_info *vcpu_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	vcpu_time = &get_cpu_var(xen_vcpu)->time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	pvclock_read_wallclock(wall_clock, vcpu_time, ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	put_cpu_var(xen_vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) static void xen_get_wallclock(struct timespec64 *now)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	xen_read_wallclock(now);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) static int xen_set_wallclock(const struct timespec64 *now)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) static int xen_pvclock_gtod_notify(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 				   unsigned long was_set, void *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	/* Protected by the calling core code serialization */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	static struct timespec64 next_sync;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	struct xen_platform_op op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	struct timespec64 now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	struct timekeeper *tk = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	static bool settime64_supported = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	now.tv_sec = tk->xtime_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	now.tv_nsec = (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	 * We only take the expensive HV call when the clock was set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	 * or when the 11 minutes RTC synchronization time elapsed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	if (!was_set && timespec64_compare(&now, &next_sync) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	if (settime64_supported) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		op.cmd = XENPF_settime64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		op.u.settime64.mbz = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		op.u.settime64.secs = now.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		op.u.settime64.nsecs = now.tv_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		op.u.settime64.system_time = xen_clocksource_read();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		op.cmd = XENPF_settime32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		op.u.settime32.secs = now.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		op.u.settime32.nsecs = now.tv_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		op.u.settime32.system_time = xen_clocksource_read();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	ret = HYPERVISOR_platform_op(&op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	if (ret == -ENOSYS && settime64_supported) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		settime64_supported = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		return NOTIFY_BAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	 * Move the next drift compensation time 11 minutes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	 * ahead. That's emulating the sync_cmos_clock() update for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	 * the hardware RTC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	next_sync = now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	next_sync.tv_sec += 11 * 60;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static struct notifier_block xen_pvclock_gtod_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	.notifier_call = xen_pvclock_gtod_notify,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static int xen_cs_enable(struct clocksource *cs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	vclocks_set_used(VDSO_CLOCKMODE_PVCLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static struct clocksource xen_clocksource __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	.name	= "xen",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	.rating	= 400,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	.read	= xen_clocksource_get_cycles,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	.mask	= CLOCKSOURCE_MASK(64),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	.flags	= CLOCK_SOURCE_IS_CONTINUOUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	.enable = xen_cs_enable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)    Xen clockevent implementation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)    Xen has two clockevent implementations:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)    The old timer_op one works with all released versions of Xen prior
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)    to version 3.0.4.  This version of the hypervisor provides a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)    single-shot timer with nanosecond resolution.  However, sharing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)    same event channel is a 100Hz tick which is delivered while the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)    vcpu is running.  We don't care about or use this tick, but it will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)    cause the core time code to think the timer fired too soon, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)    will end up resetting it each time.  It could be filtered, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)    doing so has complications when the ktime clocksource is not yet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)    the xen clocksource (ie, at boot time).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)    The new vcpu_op-based timer interface allows the tick timer period
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)    to be changed or turned off.  The tick timer is not useful as a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)    periodic timer because events are only delivered to running vcpus.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)    The one-shot timer can report when a timeout is in the past, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)    set_next_event is capable of returning -ETIME when appropriate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)    This interface is used when available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)   Get a hypervisor absolute time.  In theory we could maintain an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)   offset between the kernel's time and the hypervisor's time, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)   apply that to a kernel's absolute timeout.  Unfortunately the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)   hypervisor and kernel times can drift even if the kernel is using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)   the Xen clocksource, because ntp can warp the kernel's clocksource.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) static s64 get_abs_timeout(unsigned long delta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	return xen_clocksource_read() + delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) static int xen_timerop_shutdown(struct clock_event_device *evt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	/* cancel timeout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	HYPERVISOR_set_timer_op(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) static int xen_timerop_set_next_event(unsigned long delta,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 				      struct clock_event_device *evt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	WARN_ON(!clockevent_state_oneshot(evt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	if (HYPERVISOR_set_timer_op(get_abs_timeout(delta)) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	/* We may have missed the deadline, but there's no real way of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	   knowing for sure.  If the event was in the past, then we'll
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	   get an immediate interrupt. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) static struct clock_event_device xen_timerop_clockevent __ro_after_init = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	.name			= "xen",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	.features		= CLOCK_EVT_FEAT_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	.max_delta_ns		= 0xffffffff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	.max_delta_ticks	= 0xffffffff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	.min_delta_ns		= TIMER_SLOP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	.min_delta_ticks	= TIMER_SLOP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	.mult			= 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	.shift			= 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	.rating			= 500,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	.set_state_shutdown	= xen_timerop_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	.set_next_event		= xen_timerop_set_next_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) static int xen_vcpuop_shutdown(struct clock_event_device *evt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	int cpu = smp_processor_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	if (HYPERVISOR_vcpu_op(VCPUOP_stop_singleshot_timer, xen_vcpu_nr(cpu),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 			       NULL) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	    HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, xen_vcpu_nr(cpu),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 			       NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) static int xen_vcpuop_set_oneshot(struct clock_event_device *evt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	int cpu = smp_processor_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, xen_vcpu_nr(cpu),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			       NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) static int xen_vcpuop_set_next_event(unsigned long delta,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 				     struct clock_event_device *evt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	int cpu = smp_processor_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	struct vcpu_set_singleshot_timer single;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	WARN_ON(!clockevent_state_oneshot(evt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	single.timeout_abs_ns = get_abs_timeout(delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	/* Get an event anyway, even if the timeout is already expired */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	single.flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	ret = HYPERVISOR_vcpu_op(VCPUOP_set_singleshot_timer, xen_vcpu_nr(cpu),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 				 &single);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	BUG_ON(ret != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) static struct clock_event_device xen_vcpuop_clockevent __ro_after_init = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	.name = "xen",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	.features = CLOCK_EVT_FEAT_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	.max_delta_ns = 0xffffffff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	.max_delta_ticks = 0xffffffff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	.min_delta_ns = TIMER_SLOP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	.min_delta_ticks = TIMER_SLOP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	.mult = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	.shift = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	.rating = 500,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	.set_state_shutdown = xen_vcpuop_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	.set_state_oneshot = xen_vcpuop_set_oneshot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	.set_next_event = xen_vcpuop_set_next_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) static const struct clock_event_device *xen_clockevent =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	&xen_timerop_clockevent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) struct xen_clock_event_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	struct clock_event_device evt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	char name[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) static DEFINE_PER_CPU(struct xen_clock_event_device, xen_clock_events) = { .evt.irq = -1 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) static irqreturn_t xen_timer_interrupt(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	struct clock_event_device *evt = this_cpu_ptr(&xen_clock_events.evt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	irqreturn_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	ret = IRQ_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	if (evt->event_handler) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		evt->event_handler(evt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		ret = IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) void xen_teardown_timer(int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	struct clock_event_device *evt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	evt = &per_cpu(xen_clock_events, cpu).evt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	if (evt->irq >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		unbind_from_irqhandler(evt->irq, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		evt->irq = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) void xen_setup_timer(int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	struct xen_clock_event_device *xevt = &per_cpu(xen_clock_events, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	struct clock_event_device *evt = &xevt->evt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	WARN(evt->irq >= 0, "IRQ%d for CPU%d is already allocated\n", evt->irq, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	if (evt->irq >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		xen_teardown_timer(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	printk(KERN_INFO "installing Xen timer for CPU %d\n", cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	snprintf(xevt->name, sizeof(xevt->name), "timer%d", cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	irq = bind_virq_to_irqhandler(VIRQ_TIMER, cpu, xen_timer_interrupt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 				      IRQF_PERCPU|IRQF_NOBALANCING|IRQF_TIMER|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 				      IRQF_FORCE_RESUME|IRQF_EARLY_RESUME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 				      xevt->name, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	(void)xen_set_irq_priority(irq, XEN_IRQ_PRIORITY_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	memcpy(evt, xen_clockevent, sizeof(*evt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	evt->cpumask = cpumask_of(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	evt->irq = irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) void xen_setup_cpu_clockevents(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	clockevents_register_device(this_cpu_ptr(&xen_clock_events.evt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) void xen_timer_resume(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	if (xen_clockevent != &xen_vcpuop_clockevent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	for_each_online_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 				       xen_vcpu_nr(cpu), NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 			BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) static const struct pv_time_ops xen_time_ops __initconst = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	.sched_clock = xen_sched_clock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	.steal_clock = xen_steal_clock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) static struct pvclock_vsyscall_time_info *xen_clock __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) static u64 xen_clock_value_saved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) void xen_save_time_memory_area(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	struct vcpu_register_time_memory_area t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	xen_clock_value_saved = xen_clocksource_read() - xen_sched_clock_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	if (!xen_clock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	t.addr.v = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	ret = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_time_memory_area, 0, &t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		pr_notice("Cannot save secondary vcpu_time_info (err %d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 			  ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		clear_page(xen_clock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) void xen_restore_time_memory_area(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	struct vcpu_register_time_memory_area t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	if (!xen_clock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	t.addr.v = &xen_clock->pvti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	ret = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_time_memory_area, 0, &t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	 * We don't disable VDSO_CLOCKMODE_PVCLOCK entirely if it fails to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	 * register the secondary time info with Xen or if we migrated to a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	 * host without the necessary flags. On both of these cases what
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	 * happens is either process seeing a zeroed out pvti or seeing no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	 * PVCLOCK_TSC_STABLE_BIT bit set. Userspace checks the latter and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	 * if 0, it discards the data in pvti and fallbacks to a system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	 * call for a reliable timestamp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 		pr_notice("Cannot restore secondary vcpu_time_info (err %d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 			  ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	/* Need pvclock_resume() before using xen_clocksource_read(). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	pvclock_resume();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	xen_sched_clock_offset = xen_clocksource_read() - xen_clock_value_saved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) static void xen_setup_vsyscall_time_info(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	struct vcpu_register_time_memory_area t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	struct pvclock_vsyscall_time_info *ti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	ti = (struct pvclock_vsyscall_time_info *)get_zeroed_page(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	if (!ti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	t.addr.v = &ti->pvti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	ret = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_time_memory_area, 0, &t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		pr_notice("xen: VDSO_CLOCKMODE_PVCLOCK not supported (err %d)\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		free_page((unsigned long)ti);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	 * If primary time info had this bit set, secondary should too since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	 * it's the same data on both just different memory regions. But we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	 * still check it in case hypervisor is buggy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	if (!(ti->pvti.flags & PVCLOCK_TSC_STABLE_BIT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 		t.addr.v = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		ret = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_time_memory_area,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 					 0, &t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 			free_page((unsigned long)ti);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		pr_notice("xen: VDSO_CLOCKMODE_PVCLOCK not supported (tsc unstable)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	xen_clock = ti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	pvclock_set_pvti_cpu0_va(xen_clock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	xen_clocksource.vdso_clock_mode = VDSO_CLOCKMODE_PVCLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) static void __init xen_time_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	struct pvclock_vcpu_time_info *pvti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	int cpu = smp_processor_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	struct timespec64 tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	/* As Dom0 is never moved, no penalty on using TSC there */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	if (xen_initial_domain())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		xen_clocksource.rating = 275;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	clocksource_register_hz(&xen_clocksource, NSEC_PER_SEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, xen_vcpu_nr(cpu),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 			       NULL) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		/* Successfully turned off 100Hz tick, so we have the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 		   vcpuop-based timer interface */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 		printk(KERN_DEBUG "Xen: using vcpuop timer interface\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 		xen_clockevent = &xen_vcpuop_clockevent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	/* Set initial system time with full resolution */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	xen_read_wallclock(&tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	do_settimeofday64(&tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	setup_force_cpu_cap(X86_FEATURE_TSC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	 * We check ahead on the primary time info if this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	 * bit is supported hence speeding up Xen clocksource.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	pvti = &__this_cpu_read(xen_vcpu)->time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	if (pvti->flags & PVCLOCK_TSC_STABLE_BIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		pvclock_set_flags(PVCLOCK_TSC_STABLE_BIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 		xen_setup_vsyscall_time_info();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	xen_setup_runstate_info(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	xen_setup_timer(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	xen_setup_cpu_clockevents();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	xen_time_setup_guest();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	if (xen_initial_domain())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		pvclock_gtod_register_notifier(&xen_pvclock_gtod_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) void __init xen_init_time_ops(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	xen_sched_clock_offset = xen_clocksource_read();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	pv_ops.time = xen_time_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	x86_init.timers.timer_init = xen_time_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	x86_init.timers.setup_percpu_clockev = x86_init_noop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	x86_cpuinit.setup_percpu_clockev = x86_init_noop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	x86_platform.calibrate_tsc = xen_tsc_khz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	x86_platform.get_wallclock = xen_get_wallclock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	/* Dom0 uses the native method to set the hardware RTC. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	if (!xen_initial_domain())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 		x86_platform.set_wallclock = xen_set_wallclock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) #ifdef CONFIG_XEN_PVHVM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) static void xen_hvm_setup_cpu_clockevents(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	int cpu = smp_processor_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	xen_setup_runstate_info(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	 * xen_setup_timer(cpu) - snprintf is bad in atomic context. Hence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	 * doing it xen_hvm_cpu_notify (which gets called by smp_init during
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	 * early bootup and also during CPU hotplug events).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	xen_setup_cpu_clockevents();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) void __init xen_hvm_init_time_ops(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	 * vector callback is needed otherwise we cannot receive interrupts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	 * on cpu > 0 and at this point we don't know how many cpus are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	 * available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	if (!xen_have_vector_callback)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	if (!xen_feature(XENFEAT_hvm_safe_pvclock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 		pr_info("Xen doesn't support pvclock on HVM, disable pv timer");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	xen_sched_clock_offset = xen_clocksource_read();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	pv_ops.time = xen_time_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	x86_init.timers.setup_percpu_clockev = xen_time_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	x86_cpuinit.setup_percpu_clockev = xen_hvm_setup_cpu_clockevents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	x86_platform.calibrate_tsc = xen_tsc_khz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	x86_platform.get_wallclock = xen_get_wallclock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	x86_platform.set_wallclock = xen_set_wallclock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) /* Kernel parameter to specify Xen timer slop */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) static int __init parse_xen_timer_slop(char *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	unsigned long slop = memparse(ptr, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	xen_timerop_clockevent.min_delta_ns = slop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	xen_timerop_clockevent.min_delta_ticks = slop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	xen_vcpuop_clockevent.min_delta_ns = slop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	xen_vcpuop_clockevent.min_delta_ticks = slop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) early_param("xen_timer_slop", parse_xen_timer_slop);