time.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * Xen time implementation.
  3. *
  4. * This is implemented in terms of a clocksource driver which uses
  5. * the hypervisor clock as a nanosecond timebase, and a clockevent
  6. * driver which uses the hypervisor's timer mechanism.
  7. *
  8. * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/clocksource.h>
  13. #include <linux/clockchips.h>
  14. #include <linux/gfp.h>
  15. #include <linux/slab.h>
  16. #include <linux/pvclock_gtod.h>
  17. #include <linux/timekeeper_internal.h>
  18. #include <asm/pvclock.h>
  19. #include <asm/xen/hypervisor.h>
  20. #include <asm/xen/hypercall.h>
  21. #include <xen/events.h>
  22. #include <xen/features.h>
  23. #include <xen/interface/xen.h>
  24. #include <xen/interface/vcpu.h>
  25. #include "xen-ops.h"
  26. /* Xen may fire a timer up to this many ns early */
  27. #define TIMER_SLOP 100000
  28. /* Get the TSC speed from Xen */
  29. static unsigned long xen_tsc_khz(void)
  30. {
  31. struct pvclock_vcpu_time_info *info =
  32. &HYPERVISOR_shared_info->vcpu_info[0].time;
  33. return pvclock_tsc_khz(info);
  34. }
  35. cycle_t xen_clocksource_read(void)
  36. {
  37. struct pvclock_vcpu_time_info *src;
  38. cycle_t ret;
  39. preempt_disable_notrace();
  40. src = &__this_cpu_read(xen_vcpu)->time;
  41. ret = pvclock_clocksource_read(src);
  42. preempt_enable_notrace();
  43. return ret;
  44. }
  45. static cycle_t xen_clocksource_get_cycles(struct clocksource *cs)
  46. {
  47. return xen_clocksource_read();
  48. }
  49. static void xen_read_wallclock(struct timespec *ts)
  50. {
  51. struct shared_info *s = HYPERVISOR_shared_info;
  52. struct pvclock_wall_clock *wall_clock = &(s->wc);
  53. struct pvclock_vcpu_time_info *vcpu_time;
  54. vcpu_time = &get_cpu_var(xen_vcpu)->time;
  55. pvclock_read_wallclock(wall_clock, vcpu_time, ts);
  56. put_cpu_var(xen_vcpu);
  57. }
  58. static void xen_get_wallclock(struct timespec *now)
  59. {
  60. xen_read_wallclock(now);
  61. }
  62. static int xen_set_wallclock(const struct timespec *now)
  63. {
  64. return -1;
  65. }
  66. static int xen_pvclock_gtod_notify(struct notifier_block *nb,
  67. unsigned long was_set, void *priv)
  68. {
  69. /* Protected by the calling core code serialization */
  70. static struct timespec64 next_sync;
  71. struct xen_platform_op op;
  72. struct timespec64 now;
  73. struct timekeeper *tk = priv;
  74. static bool settime64_supported = true;
  75. int ret;
  76. now.tv_sec = tk->xtime_sec;
  77. now.tv_nsec = (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
  78. /*
  79. * We only take the expensive HV call when the clock was set
  80. * or when the 11 minutes RTC synchronization time elapsed.
  81. */
  82. if (!was_set && timespec64_compare(&now, &next_sync) < 0)
  83. return NOTIFY_OK;
  84. again:
  85. if (settime64_supported) {
  86. op.cmd = XENPF_settime64;
  87. op.u.settime64.mbz = 0;
  88. op.u.settime64.secs = now.tv_sec;
  89. op.u.settime64.nsecs = now.tv_nsec;
  90. op.u.settime64.system_time = xen_clocksource_read();
  91. } else {
  92. op.cmd = XENPF_settime32;
  93. op.u.settime32.secs = now.tv_sec;
  94. op.u.settime32.nsecs = now.tv_nsec;
  95. op.u.settime32.system_time = xen_clocksource_read();
  96. }
  97. ret = HYPERVISOR_platform_op(&op);
  98. if (ret == -ENOSYS && settime64_supported) {
  99. settime64_supported = false;
  100. goto again;
  101. }
  102. if (ret < 0)
  103. return NOTIFY_BAD;
  104. /*
  105. * Move the next drift compensation time 11 minutes
  106. * ahead. That's emulating the sync_cmos_clock() update for
  107. * the hardware RTC.
  108. */
  109. next_sync = now;
  110. next_sync.tv_sec += 11 * 60;
  111. return NOTIFY_OK;
  112. }
  113. static struct notifier_block xen_pvclock_gtod_notifier = {
  114. .notifier_call = xen_pvclock_gtod_notify,
  115. };
  116. static struct clocksource xen_clocksource __read_mostly = {
  117. .name = "xen",
  118. .rating = 400,
  119. .read = xen_clocksource_get_cycles,
  120. .mask = ~0,
  121. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  122. };
  123. /*
  124. Xen clockevent implementation
  125. Xen has two clockevent implementations:
  126. The old timer_op one works with all released versions of Xen prior
  127. to version 3.0.4. This version of the hypervisor provides a
  128. single-shot timer with nanosecond resolution. However, sharing the
  129. same event channel is a 100Hz tick which is delivered while the
  130. vcpu is running. We don't care about or use this tick, but it will
  131. cause the core time code to think the timer fired too soon, and
  132. will end up resetting it each time. It could be filtered, but
  133. doing so has complications when the ktime clocksource is not yet
  134. the xen clocksource (ie, at boot time).
  135. The new vcpu_op-based timer interface allows the tick timer period
  136. to be changed or turned off. The tick timer is not useful as a
  137. periodic timer because events are only delivered to running vcpus.
  138. The one-shot timer can report when a timeout is in the past, so
  139. set_next_event is capable of returning -ETIME when appropriate.
  140. This interface is used when available.
  141. */
  142. /*
  143. Get a hypervisor absolute time. In theory we could maintain an
  144. offset between the kernel's time and the hypervisor's time, and
  145. apply that to a kernel's absolute timeout. Unfortunately the
  146. hypervisor and kernel times can drift even if the kernel is using
  147. the Xen clocksource, because ntp can warp the kernel's clocksource.
  148. */
  149. static s64 get_abs_timeout(unsigned long delta)
  150. {
  151. return xen_clocksource_read() + delta;
  152. }
  153. static int xen_timerop_shutdown(struct clock_event_device *evt)
  154. {
  155. /* cancel timeout */
  156. HYPERVISOR_set_timer_op(0);
  157. return 0;
  158. }
  159. static int xen_timerop_set_next_event(unsigned long delta,
  160. struct clock_event_device *evt)
  161. {
  162. WARN_ON(!clockevent_state_oneshot(evt));
  163. if (HYPERVISOR_set_timer_op(get_abs_timeout(delta)) < 0)
  164. BUG();
  165. /* We may have missed the deadline, but there's no real way of
  166. knowing for sure. If the event was in the past, then we'll
  167. get an immediate interrupt. */
  168. return 0;
  169. }
  170. static const struct clock_event_device xen_timerop_clockevent = {
  171. .name = "xen",
  172. .features = CLOCK_EVT_FEAT_ONESHOT,
  173. .max_delta_ns = 0xffffffff,
  174. .min_delta_ns = TIMER_SLOP,
  175. .mult = 1,
  176. .shift = 0,
  177. .rating = 500,
  178. .set_state_shutdown = xen_timerop_shutdown,
  179. .set_next_event = xen_timerop_set_next_event,
  180. };
  181. static int xen_vcpuop_shutdown(struct clock_event_device *evt)
  182. {
  183. int cpu = smp_processor_id();
  184. if (HYPERVISOR_vcpu_op(VCPUOP_stop_singleshot_timer, xen_vcpu_nr(cpu),
  185. NULL) ||
  186. HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, xen_vcpu_nr(cpu),
  187. NULL))
  188. BUG();
  189. return 0;
  190. }
  191. static int xen_vcpuop_set_oneshot(struct clock_event_device *evt)
  192. {
  193. int cpu = smp_processor_id();
  194. if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, xen_vcpu_nr(cpu),
  195. NULL))
  196. BUG();
  197. return 0;
  198. }
  199. static int xen_vcpuop_set_next_event(unsigned long delta,
  200. struct clock_event_device *evt)
  201. {
  202. int cpu = smp_processor_id();
  203. struct vcpu_set_singleshot_timer single;
  204. int ret;
  205. WARN_ON(!clockevent_state_oneshot(evt));
  206. single.timeout_abs_ns = get_abs_timeout(delta);
  207. /* Get an event anyway, even if the timeout is already expired */
  208. single.flags = 0;
  209. ret = HYPERVISOR_vcpu_op(VCPUOP_set_singleshot_timer, xen_vcpu_nr(cpu),
  210. &single);
  211. BUG_ON(ret != 0);
  212. return ret;
  213. }
  214. static const struct clock_event_device xen_vcpuop_clockevent = {
  215. .name = "xen",
  216. .features = CLOCK_EVT_FEAT_ONESHOT,
  217. .max_delta_ns = 0xffffffff,
  218. .min_delta_ns = TIMER_SLOP,
  219. .mult = 1,
  220. .shift = 0,
  221. .rating = 500,
  222. .set_state_shutdown = xen_vcpuop_shutdown,
  223. .set_state_oneshot = xen_vcpuop_set_oneshot,
  224. .set_next_event = xen_vcpuop_set_next_event,
  225. };
  226. static const struct clock_event_device *xen_clockevent =
  227. &xen_timerop_clockevent;
  228. struct xen_clock_event_device {
  229. struct clock_event_device evt;
  230. char name[16];
  231. };
  232. static DEFINE_PER_CPU(struct xen_clock_event_device, xen_clock_events) = { .evt.irq = -1 };
  233. static irqreturn_t xen_timer_interrupt(int irq, void *dev_id)
  234. {
  235. struct clock_event_device *evt = this_cpu_ptr(&xen_clock_events.evt);
  236. irqreturn_t ret;
  237. ret = IRQ_NONE;
  238. if (evt->event_handler) {
  239. evt->event_handler(evt);
  240. ret = IRQ_HANDLED;
  241. }
  242. return ret;
  243. }
  244. void xen_teardown_timer(int cpu)
  245. {
  246. struct clock_event_device *evt;
  247. BUG_ON(cpu == 0);
  248. evt = &per_cpu(xen_clock_events, cpu).evt;
  249. if (evt->irq >= 0) {
  250. unbind_from_irqhandler(evt->irq, NULL);
  251. evt->irq = -1;
  252. }
  253. }
  254. void xen_setup_timer(int cpu)
  255. {
  256. struct xen_clock_event_device *xevt = &per_cpu(xen_clock_events, cpu);
  257. struct clock_event_device *evt = &xevt->evt;
  258. int irq;
  259. WARN(evt->irq >= 0, "IRQ%d for CPU%d is already allocated\n", evt->irq, cpu);
  260. if (evt->irq >= 0)
  261. xen_teardown_timer(cpu);
  262. printk(KERN_INFO "installing Xen timer for CPU %d\n", cpu);
  263. snprintf(xevt->name, sizeof(xevt->name), "timer%d", cpu);
  264. irq = bind_virq_to_irqhandler(VIRQ_TIMER, cpu, xen_timer_interrupt,
  265. IRQF_PERCPU|IRQF_NOBALANCING|IRQF_TIMER|
  266. IRQF_FORCE_RESUME|IRQF_EARLY_RESUME,
  267. xevt->name, NULL);
  268. (void)xen_set_irq_priority(irq, XEN_IRQ_PRIORITY_MAX);
  269. memcpy(evt, xen_clockevent, sizeof(*evt));
  270. evt->cpumask = cpumask_of(cpu);
  271. evt->irq = irq;
  272. }
  273. void xen_setup_cpu_clockevents(void)
  274. {
  275. clockevents_register_device(this_cpu_ptr(&xen_clock_events.evt));
  276. }
  277. void xen_timer_resume(void)
  278. {
  279. int cpu;
  280. pvclock_resume();
  281. if (xen_clockevent != &xen_vcpuop_clockevent)
  282. return;
  283. for_each_online_cpu(cpu) {
  284. if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer,
  285. xen_vcpu_nr(cpu), NULL))
  286. BUG();
  287. }
  288. }
  289. static const struct pv_time_ops xen_time_ops __initconst = {
  290. .sched_clock = xen_clocksource_read,
  291. .steal_clock = xen_steal_clock,
  292. };
  293. static void __init xen_time_init(void)
  294. {
  295. int cpu = smp_processor_id();
  296. struct timespec tp;
  297. /* As Dom0 is never moved, no penalty on using TSC there */
  298. if (xen_initial_domain())
  299. xen_clocksource.rating = 275;
  300. clocksource_register_hz(&xen_clocksource, NSEC_PER_SEC);
  301. if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, xen_vcpu_nr(cpu),
  302. NULL) == 0) {
  303. /* Successfully turned off 100Hz tick, so we have the
  304. vcpuop-based timer interface */
  305. printk(KERN_DEBUG "Xen: using vcpuop timer interface\n");
  306. xen_clockevent = &xen_vcpuop_clockevent;
  307. }
  308. /* Set initial system time with full resolution */
  309. xen_read_wallclock(&tp);
  310. do_settimeofday(&tp);
  311. setup_force_cpu_cap(X86_FEATURE_TSC);
  312. xen_setup_runstate_info(cpu);
  313. xen_setup_timer(cpu);
  314. xen_setup_cpu_clockevents();
  315. xen_time_setup_guest();
  316. if (xen_initial_domain())
  317. pvclock_gtod_register_notifier(&xen_pvclock_gtod_notifier);
  318. }
  319. void __init xen_init_time_ops(void)
  320. {
  321. pv_time_ops = xen_time_ops;
  322. x86_init.timers.timer_init = xen_time_init;
  323. x86_init.timers.setup_percpu_clockev = x86_init_noop;
  324. x86_cpuinit.setup_percpu_clockev = x86_init_noop;
  325. x86_platform.calibrate_tsc = xen_tsc_khz;
  326. x86_platform.get_wallclock = xen_get_wallclock;
  327. /* Dom0 uses the native method to set the hardware RTC. */
  328. if (!xen_initial_domain())
  329. x86_platform.set_wallclock = xen_set_wallclock;
  330. }
  331. #ifdef CONFIG_XEN_PVHVM
  332. static void xen_hvm_setup_cpu_clockevents(void)
  333. {
  334. int cpu = smp_processor_id();
  335. xen_setup_runstate_info(cpu);
  336. /*
  337. * xen_setup_timer(cpu) - snprintf is bad in atomic context. Hence
  338. * doing it xen_hvm_cpu_notify (which gets called by smp_init during
  339. * early bootup and also during CPU hotplug events).
  340. */
  341. xen_setup_cpu_clockevents();
  342. }
  343. void __init xen_hvm_init_time_ops(void)
  344. {
  345. /* vector callback is needed otherwise we cannot receive interrupts
  346. * on cpu > 0 and at this point we don't know how many cpus are
  347. * available */
  348. if (!xen_have_vector_callback)
  349. return;
  350. if (!xen_feature(XENFEAT_hvm_safe_pvclock)) {
  351. printk(KERN_INFO "Xen doesn't support pvclock on HVM,"
  352. "disable pv timer\n");
  353. return;
  354. }
  355. pv_time_ops = xen_time_ops;
  356. x86_init.timers.setup_percpu_clockev = xen_time_init;
  357. x86_cpuinit.setup_percpu_clockev = xen_hvm_setup_cpu_clockevents;
  358. x86_platform.calibrate_tsc = xen_tsc_khz;
  359. x86_platform.get_wallclock = xen_get_wallclock;
  360. x86_platform.set_wallclock = xen_set_wallclock;
  361. }
  362. #endif