time.c 14 KB

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