time.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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/kernel_stat.h>
  15. #include <linux/math64.h>
  16. #include <linux/gfp.h>
  17. #include <linux/slab.h>
  18. #include <linux/pvclock_gtod.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. #define NS_PER_TICK (1000000000LL / HZ)
  30. /* runstate info updated by Xen */
  31. static DEFINE_PER_CPU(struct vcpu_runstate_info, xen_runstate);
  32. /* snapshots of runstate info */
  33. static DEFINE_PER_CPU(struct vcpu_runstate_info, xen_runstate_snapshot);
  34. /* unused ns of stolen time */
  35. static DEFINE_PER_CPU(u64, xen_residual_stolen);
  36. /* return an consistent snapshot of 64-bit time/counter value */
  37. static u64 get64(const u64 *p)
  38. {
  39. u64 ret;
  40. if (BITS_PER_LONG < 64) {
  41. u32 *p32 = (u32 *)p;
  42. u32 h, l;
  43. /*
  44. * Read high then low, and then make sure high is
  45. * still the same; this will only loop if low wraps
  46. * and carries into high.
  47. * XXX some clean way to make this endian-proof?
  48. */
  49. do {
  50. h = p32[1];
  51. barrier();
  52. l = p32[0];
  53. barrier();
  54. } while (p32[1] != h);
  55. ret = (((u64)h) << 32) | l;
  56. } else
  57. ret = *p;
  58. return ret;
  59. }
  60. /*
  61. * Runstate accounting
  62. */
  63. static void get_runstate_snapshot(struct vcpu_runstate_info *res)
  64. {
  65. u64 state_time;
  66. struct vcpu_runstate_info *state;
  67. BUG_ON(preemptible());
  68. state = &__get_cpu_var(xen_runstate);
  69. /*
  70. * The runstate info is always updated by the hypervisor on
  71. * the current CPU, so there's no need to use anything
  72. * stronger than a compiler barrier when fetching it.
  73. */
  74. do {
  75. state_time = get64(&state->state_entry_time);
  76. barrier();
  77. *res = *state;
  78. barrier();
  79. } while (get64(&state->state_entry_time) != state_time);
  80. }
  81. /* return true when a vcpu could run but has no real cpu to run on */
  82. bool xen_vcpu_stolen(int vcpu)
  83. {
  84. return per_cpu(xen_runstate, vcpu).state == RUNSTATE_runnable;
  85. }
  86. void xen_setup_runstate_info(int cpu)
  87. {
  88. struct vcpu_register_runstate_memory_area area;
  89. area.addr.v = &per_cpu(xen_runstate, cpu);
  90. if (HYPERVISOR_vcpu_op(VCPUOP_register_runstate_memory_area,
  91. cpu, &area))
  92. BUG();
  93. }
  94. static void do_stolen_accounting(void)
  95. {
  96. struct vcpu_runstate_info state;
  97. struct vcpu_runstate_info *snap;
  98. s64 runnable, offline, stolen;
  99. cputime_t ticks;
  100. get_runstate_snapshot(&state);
  101. WARN_ON(state.state != RUNSTATE_running);
  102. snap = &__get_cpu_var(xen_runstate_snapshot);
  103. /* work out how much time the VCPU has not been runn*ing* */
  104. runnable = state.time[RUNSTATE_runnable] - snap->time[RUNSTATE_runnable];
  105. offline = state.time[RUNSTATE_offline] - snap->time[RUNSTATE_offline];
  106. *snap = state;
  107. /* Add the appropriate number of ticks of stolen time,
  108. including any left-overs from last time. */
  109. stolen = runnable + offline + __this_cpu_read(xen_residual_stolen);
  110. if (stolen < 0)
  111. stolen = 0;
  112. ticks = iter_div_u64_rem(stolen, NS_PER_TICK, &stolen);
  113. __this_cpu_write(xen_residual_stolen, stolen);
  114. account_steal_ticks(ticks);
  115. }
  116. /* Get the TSC speed from Xen */
  117. static unsigned long xen_tsc_khz(void)
  118. {
  119. struct pvclock_vcpu_time_info *info =
  120. &HYPERVISOR_shared_info->vcpu_info[0].time;
  121. return pvclock_tsc_khz(info);
  122. }
  123. cycle_t xen_clocksource_read(void)
  124. {
  125. struct pvclock_vcpu_time_info *src;
  126. cycle_t ret;
  127. preempt_disable_notrace();
  128. src = &__get_cpu_var(xen_vcpu)->time;
  129. ret = pvclock_clocksource_read(src);
  130. preempt_enable_notrace();
  131. return ret;
  132. }
  133. static cycle_t xen_clocksource_get_cycles(struct clocksource *cs)
  134. {
  135. return xen_clocksource_read();
  136. }
  137. static void xen_read_wallclock(struct timespec *ts)
  138. {
  139. struct shared_info *s = HYPERVISOR_shared_info;
  140. struct pvclock_wall_clock *wall_clock = &(s->wc);
  141. struct pvclock_vcpu_time_info *vcpu_time;
  142. vcpu_time = &get_cpu_var(xen_vcpu)->time;
  143. pvclock_read_wallclock(wall_clock, vcpu_time, ts);
  144. put_cpu_var(xen_vcpu);
  145. }
  146. static void xen_get_wallclock(struct timespec *now)
  147. {
  148. xen_read_wallclock(now);
  149. }
  150. static int xen_set_wallclock(const struct timespec *now)
  151. {
  152. return -1;
  153. }
  154. static int xen_pvclock_gtod_notify(struct notifier_block *nb,
  155. unsigned long was_set, void *priv)
  156. {
  157. /* Protected by the calling core code serialization */
  158. static struct timespec next_sync;
  159. struct xen_platform_op op;
  160. struct timespec now;
  161. now = __current_kernel_time();
  162. /*
  163. * We only take the expensive HV call when the clock was set
  164. * or when the 11 minutes RTC synchronization time elapsed.
  165. */
  166. if (!was_set && timespec_compare(&now, &next_sync) < 0)
  167. return NOTIFY_OK;
  168. op.cmd = XENPF_settime;
  169. op.u.settime.secs = now.tv_sec;
  170. op.u.settime.nsecs = now.tv_nsec;
  171. op.u.settime.system_time = xen_clocksource_read();
  172. (void)HYPERVISOR_dom0_op(&op);
  173. /*
  174. * Move the next drift compensation time 11 minutes
  175. * ahead. That's emulating the sync_cmos_clock() update for
  176. * the hardware RTC.
  177. */
  178. next_sync = now;
  179. next_sync.tv_sec += 11 * 60;
  180. return NOTIFY_OK;
  181. }
  182. static struct notifier_block xen_pvclock_gtod_notifier = {
  183. .notifier_call = xen_pvclock_gtod_notify,
  184. };
  185. static struct clocksource xen_clocksource __read_mostly = {
  186. .name = "xen",
  187. .rating = 400,
  188. .read = xen_clocksource_get_cycles,
  189. .mask = ~0,
  190. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  191. };
  192. /*
  193. Xen clockevent implementation
  194. Xen has two clockevent implementations:
  195. The old timer_op one works with all released versions of Xen prior
  196. to version 3.0.4. This version of the hypervisor provides a
  197. single-shot timer with nanosecond resolution. However, sharing the
  198. same event channel is a 100Hz tick which is delivered while the
  199. vcpu is running. We don't care about or use this tick, but it will
  200. cause the core time code to think the timer fired too soon, and
  201. will end up resetting it each time. It could be filtered, but
  202. doing so has complications when the ktime clocksource is not yet
  203. the xen clocksource (ie, at boot time).
  204. The new vcpu_op-based timer interface allows the tick timer period
  205. to be changed or turned off. The tick timer is not useful as a
  206. periodic timer because events are only delivered to running vcpus.
  207. The one-shot timer can report when a timeout is in the past, so
  208. set_next_event is capable of returning -ETIME when appropriate.
  209. This interface is used when available.
  210. */
  211. /*
  212. Get a hypervisor absolute time. In theory we could maintain an
  213. offset between the kernel's time and the hypervisor's time, and
  214. apply that to a kernel's absolute timeout. Unfortunately the
  215. hypervisor and kernel times can drift even if the kernel is using
  216. the Xen clocksource, because ntp can warp the kernel's clocksource.
  217. */
  218. static s64 get_abs_timeout(unsigned long delta)
  219. {
  220. return xen_clocksource_read() + delta;
  221. }
  222. static void xen_timerop_set_mode(enum clock_event_mode mode,
  223. struct clock_event_device *evt)
  224. {
  225. switch (mode) {
  226. case CLOCK_EVT_MODE_PERIODIC:
  227. /* unsupported */
  228. WARN_ON(1);
  229. break;
  230. case CLOCK_EVT_MODE_ONESHOT:
  231. case CLOCK_EVT_MODE_RESUME:
  232. break;
  233. case CLOCK_EVT_MODE_UNUSED:
  234. case CLOCK_EVT_MODE_SHUTDOWN:
  235. HYPERVISOR_set_timer_op(0); /* cancel timeout */
  236. break;
  237. }
  238. }
  239. static int xen_timerop_set_next_event(unsigned long delta,
  240. struct clock_event_device *evt)
  241. {
  242. WARN_ON(evt->mode != CLOCK_EVT_MODE_ONESHOT);
  243. if (HYPERVISOR_set_timer_op(get_abs_timeout(delta)) < 0)
  244. BUG();
  245. /* We may have missed the deadline, but there's no real way of
  246. knowing for sure. If the event was in the past, then we'll
  247. get an immediate interrupt. */
  248. return 0;
  249. }
  250. static const struct clock_event_device xen_timerop_clockevent = {
  251. .name = "xen",
  252. .features = CLOCK_EVT_FEAT_ONESHOT,
  253. .max_delta_ns = 0xffffffff,
  254. .min_delta_ns = TIMER_SLOP,
  255. .mult = 1,
  256. .shift = 0,
  257. .rating = 500,
  258. .set_mode = xen_timerop_set_mode,
  259. .set_next_event = xen_timerop_set_next_event,
  260. };
  261. static void xen_vcpuop_set_mode(enum clock_event_mode mode,
  262. struct clock_event_device *evt)
  263. {
  264. int cpu = smp_processor_id();
  265. switch (mode) {
  266. case CLOCK_EVT_MODE_PERIODIC:
  267. WARN_ON(1); /* unsupported */
  268. break;
  269. case CLOCK_EVT_MODE_ONESHOT:
  270. if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, cpu, NULL))
  271. BUG();
  272. break;
  273. case CLOCK_EVT_MODE_UNUSED:
  274. case CLOCK_EVT_MODE_SHUTDOWN:
  275. if (HYPERVISOR_vcpu_op(VCPUOP_stop_singleshot_timer, cpu, NULL) ||
  276. HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, cpu, NULL))
  277. BUG();
  278. break;
  279. case CLOCK_EVT_MODE_RESUME:
  280. break;
  281. }
  282. }
  283. static int xen_vcpuop_set_next_event(unsigned long delta,
  284. struct clock_event_device *evt)
  285. {
  286. int cpu = smp_processor_id();
  287. struct vcpu_set_singleshot_timer single;
  288. int ret;
  289. WARN_ON(evt->mode != CLOCK_EVT_MODE_ONESHOT);
  290. single.timeout_abs_ns = get_abs_timeout(delta);
  291. single.flags = VCPU_SSHOTTMR_future;
  292. ret = HYPERVISOR_vcpu_op(VCPUOP_set_singleshot_timer, cpu, &single);
  293. BUG_ON(ret != 0 && ret != -ETIME);
  294. return ret;
  295. }
  296. static const struct clock_event_device xen_vcpuop_clockevent = {
  297. .name = "xen",
  298. .features = CLOCK_EVT_FEAT_ONESHOT,
  299. .max_delta_ns = 0xffffffff,
  300. .min_delta_ns = TIMER_SLOP,
  301. .mult = 1,
  302. .shift = 0,
  303. .rating = 500,
  304. .set_mode = xen_vcpuop_set_mode,
  305. .set_next_event = xen_vcpuop_set_next_event,
  306. };
  307. static const struct clock_event_device *xen_clockevent =
  308. &xen_timerop_clockevent;
  309. struct xen_clock_event_device {
  310. struct clock_event_device evt;
  311. char *name;
  312. };
  313. static DEFINE_PER_CPU(struct xen_clock_event_device, xen_clock_events) = { .evt.irq = -1 };
  314. static irqreturn_t xen_timer_interrupt(int irq, void *dev_id)
  315. {
  316. struct clock_event_device *evt = &__get_cpu_var(xen_clock_events).evt;
  317. irqreturn_t ret;
  318. ret = IRQ_NONE;
  319. if (evt->event_handler) {
  320. evt->event_handler(evt);
  321. ret = IRQ_HANDLED;
  322. }
  323. do_stolen_accounting();
  324. return ret;
  325. }
  326. void xen_teardown_timer(int cpu)
  327. {
  328. struct clock_event_device *evt;
  329. BUG_ON(cpu == 0);
  330. evt = &per_cpu(xen_clock_events, cpu).evt;
  331. if (evt->irq >= 0) {
  332. unbind_from_irqhandler(evt->irq, NULL);
  333. evt->irq = -1;
  334. kfree(per_cpu(xen_clock_events, cpu).name);
  335. per_cpu(xen_clock_events, cpu).name = NULL;
  336. }
  337. }
  338. void xen_setup_timer(int cpu)
  339. {
  340. char *name;
  341. struct clock_event_device *evt;
  342. int irq;
  343. evt = &per_cpu(xen_clock_events, cpu).evt;
  344. WARN(evt->irq >= 0, "IRQ%d for CPU%d is already allocated\n", evt->irq, cpu);
  345. if (evt->irq >= 0)
  346. xen_teardown_timer(cpu);
  347. printk(KERN_INFO "installing Xen timer for CPU %d\n", cpu);
  348. name = kasprintf(GFP_KERNEL, "timer%d", cpu);
  349. if (!name)
  350. name = "<timer kasprintf failed>";
  351. irq = bind_virq_to_irqhandler(VIRQ_TIMER, cpu, xen_timer_interrupt,
  352. IRQF_DISABLED|IRQF_PERCPU|
  353. IRQF_NOBALANCING|IRQF_TIMER|
  354. IRQF_FORCE_RESUME,
  355. name, NULL);
  356. memcpy(evt, xen_clockevent, sizeof(*evt));
  357. evt->cpumask = cpumask_of(cpu);
  358. evt->irq = irq;
  359. per_cpu(xen_clock_events, cpu).name = name;
  360. }
  361. void xen_setup_cpu_clockevents(void)
  362. {
  363. BUG_ON(preemptible());
  364. clockevents_register_device(&__get_cpu_var(xen_clock_events).evt);
  365. }
  366. void xen_timer_resume(void)
  367. {
  368. int cpu;
  369. pvclock_resume();
  370. if (xen_clockevent != &xen_vcpuop_clockevent)
  371. return;
  372. for_each_online_cpu(cpu) {
  373. if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, cpu, NULL))
  374. BUG();
  375. }
  376. }
  377. static const struct pv_time_ops xen_time_ops __initconst = {
  378. .sched_clock = xen_clocksource_read,
  379. };
  380. static void __init xen_time_init(void)
  381. {
  382. int cpu = smp_processor_id();
  383. struct timespec tp;
  384. clocksource_register_hz(&xen_clocksource, NSEC_PER_SEC);
  385. if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, cpu, NULL) == 0) {
  386. /* Successfully turned off 100Hz tick, so we have the
  387. vcpuop-based timer interface */
  388. printk(KERN_DEBUG "Xen: using vcpuop timer interface\n");
  389. xen_clockevent = &xen_vcpuop_clockevent;
  390. }
  391. /* Set initial system time with full resolution */
  392. xen_read_wallclock(&tp);
  393. do_settimeofday(&tp);
  394. setup_force_cpu_cap(X86_FEATURE_TSC);
  395. xen_setup_runstate_info(cpu);
  396. xen_setup_timer(cpu);
  397. xen_setup_cpu_clockevents();
  398. if (xen_initial_domain())
  399. pvclock_gtod_register_notifier(&xen_pvclock_gtod_notifier);
  400. }
  401. void __init xen_init_time_ops(void)
  402. {
  403. pv_time_ops = xen_time_ops;
  404. x86_init.timers.timer_init = xen_time_init;
  405. x86_init.timers.setup_percpu_clockev = x86_init_noop;
  406. x86_cpuinit.setup_percpu_clockev = x86_init_noop;
  407. x86_platform.calibrate_tsc = xen_tsc_khz;
  408. x86_platform.get_wallclock = xen_get_wallclock;
  409. /* Dom0 uses the native method to set the hardware RTC. */
  410. if (!xen_initial_domain())
  411. x86_platform.set_wallclock = xen_set_wallclock;
  412. }
  413. #ifdef CONFIG_XEN_PVHVM
  414. static void xen_hvm_setup_cpu_clockevents(void)
  415. {
  416. int cpu = smp_processor_id();
  417. xen_setup_runstate_info(cpu);
  418. /*
  419. * xen_setup_timer(cpu) - snprintf is bad in atomic context. Hence
  420. * doing it xen_hvm_cpu_notify (which gets called by smp_init during
  421. * early bootup and also during CPU hotplug events).
  422. */
  423. xen_setup_cpu_clockevents();
  424. }
  425. void __init xen_hvm_init_time_ops(void)
  426. {
  427. /* vector callback is needed otherwise we cannot receive interrupts
  428. * on cpu > 0 and at this point we don't know how many cpus are
  429. * available */
  430. if (!xen_have_vector_callback)
  431. return;
  432. if (!xen_feature(XENFEAT_hvm_safe_pvclock)) {
  433. printk(KERN_INFO "Xen doesn't support pvclock on HVM,"
  434. "disable pv timer\n");
  435. return;
  436. }
  437. pv_time_ops = xen_time_ops;
  438. x86_init.timers.setup_percpu_clockev = xen_time_init;
  439. x86_cpuinit.setup_percpu_clockev = xen_hvm_setup_cpu_clockevents;
  440. x86_platform.calibrate_tsc = xen_tsc_khz;
  441. x86_platform.get_wallclock = xen_get_wallclock;
  442. x86_platform.set_wallclock = xen_set_wallclock;
  443. }
  444. #endif