vmiclock.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * VMI paravirtual timer support routines.
  3. *
  4. * Copyright (C) 2007, VMware, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  14. * NON INFRINGEMENT. See the GNU General Public License for more
  15. * details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. */
  22. #include <linux/smp.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/cpumask.h>
  25. #include <linux/clocksource.h>
  26. #include <linux/clockchips.h>
  27. #include <asm/vmi.h>
  28. #include <asm/vmi_time.h>
  29. #include <asm/arch_hooks.h>
  30. #include <asm/apicdef.h>
  31. #include <asm/apic.h>
  32. #include <asm/timer.h>
  33. #include <irq_vectors.h>
  34. #include "io_ports.h"
  35. #define VMI_ONESHOT (VMI_ALARM_IS_ONESHOT | VMI_CYCLES_REAL | vmi_get_alarm_wiring())
  36. #define VMI_PERIODIC (VMI_ALARM_IS_PERIODIC | VMI_CYCLES_REAL | vmi_get_alarm_wiring())
  37. static DEFINE_PER_CPU(struct clock_event_device, local_events);
  38. static inline u32 vmi_counter(u32 flags)
  39. {
  40. /* Given VMI_ONESHOT or VMI_PERIODIC, return the corresponding
  41. * cycle counter. */
  42. return flags & VMI_ALARM_COUNTER_MASK;
  43. }
  44. /* paravirt_ops.get_wallclock = vmi_get_wallclock */
  45. unsigned long vmi_get_wallclock(void)
  46. {
  47. unsigned long long wallclock;
  48. wallclock = vmi_timer_ops.get_wallclock(); // nsec
  49. (void)do_div(wallclock, 1000000000); // sec
  50. return wallclock;
  51. }
  52. /* paravirt_ops.set_wallclock = vmi_set_wallclock */
  53. int vmi_set_wallclock(unsigned long now)
  54. {
  55. return 0;
  56. }
  57. /* paravirt_ops.sched_clock = vmi_sched_clock */
  58. unsigned long long vmi_sched_clock(void)
  59. {
  60. return cycles_2_ns(vmi_timer_ops.get_cycle_counter(VMI_CYCLES_AVAILABLE));
  61. }
  62. /* paravirt_ops.get_cpu_khz = vmi_cpu_khz */
  63. unsigned long vmi_cpu_khz(void)
  64. {
  65. unsigned long long khz;
  66. khz = vmi_timer_ops.get_cycle_frequency();
  67. (void)do_div(khz, 1000);
  68. return khz;
  69. }
  70. static inline unsigned int vmi_get_timer_vector(void)
  71. {
  72. #ifdef CONFIG_X86_IO_APIC
  73. return FIRST_DEVICE_VECTOR;
  74. #else
  75. return FIRST_EXTERNAL_VECTOR;
  76. #endif
  77. }
  78. /** vmi clockchip */
  79. #ifdef CONFIG_X86_LOCAL_APIC
  80. static unsigned int startup_timer_irq(unsigned int irq)
  81. {
  82. unsigned long val = apic_read(APIC_LVTT);
  83. apic_write(APIC_LVTT, vmi_get_timer_vector());
  84. return (val & APIC_SEND_PENDING);
  85. }
  86. static void mask_timer_irq(unsigned int irq)
  87. {
  88. unsigned long val = apic_read(APIC_LVTT);
  89. apic_write(APIC_LVTT, val | APIC_LVT_MASKED);
  90. }
  91. static void unmask_timer_irq(unsigned int irq)
  92. {
  93. unsigned long val = apic_read(APIC_LVTT);
  94. apic_write(APIC_LVTT, val & ~APIC_LVT_MASKED);
  95. }
  96. static void ack_timer_irq(unsigned int irq)
  97. {
  98. ack_APIC_irq();
  99. }
  100. static struct irq_chip vmi_chip __read_mostly = {
  101. .name = "VMI-LOCAL",
  102. .startup = startup_timer_irq,
  103. .mask = mask_timer_irq,
  104. .unmask = unmask_timer_irq,
  105. .ack = ack_timer_irq
  106. };
  107. #endif
  108. /** vmi clockevent */
  109. #define VMI_ALARM_WIRED_IRQ0 0x00000000
  110. #define VMI_ALARM_WIRED_LVTT 0x00010000
  111. static int vmi_wiring = VMI_ALARM_WIRED_IRQ0;
  112. static inline int vmi_get_alarm_wiring(void)
  113. {
  114. return vmi_wiring;
  115. }
  116. static void vmi_timer_set_mode(enum clock_event_mode mode,
  117. struct clock_event_device *evt)
  118. {
  119. cycle_t now, cycles_per_hz;
  120. BUG_ON(!irqs_disabled());
  121. switch (mode) {
  122. case CLOCK_EVT_MODE_ONESHOT:
  123. break;
  124. case CLOCK_EVT_MODE_PERIODIC:
  125. cycles_per_hz = vmi_timer_ops.get_cycle_frequency();
  126. (void)do_div(cycles_per_hz, HZ);
  127. now = vmi_timer_ops.get_cycle_counter(vmi_counter(VMI_PERIODIC));
  128. vmi_timer_ops.set_alarm(VMI_PERIODIC, now, cycles_per_hz);
  129. break;
  130. case CLOCK_EVT_MODE_UNUSED:
  131. case CLOCK_EVT_MODE_SHUTDOWN:
  132. switch (evt->mode) {
  133. case CLOCK_EVT_MODE_ONESHOT:
  134. vmi_timer_ops.cancel_alarm(VMI_ONESHOT);
  135. break;
  136. case CLOCK_EVT_MODE_PERIODIC:
  137. vmi_timer_ops.cancel_alarm(VMI_PERIODIC);
  138. break;
  139. default:
  140. break;
  141. }
  142. break;
  143. default:
  144. break;
  145. }
  146. }
  147. static int vmi_timer_next_event(unsigned long delta,
  148. struct clock_event_device *evt)
  149. {
  150. /* Unfortunately, set_next_event interface only passes relative
  151. * expiry, but we want absolute expiry. It'd be better if were
  152. * were passed an aboslute expiry, since a bunch of time may
  153. * have been stolen between the time the delta is computed and
  154. * when we set the alarm below. */
  155. cycle_t now = vmi_timer_ops.get_cycle_counter(vmi_counter(VMI_ONESHOT));
  156. BUG_ON(evt->mode != CLOCK_EVT_MODE_ONESHOT);
  157. vmi_timer_ops.set_alarm(VMI_ONESHOT, now + delta, 0);
  158. return 0;
  159. }
  160. static struct clock_event_device vmi_clockevent = {
  161. .name = "vmi-timer",
  162. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  163. .shift = 22,
  164. .set_mode = vmi_timer_set_mode,
  165. .set_next_event = vmi_timer_next_event,
  166. .rating = 1000,
  167. .irq = 0,
  168. };
  169. static irqreturn_t vmi_timer_interrupt(int irq, void *dev_id)
  170. {
  171. struct clock_event_device *evt = &__get_cpu_var(local_events);
  172. evt->event_handler(evt);
  173. return IRQ_HANDLED;
  174. }
  175. static struct irqaction vmi_clock_action = {
  176. .name = "vmi-timer",
  177. .handler = vmi_timer_interrupt,
  178. .flags = IRQF_DISABLED | IRQF_NOBALANCING,
  179. .mask = CPU_MASK_ALL,
  180. };
  181. static void __devinit vmi_time_init_clockevent(void)
  182. {
  183. cycle_t cycles_per_msec;
  184. struct clock_event_device *evt;
  185. int cpu = smp_processor_id();
  186. evt = &__get_cpu_var(local_events);
  187. /* Use cycles_per_msec since div_sc params are 32-bits. */
  188. cycles_per_msec = vmi_timer_ops.get_cycle_frequency();
  189. (void)do_div(cycles_per_msec, 1000);
  190. memcpy(evt, &vmi_clockevent, sizeof(*evt));
  191. /* Must pick .shift such that .mult fits in 32-bits. Choosing
  192. * .shift to be 22 allows 2^(32-22) cycles per nano-seconds
  193. * before overflow. */
  194. evt->mult = div_sc(cycles_per_msec, NSEC_PER_MSEC, evt->shift);
  195. /* Upper bound is clockevent's use of ulong for cycle deltas. */
  196. evt->max_delta_ns = clockevent_delta2ns(ULONG_MAX, evt);
  197. evt->min_delta_ns = clockevent_delta2ns(1, evt);
  198. evt->cpumask = cpumask_of_cpu(cpu);
  199. printk(KERN_WARNING "vmi: registering clock event %s. mult=%lu shift=%u\n",
  200. evt->name, evt->mult, evt->shift);
  201. clockevents_register_device(evt);
  202. }
  203. void __init vmi_time_init(void)
  204. {
  205. /* Disable PIT: BIOSes start PIT CH0 with 18.2hz peridic. */
  206. outb_p(0x3a, PIT_MODE); /* binary, mode 5, LSB/MSB, ch 0 */
  207. vmi_time_init_clockevent();
  208. setup_irq(0, &vmi_clock_action);
  209. }
  210. #ifdef CONFIG_X86_LOCAL_APIC
  211. void __devinit vmi_time_bsp_init(void)
  212. {
  213. /*
  214. * On APIC systems, we want local timers to fire on each cpu. We do
  215. * this by programming LVTT to deliver timer events to the IRQ handler
  216. * for IRQ-0, since we can't re-use the APIC local timer handler
  217. * without interfering with that code.
  218. */
  219. clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
  220. local_irq_disable();
  221. #ifdef CONFIG_X86_SMP
  222. /*
  223. * XXX handle_percpu_irq only defined for SMP; we need to switch over
  224. * to using it, since this is a local interrupt, which each CPU must
  225. * handle individually without locking out or dropping simultaneous
  226. * local timers on other CPUs. We also don't want to trigger the
  227. * quirk workaround code for interrupts which gets invoked from
  228. * handle_percpu_irq via eoi, so we use our own IRQ chip.
  229. */
  230. set_irq_chip_and_handler_name(0, &vmi_chip, handle_percpu_irq, "lvtt");
  231. #else
  232. set_irq_chip_and_handler_name(0, &vmi_chip, handle_edge_irq, "lvtt");
  233. #endif
  234. vmi_wiring = VMI_ALARM_WIRED_LVTT;
  235. apic_write(APIC_LVTT, vmi_get_timer_vector());
  236. local_irq_enable();
  237. clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
  238. }
  239. void __devinit vmi_time_ap_init(void)
  240. {
  241. vmi_time_init_clockevent();
  242. apic_write(APIC_LVTT, vmi_get_timer_vector());
  243. }
  244. #endif
  245. /** vmi clocksource */
  246. static cycle_t read_real_cycles(void)
  247. {
  248. return vmi_timer_ops.get_cycle_counter(VMI_CYCLES_REAL);
  249. }
  250. static struct clocksource clocksource_vmi = {
  251. .name = "vmi-timer",
  252. .rating = 450,
  253. .read = read_real_cycles,
  254. .mask = CLOCKSOURCE_MASK(64),
  255. .mult = 0, /* to be set */
  256. .shift = 22,
  257. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  258. };
  259. static int __init init_vmi_clocksource(void)
  260. {
  261. cycle_t cycles_per_msec;
  262. if (!vmi_timer_ops.get_cycle_frequency)
  263. return 0;
  264. /* Use khz2mult rather than hz2mult since hz arg is only 32-bits. */
  265. cycles_per_msec = vmi_timer_ops.get_cycle_frequency();
  266. (void)do_div(cycles_per_msec, 1000);
  267. /* Note that clocksource.{mult, shift} converts in the opposite direction
  268. * as clockevents. */
  269. clocksource_vmi.mult = clocksource_khz2mult(cycles_per_msec,
  270. clocksource_vmi.shift);
  271. printk(KERN_WARNING "vmi: registering clock source khz=%lld\n", cycles_per_msec);
  272. return clocksource_register(&clocksource_vmi);
  273. }
  274. module_init(init_vmi_clocksource);