time.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 <asm/xen/hypervisor.h>
  15. #include <asm/xen/hypercall.h>
  16. #include <xen/events.h>
  17. #include <xen/interface/xen.h>
  18. #include <xen/interface/vcpu.h>
  19. #include "xen-ops.h"
  20. #define XEN_SHIFT 22
  21. /* Xen may fire a timer up to this many ns early */
  22. #define TIMER_SLOP 100000
  23. /* These are perodically updated in shared_info, and then copied here. */
  24. struct shadow_time_info {
  25. u64 tsc_timestamp; /* TSC at last update of time vals. */
  26. u64 system_timestamp; /* Time, in nanosecs, since boot. */
  27. u32 tsc_to_nsec_mul;
  28. int tsc_shift;
  29. u32 version;
  30. };
  31. static DEFINE_PER_CPU(struct shadow_time_info, shadow_time);
  32. unsigned long xen_cpu_khz(void)
  33. {
  34. u64 cpu_khz = 1000000ULL << 32;
  35. const struct vcpu_time_info *info =
  36. &HYPERVISOR_shared_info->vcpu_info[0].time;
  37. do_div(cpu_khz, info->tsc_to_system_mul);
  38. if (info->tsc_shift < 0)
  39. cpu_khz <<= -info->tsc_shift;
  40. else
  41. cpu_khz >>= info->tsc_shift;
  42. return cpu_khz;
  43. }
  44. /*
  45. * Reads a consistent set of time-base values from Xen, into a shadow data
  46. * area.
  47. */
  48. static void get_time_values_from_xen(void)
  49. {
  50. struct vcpu_time_info *src;
  51. struct shadow_time_info *dst;
  52. preempt_disable();
  53. /* src is shared memory with the hypervisor, so we need to
  54. make sure we get a consistent snapshot, even in the face of
  55. being preempted. */
  56. src = &__get_cpu_var(xen_vcpu)->time;
  57. dst = &__get_cpu_var(shadow_time);
  58. do {
  59. dst->version = src->version;
  60. rmb(); /* fetch version before data */
  61. dst->tsc_timestamp = src->tsc_timestamp;
  62. dst->system_timestamp = src->system_time;
  63. dst->tsc_to_nsec_mul = src->tsc_to_system_mul;
  64. dst->tsc_shift = src->tsc_shift;
  65. rmb(); /* test version after fetching data */
  66. } while ((src->version & 1) | (dst->version ^ src->version));
  67. preempt_enable();
  68. }
  69. /*
  70. * Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction,
  71. * yielding a 64-bit result.
  72. */
  73. static inline u64 scale_delta(u64 delta, u32 mul_frac, int shift)
  74. {
  75. u64 product;
  76. #ifdef __i386__
  77. u32 tmp1, tmp2;
  78. #endif
  79. if (shift < 0)
  80. delta >>= -shift;
  81. else
  82. delta <<= shift;
  83. #ifdef __i386__
  84. __asm__ (
  85. "mul %5 ; "
  86. "mov %4,%%eax ; "
  87. "mov %%edx,%4 ; "
  88. "mul %5 ; "
  89. "xor %5,%5 ; "
  90. "add %4,%%eax ; "
  91. "adc %5,%%edx ; "
  92. : "=A" (product), "=r" (tmp1), "=r" (tmp2)
  93. : "a" ((u32)delta), "1" ((u32)(delta >> 32)), "2" (mul_frac) );
  94. #elif __x86_64__
  95. __asm__ (
  96. "mul %%rdx ; shrd $32,%%rdx,%%rax"
  97. : "=a" (product) : "0" (delta), "d" ((u64)mul_frac) );
  98. #else
  99. #error implement me!
  100. #endif
  101. return product;
  102. }
  103. static u64 get_nsec_offset(struct shadow_time_info *shadow)
  104. {
  105. u64 now, delta;
  106. rdtscll(now);
  107. delta = now - shadow->tsc_timestamp;
  108. return scale_delta(delta, shadow->tsc_to_nsec_mul, shadow->tsc_shift);
  109. }
  110. cycle_t xen_clocksource_read(void)
  111. {
  112. struct shadow_time_info *shadow = &get_cpu_var(shadow_time);
  113. cycle_t ret;
  114. get_time_values_from_xen();
  115. ret = shadow->system_timestamp + get_nsec_offset(shadow);
  116. put_cpu_var(shadow_time);
  117. return ret;
  118. }
  119. static void xen_read_wallclock(struct timespec *ts)
  120. {
  121. const struct shared_info *s = HYPERVISOR_shared_info;
  122. u32 version;
  123. u64 delta;
  124. struct timespec now;
  125. /* get wallclock at system boot */
  126. do {
  127. version = s->wc_version;
  128. rmb(); /* fetch version before time */
  129. now.tv_sec = s->wc_sec;
  130. now.tv_nsec = s->wc_nsec;
  131. rmb(); /* fetch time before checking version */
  132. } while ((s->wc_version & 1) | (version ^ s->wc_version));
  133. delta = xen_clocksource_read(); /* time since system boot */
  134. delta += now.tv_sec * (u64)NSEC_PER_SEC + now.tv_nsec;
  135. now.tv_nsec = do_div(delta, NSEC_PER_SEC);
  136. now.tv_sec = delta;
  137. set_normalized_timespec(ts, now.tv_sec, now.tv_nsec);
  138. }
  139. unsigned long xen_get_wallclock(void)
  140. {
  141. struct timespec ts;
  142. xen_read_wallclock(&ts);
  143. return ts.tv_sec;
  144. }
  145. int xen_set_wallclock(unsigned long now)
  146. {
  147. /* do nothing for domU */
  148. return -1;
  149. }
  150. static struct clocksource xen_clocksource __read_mostly = {
  151. .name = "xen",
  152. .rating = 400,
  153. .read = xen_clocksource_read,
  154. .mask = ~0,
  155. .mult = 1<<XEN_SHIFT, /* time directly in nanoseconds */
  156. .shift = XEN_SHIFT,
  157. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  158. };
  159. /*
  160. Xen clockevent implementation
  161. Xen has two clockevent implementations:
  162. The old timer_op one works with all released versions of Xen prior
  163. to version 3.0.4. This version of the hypervisor provides a
  164. single-shot timer with nanosecond resolution. However, sharing the
  165. same event channel is a 100Hz tick which is delivered while the
  166. vcpu is running. We don't care about or use this tick, but it will
  167. cause the core time code to think the timer fired too soon, and
  168. will end up resetting it each time. It could be filtered, but
  169. doing so has complications when the ktime clocksource is not yet
  170. the xen clocksource (ie, at boot time).
  171. The new vcpu_op-based timer interface allows the tick timer period
  172. to be changed or turned off. The tick timer is not useful as a
  173. periodic timer because events are only delivered to running vcpus.
  174. The one-shot timer can report when a timeout is in the past, so
  175. set_next_event is capable of returning -ETIME when appropriate.
  176. This interface is used when available.
  177. */
  178. /*
  179. Get a hypervisor absolute time. In theory we could maintain an
  180. offset between the kernel's time and the hypervisor's time, and
  181. apply that to a kernel's absolute timeout. Unfortunately the
  182. hypervisor and kernel times can drift even if the kernel is using
  183. the Xen clocksource, because ntp can warp the kernel's clocksource.
  184. */
  185. static s64 get_abs_timeout(unsigned long delta)
  186. {
  187. return xen_clocksource_read() + delta;
  188. }
  189. static void xen_timerop_set_mode(enum clock_event_mode mode,
  190. struct clock_event_device *evt)
  191. {
  192. switch (mode) {
  193. case CLOCK_EVT_MODE_PERIODIC:
  194. /* unsupported */
  195. WARN_ON(1);
  196. break;
  197. case CLOCK_EVT_MODE_ONESHOT:
  198. break;
  199. case CLOCK_EVT_MODE_UNUSED:
  200. case CLOCK_EVT_MODE_SHUTDOWN:
  201. HYPERVISOR_set_timer_op(0); /* cancel timeout */
  202. break;
  203. }
  204. }
  205. static int xen_timerop_set_next_event(unsigned long delta,
  206. struct clock_event_device *evt)
  207. {
  208. WARN_ON(evt->mode != CLOCK_EVT_MODE_ONESHOT);
  209. if (HYPERVISOR_set_timer_op(get_abs_timeout(delta)) < 0)
  210. BUG();
  211. /* We may have missed the deadline, but there's no real way of
  212. knowing for sure. If the event was in the past, then we'll
  213. get an immediate interrupt. */
  214. return 0;
  215. }
  216. static const struct clock_event_device xen_timerop_clockevent = {
  217. .name = "xen",
  218. .features = CLOCK_EVT_FEAT_ONESHOT,
  219. .max_delta_ns = 0xffffffff,
  220. .min_delta_ns = TIMER_SLOP,
  221. .mult = 1,
  222. .shift = 0,
  223. .rating = 500,
  224. .set_mode = xen_timerop_set_mode,
  225. .set_next_event = xen_timerop_set_next_event,
  226. };
  227. static void xen_vcpuop_set_mode(enum clock_event_mode mode,
  228. struct clock_event_device *evt)
  229. {
  230. int cpu = smp_processor_id();
  231. switch (mode) {
  232. case CLOCK_EVT_MODE_PERIODIC:
  233. WARN_ON(1); /* unsupported */
  234. break;
  235. case CLOCK_EVT_MODE_ONESHOT:
  236. if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, cpu, NULL))
  237. BUG();
  238. break;
  239. case CLOCK_EVT_MODE_UNUSED:
  240. case CLOCK_EVT_MODE_SHUTDOWN:
  241. if (HYPERVISOR_vcpu_op(VCPUOP_stop_singleshot_timer, cpu, NULL) ||
  242. HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, cpu, NULL))
  243. BUG();
  244. break;
  245. }
  246. }
  247. static int xen_vcpuop_set_next_event(unsigned long delta,
  248. struct clock_event_device *evt)
  249. {
  250. int cpu = smp_processor_id();
  251. struct vcpu_set_singleshot_timer single;
  252. int ret;
  253. WARN_ON(evt->mode != CLOCK_EVT_MODE_ONESHOT);
  254. single.timeout_abs_ns = get_abs_timeout(delta);
  255. single.flags = VCPU_SSHOTTMR_future;
  256. ret = HYPERVISOR_vcpu_op(VCPUOP_set_singleshot_timer, cpu, &single);
  257. BUG_ON(ret != 0 && ret != -ETIME);
  258. return ret;
  259. }
  260. static const struct clock_event_device xen_vcpuop_clockevent = {
  261. .name = "xen",
  262. .features = CLOCK_EVT_FEAT_ONESHOT,
  263. .max_delta_ns = 0xffffffff,
  264. .min_delta_ns = TIMER_SLOP,
  265. .mult = 1,
  266. .shift = 0,
  267. .rating = 500,
  268. .set_mode = xen_vcpuop_set_mode,
  269. .set_next_event = xen_vcpuop_set_next_event,
  270. };
  271. static const struct clock_event_device *xen_clockevent =
  272. &xen_timerop_clockevent;
  273. static DEFINE_PER_CPU(struct clock_event_device, xen_clock_events);
  274. static irqreturn_t xen_timer_interrupt(int irq, void *dev_id)
  275. {
  276. struct clock_event_device *evt = &__get_cpu_var(xen_clock_events);
  277. irqreturn_t ret;
  278. ret = IRQ_NONE;
  279. if (evt->event_handler) {
  280. evt->event_handler(evt);
  281. ret = IRQ_HANDLED;
  282. }
  283. return ret;
  284. }
  285. static void xen_setup_timer(int cpu)
  286. {
  287. const char *name;
  288. struct clock_event_device *evt;
  289. int irq;
  290. printk(KERN_INFO "installing Xen timer for CPU %d\n", cpu);
  291. name = kasprintf(GFP_KERNEL, "timer%d", cpu);
  292. if (!name)
  293. name = "<timer kasprintf failed>";
  294. irq = bind_virq_to_irqhandler(VIRQ_TIMER, cpu, xen_timer_interrupt,
  295. IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
  296. name, NULL);
  297. evt = &get_cpu_var(xen_clock_events);
  298. memcpy(evt, xen_clockevent, sizeof(*evt));
  299. evt->cpumask = cpumask_of_cpu(cpu);
  300. evt->irq = irq;
  301. clockevents_register_device(evt);
  302. put_cpu_var(xen_clock_events);
  303. }
  304. __init void xen_time_init(void)
  305. {
  306. int cpu = smp_processor_id();
  307. get_time_values_from_xen();
  308. clocksource_register(&xen_clocksource);
  309. if (HYPERVISOR_vcpu_op(VCPUOP_stop_periodic_timer, cpu, NULL) == 0) {
  310. /* Successfully turned off 100hz tick, so we have the
  311. vcpuop-based timer interface */
  312. printk(KERN_DEBUG "Xen: using vcpuop timer interface\n");
  313. xen_clockevent = &xen_vcpuop_clockevent;
  314. }
  315. /* Set initial system time with full resolution */
  316. xen_read_wallclock(&xtime);
  317. set_normalized_timespec(&wall_to_monotonic,
  318. -xtime.tv_sec, -xtime.tv_nsec);
  319. tsc_disable = 0;
  320. xen_setup_timer(cpu);
  321. }