vmiclock_32.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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/apicdef.h>
  30. #include <asm/apic.h>
  31. #include <asm/timer.h>
  32. #include <asm/i8253.h>
  33. #include <asm/irq_vectors.h>
  34. #define VMI_ONESHOT (VMI_ALARM_IS_ONESHOT | VMI_CYCLES_REAL | vmi_get_alarm_wiring())
  35. #define VMI_PERIODIC (VMI_ALARM_IS_PERIODIC | VMI_CYCLES_REAL | vmi_get_alarm_wiring())
  36. static DEFINE_PER_CPU(struct clock_event_device, local_events);
  37. static inline u32 vmi_counter(u32 flags)
  38. {
  39. /* Given VMI_ONESHOT or VMI_PERIODIC, return the corresponding
  40. * cycle counter. */
  41. return flags & VMI_ALARM_COUNTER_MASK;
  42. }
  43. /* paravirt_ops.get_wallclock = vmi_get_wallclock */
  44. unsigned long vmi_get_wallclock(void)
  45. {
  46. unsigned long long wallclock;
  47. wallclock = vmi_timer_ops.get_wallclock(); // nsec
  48. (void)do_div(wallclock, 1000000000); // sec
  49. return wallclock;
  50. }
  51. /* paravirt_ops.set_wallclock = vmi_set_wallclock */
  52. int vmi_set_wallclock(unsigned long now)
  53. {
  54. return 0;
  55. }
  56. /* paravirt_ops.sched_clock = vmi_sched_clock */
  57. unsigned long long vmi_sched_clock(void)
  58. {
  59. return cycles_2_ns(vmi_timer_ops.get_cycle_counter(VMI_CYCLES_AVAILABLE));
  60. }
  61. /* paravirt_ops.get_tsc_khz = vmi_tsc_khz */
  62. unsigned long vmi_tsc_khz(void)
  63. {
  64. unsigned long long khz;
  65. khz = vmi_timer_ops.get_cycle_frequency();
  66. (void)do_div(khz, 1000);
  67. return khz;
  68. }
  69. static inline unsigned int vmi_get_timer_vector(void)
  70. {
  71. #ifdef CONFIG_X86_IO_APIC
  72. return FIRST_DEVICE_VECTOR;
  73. #else
  74. return FIRST_EXTERNAL_VECTOR;
  75. #endif
  76. }
  77. /** vmi clockchip */
  78. #ifdef CONFIG_X86_LOCAL_APIC
  79. static unsigned int startup_timer_irq(unsigned int irq)
  80. {
  81. unsigned long val = apic_read(APIC_LVTT);
  82. apic_write(APIC_LVTT, vmi_get_timer_vector());
  83. return (val & APIC_SEND_PENDING);
  84. }
  85. static void mask_timer_irq(unsigned int irq)
  86. {
  87. unsigned long val = apic_read(APIC_LVTT);
  88. apic_write(APIC_LVTT, val | APIC_LVT_MASKED);
  89. }
  90. static void unmask_timer_irq(unsigned int irq)
  91. {
  92. unsigned long val = apic_read(APIC_LVTT);
  93. apic_write(APIC_LVTT, val & ~APIC_LVT_MASKED);
  94. }
  95. static void ack_timer_irq(unsigned int irq)
  96. {
  97. ack_APIC_irq();
  98. }
  99. static struct irq_chip vmi_chip __read_mostly = {
  100. .name = "VMI-LOCAL",
  101. .startup = startup_timer_irq,
  102. .mask = mask_timer_irq,
  103. .unmask = unmask_timer_irq,
  104. .ack = ack_timer_irq
  105. };
  106. #endif
  107. /** vmi clockevent */
  108. #define VMI_ALARM_WIRED_IRQ0 0x00000000
  109. #define VMI_ALARM_WIRED_LVTT 0x00010000
  110. static int vmi_wiring = VMI_ALARM_WIRED_IRQ0;
  111. static inline int vmi_get_alarm_wiring(void)
  112. {
  113. return vmi_wiring;
  114. }
  115. static void vmi_timer_set_mode(enum clock_event_mode mode,
  116. struct clock_event_device *evt)
  117. {
  118. cycle_t now, cycles_per_hz;
  119. BUG_ON(!irqs_disabled());
  120. switch (mode) {
  121. case CLOCK_EVT_MODE_ONESHOT:
  122. case CLOCK_EVT_MODE_RESUME:
  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 | IRQF_TIMER,
  179. };
  180. static void __devinit vmi_time_init_clockevent(void)
  181. {
  182. cycle_t cycles_per_msec;
  183. struct clock_event_device *evt;
  184. int cpu = smp_processor_id();
  185. evt = &__get_cpu_var(local_events);
  186. /* Use cycles_per_msec since div_sc params are 32-bits. */
  187. cycles_per_msec = vmi_timer_ops.get_cycle_frequency();
  188. (void)do_div(cycles_per_msec, 1000);
  189. memcpy(evt, &vmi_clockevent, sizeof(*evt));
  190. /* Must pick .shift such that .mult fits in 32-bits. Choosing
  191. * .shift to be 22 allows 2^(32-22) cycles per nano-seconds
  192. * before overflow. */
  193. evt->mult = div_sc(cycles_per_msec, NSEC_PER_MSEC, evt->shift);
  194. /* Upper bound is clockevent's use of ulong for cycle deltas. */
  195. evt->max_delta_ns = clockevent_delta2ns(ULONG_MAX, evt);
  196. evt->min_delta_ns = clockevent_delta2ns(1, evt);
  197. evt->cpumask = cpumask_of(cpu);
  198. printk(KERN_WARNING "vmi: registering clock event %s. mult=%lu shift=%u\n",
  199. evt->name, evt->mult, evt->shift);
  200. clockevents_register_device(evt);
  201. }
  202. void __init vmi_time_init(void)
  203. {
  204. unsigned int cpu;
  205. /* Disable PIT: BIOSes start PIT CH0 with 18.2hz peridic. */
  206. outb_pit(0x3a, PIT_MODE); /* binary, mode 5, LSB/MSB, ch 0 */
  207. vmi_time_init_clockevent();
  208. setup_irq(0, &vmi_clock_action);
  209. for_each_possible_cpu(cpu)
  210. per_cpu(vector_irq, cpu)[vmi_get_timer_vector()] = 0;
  211. }
  212. #ifdef CONFIG_X86_LOCAL_APIC
  213. void __devinit vmi_time_bsp_init(void)
  214. {
  215. /*
  216. * On APIC systems, we want local timers to fire on each cpu. We do
  217. * this by programming LVTT to deliver timer events to the IRQ handler
  218. * for IRQ-0, since we can't re-use the APIC local timer handler
  219. * without interfering with that code.
  220. */
  221. clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL);
  222. local_irq_disable();
  223. #ifdef CONFIG_SMP
  224. /*
  225. * XXX handle_percpu_irq only defined for SMP; we need to switch over
  226. * to using it, since this is a local interrupt, which each CPU must
  227. * handle individually without locking out or dropping simultaneous
  228. * local timers on other CPUs. We also don't want to trigger the
  229. * quirk workaround code for interrupts which gets invoked from
  230. * handle_percpu_irq via eoi, so we use our own IRQ chip.
  231. */
  232. set_irq_chip_and_handler_name(0, &vmi_chip, handle_percpu_irq, "lvtt");
  233. #else
  234. set_irq_chip_and_handler_name(0, &vmi_chip, handle_edge_irq, "lvtt");
  235. #endif
  236. vmi_wiring = VMI_ALARM_WIRED_LVTT;
  237. apic_write(APIC_LVTT, vmi_get_timer_vector());
  238. local_irq_enable();
  239. clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
  240. }
  241. void __devinit vmi_time_ap_init(void)
  242. {
  243. vmi_time_init_clockevent();
  244. apic_write(APIC_LVTT, vmi_get_timer_vector());
  245. }
  246. #endif
  247. /** vmi clocksource */
  248. static struct clocksource clocksource_vmi;
  249. static cycle_t read_real_cycles(void)
  250. {
  251. cycle_t ret = (cycle_t)vmi_timer_ops.get_cycle_counter(VMI_CYCLES_REAL);
  252. return max(ret, clocksource_vmi.cycle_last);
  253. }
  254. static struct clocksource clocksource_vmi = {
  255. .name = "vmi-timer",
  256. .rating = 450,
  257. .read = read_real_cycles,
  258. .mask = CLOCKSOURCE_MASK(64),
  259. .mult = 0, /* to be set */
  260. .shift = 22,
  261. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  262. };
  263. static int __init init_vmi_clocksource(void)
  264. {
  265. cycle_t cycles_per_msec;
  266. if (!vmi_timer_ops.get_cycle_frequency)
  267. return 0;
  268. /* Use khz2mult rather than hz2mult since hz arg is only 32-bits. */
  269. cycles_per_msec = vmi_timer_ops.get_cycle_frequency();
  270. (void)do_div(cycles_per_msec, 1000);
  271. /* Note that clocksource.{mult, shift} converts in the opposite direction
  272. * as clockevents. */
  273. clocksource_vmi.mult = clocksource_khz2mult(cycles_per_msec,
  274. clocksource_vmi.shift);
  275. printk(KERN_WARNING "vmi: registering clock source khz=%lld\n", cycles_per_msec);
  276. return clocksource_register(&clocksource_vmi);
  277. }
  278. module_init(init_vmi_clocksource);