vmiclock_32.c 8.8 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 <asm/i8253.h>
  34. #include <asm/irq_vectors.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_tsc_khz = vmi_tsc_khz */
  63. unsigned long vmi_tsc_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. case CLOCK_EVT_MODE_RESUME:
  124. break;
  125. case CLOCK_EVT_MODE_PERIODIC:
  126. cycles_per_hz = vmi_timer_ops.get_cycle_frequency();
  127. (void)do_div(cycles_per_hz, HZ);
  128. now = vmi_timer_ops.get_cycle_counter(vmi_counter(VMI_PERIODIC));
  129. vmi_timer_ops.set_alarm(VMI_PERIODIC, now, cycles_per_hz);
  130. break;
  131. case CLOCK_EVT_MODE_UNUSED:
  132. case CLOCK_EVT_MODE_SHUTDOWN:
  133. switch (evt->mode) {
  134. case CLOCK_EVT_MODE_ONESHOT:
  135. vmi_timer_ops.cancel_alarm(VMI_ONESHOT);
  136. break;
  137. case CLOCK_EVT_MODE_PERIODIC:
  138. vmi_timer_ops.cancel_alarm(VMI_PERIODIC);
  139. break;
  140. default:
  141. break;
  142. }
  143. break;
  144. default:
  145. break;
  146. }
  147. }
  148. static int vmi_timer_next_event(unsigned long delta,
  149. struct clock_event_device *evt)
  150. {
  151. /* Unfortunately, set_next_event interface only passes relative
  152. * expiry, but we want absolute expiry. It'd be better if were
  153. * were passed an aboslute expiry, since a bunch of time may
  154. * have been stolen between the time the delta is computed and
  155. * when we set the alarm below. */
  156. cycle_t now = vmi_timer_ops.get_cycle_counter(vmi_counter(VMI_ONESHOT));
  157. BUG_ON(evt->mode != CLOCK_EVT_MODE_ONESHOT);
  158. vmi_timer_ops.set_alarm(VMI_ONESHOT, now + delta, 0);
  159. return 0;
  160. }
  161. static struct clock_event_device vmi_clockevent = {
  162. .name = "vmi-timer",
  163. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  164. .shift = 22,
  165. .set_mode = vmi_timer_set_mode,
  166. .set_next_event = vmi_timer_next_event,
  167. .rating = 1000,
  168. .irq = 0,
  169. };
  170. static irqreturn_t vmi_timer_interrupt(int irq, void *dev_id)
  171. {
  172. struct clock_event_device *evt = &__get_cpu_var(local_events);
  173. evt->event_handler(evt);
  174. return IRQ_HANDLED;
  175. }
  176. static struct irqaction vmi_clock_action = {
  177. .name = "vmi-timer",
  178. .handler = vmi_timer_interrupt,
  179. .flags = IRQF_DISABLED | IRQF_NOBALANCING,
  180. .mask = CPU_MASK_ALL,
  181. };
  182. static void __devinit vmi_time_init_clockevent(void)
  183. {
  184. cycle_t cycles_per_msec;
  185. struct clock_event_device *evt;
  186. int cpu = smp_processor_id();
  187. evt = &__get_cpu_var(local_events);
  188. /* Use cycles_per_msec since div_sc params are 32-bits. */
  189. cycles_per_msec = vmi_timer_ops.get_cycle_frequency();
  190. (void)do_div(cycles_per_msec, 1000);
  191. memcpy(evt, &vmi_clockevent, sizeof(*evt));
  192. /* Must pick .shift such that .mult fits in 32-bits. Choosing
  193. * .shift to be 22 allows 2^(32-22) cycles per nano-seconds
  194. * before overflow. */
  195. evt->mult = div_sc(cycles_per_msec, NSEC_PER_MSEC, evt->shift);
  196. /* Upper bound is clockevent's use of ulong for cycle deltas. */
  197. evt->max_delta_ns = clockevent_delta2ns(ULONG_MAX, evt);
  198. evt->min_delta_ns = clockevent_delta2ns(1, evt);
  199. evt->cpumask = cpumask_of_cpu(cpu);
  200. printk(KERN_WARNING "vmi: registering clock event %s. mult=%lu shift=%u\n",
  201. evt->name, evt->mult, evt->shift);
  202. clockevents_register_device(evt);
  203. }
  204. void __init vmi_time_init(void)
  205. {
  206. /* Disable PIT: BIOSes start PIT CH0 with 18.2hz peridic. */
  207. outb_pit(0x3a, PIT_MODE); /* binary, mode 5, LSB/MSB, ch 0 */
  208. vmi_time_init_clockevent();
  209. setup_irq(0, &vmi_clock_action);
  210. }
  211. #ifdef CONFIG_X86_LOCAL_APIC
  212. void __devinit vmi_time_bsp_init(void)
  213. {
  214. /*
  215. * On APIC systems, we want local timers to fire on each cpu. We do
  216. * this by programming LVTT to deliver timer events to the IRQ handler
  217. * for IRQ-0, since we can't re-use the APIC local timer handler
  218. * without interfering with that code.
  219. */
  220. clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
  221. local_irq_disable();
  222. #ifdef CONFIG_X86_SMP
  223. /*
  224. * XXX handle_percpu_irq only defined for SMP; we need to switch over
  225. * to using it, since this is a local interrupt, which each CPU must
  226. * handle individually without locking out or dropping simultaneous
  227. * local timers on other CPUs. We also don't want to trigger the
  228. * quirk workaround code for interrupts which gets invoked from
  229. * handle_percpu_irq via eoi, so we use our own IRQ chip.
  230. */
  231. set_irq_chip_and_handler_name(0, &vmi_chip, handle_percpu_irq, "lvtt");
  232. #else
  233. set_irq_chip_and_handler_name(0, &vmi_chip, handle_edge_irq, "lvtt");
  234. #endif
  235. vmi_wiring = VMI_ALARM_WIRED_LVTT;
  236. apic_write(APIC_LVTT, vmi_get_timer_vector());
  237. local_irq_enable();
  238. clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
  239. }
  240. void __devinit vmi_time_ap_init(void)
  241. {
  242. vmi_time_init_clockevent();
  243. apic_write(APIC_LVTT, vmi_get_timer_vector());
  244. }
  245. #endif
  246. /** vmi clocksource */
  247. static cycle_t read_real_cycles(void)
  248. {
  249. return vmi_timer_ops.get_cycle_counter(VMI_CYCLES_REAL);
  250. }
  251. static struct clocksource clocksource_vmi = {
  252. .name = "vmi-timer",
  253. .rating = 450,
  254. .read = read_real_cycles,
  255. .mask = CLOCKSOURCE_MASK(64),
  256. .mult = 0, /* to be set */
  257. .shift = 22,
  258. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  259. };
  260. static int __init init_vmi_clocksource(void)
  261. {
  262. cycle_t cycles_per_msec;
  263. if (!vmi_timer_ops.get_cycle_frequency)
  264. return 0;
  265. /* Use khz2mult rather than hz2mult since hz arg is only 32-bits. */
  266. cycles_per_msec = vmi_timer_ops.get_cycle_frequency();
  267. (void)do_div(cycles_per_msec, 1000);
  268. /* Note that clocksource.{mult, shift} converts in the opposite direction
  269. * as clockevents. */
  270. clocksource_vmi.mult = clocksource_khz2mult(cycles_per_msec,
  271. clocksource_vmi.shift);
  272. printk(KERN_WARNING "vmi: registering clock source khz=%lld\n", cycles_per_msec);
  273. return clocksource_register(&clocksource_vmi);
  274. }
  275. module_init(init_vmi_clocksource);