vmiclock_32.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 <irq_vectors.h>
  35. #include "io_ports.h"
  36. #define VMI_ONESHOT (VMI_ALARM_IS_ONESHOT | VMI_CYCLES_REAL | vmi_get_alarm_wiring())
  37. #define VMI_PERIODIC (VMI_ALARM_IS_PERIODIC | VMI_CYCLES_REAL | vmi_get_alarm_wiring())
  38. static DEFINE_PER_CPU(struct clock_event_device, local_events);
  39. static inline u32 vmi_counter(u32 flags)
  40. {
  41. /* Given VMI_ONESHOT or VMI_PERIODIC, return the corresponding
  42. * cycle counter. */
  43. return flags & VMI_ALARM_COUNTER_MASK;
  44. }
  45. /* paravirt_ops.get_wallclock = vmi_get_wallclock */
  46. unsigned long vmi_get_wallclock(void)
  47. {
  48. unsigned long long wallclock;
  49. wallclock = vmi_timer_ops.get_wallclock(); // nsec
  50. (void)do_div(wallclock, 1000000000); // sec
  51. return wallclock;
  52. }
  53. /* paravirt_ops.set_wallclock = vmi_set_wallclock */
  54. int vmi_set_wallclock(unsigned long now)
  55. {
  56. return 0;
  57. }
  58. /* paravirt_ops.sched_clock = vmi_sched_clock */
  59. unsigned long long vmi_sched_clock(void)
  60. {
  61. return cycles_2_ns(vmi_timer_ops.get_cycle_counter(VMI_CYCLES_AVAILABLE));
  62. }
  63. /* paravirt_ops.get_cpu_khz = vmi_cpu_khz */
  64. unsigned long vmi_cpu_khz(void)
  65. {
  66. unsigned long long khz;
  67. khz = vmi_timer_ops.get_cycle_frequency();
  68. (void)do_div(khz, 1000);
  69. return khz;
  70. }
  71. static inline unsigned int vmi_get_timer_vector(void)
  72. {
  73. #ifdef CONFIG_X86_IO_APIC
  74. return FIRST_DEVICE_VECTOR;
  75. #else
  76. return FIRST_EXTERNAL_VECTOR;
  77. #endif
  78. }
  79. /** vmi clockchip */
  80. #ifdef CONFIG_X86_LOCAL_APIC
  81. static unsigned int startup_timer_irq(unsigned int irq)
  82. {
  83. unsigned long val = apic_read(APIC_LVTT);
  84. apic_write(APIC_LVTT, vmi_get_timer_vector());
  85. return (val & APIC_SEND_PENDING);
  86. }
  87. static void mask_timer_irq(unsigned int irq)
  88. {
  89. unsigned long val = apic_read(APIC_LVTT);
  90. apic_write(APIC_LVTT, val | APIC_LVT_MASKED);
  91. }
  92. static void unmask_timer_irq(unsigned int irq)
  93. {
  94. unsigned long val = apic_read(APIC_LVTT);
  95. apic_write(APIC_LVTT, val & ~APIC_LVT_MASKED);
  96. }
  97. static void ack_timer_irq(unsigned int irq)
  98. {
  99. ack_APIC_irq();
  100. }
  101. static struct irq_chip vmi_chip __read_mostly = {
  102. .name = "VMI-LOCAL",
  103. .startup = startup_timer_irq,
  104. .mask = mask_timer_irq,
  105. .unmask = unmask_timer_irq,
  106. .ack = ack_timer_irq
  107. };
  108. #endif
  109. /** vmi clockevent */
  110. #define VMI_ALARM_WIRED_IRQ0 0x00000000
  111. #define VMI_ALARM_WIRED_LVTT 0x00010000
  112. static int vmi_wiring = VMI_ALARM_WIRED_IRQ0;
  113. static inline int vmi_get_alarm_wiring(void)
  114. {
  115. return vmi_wiring;
  116. }
  117. static void vmi_timer_set_mode(enum clock_event_mode mode,
  118. struct clock_event_device *evt)
  119. {
  120. cycle_t now, cycles_per_hz;
  121. BUG_ON(!irqs_disabled());
  122. switch (mode) {
  123. case CLOCK_EVT_MODE_ONESHOT:
  124. case CLOCK_EVT_MODE_RESUME:
  125. break;
  126. case CLOCK_EVT_MODE_PERIODIC:
  127. cycles_per_hz = vmi_timer_ops.get_cycle_frequency();
  128. (void)do_div(cycles_per_hz, HZ);
  129. now = vmi_timer_ops.get_cycle_counter(vmi_counter(VMI_PERIODIC));
  130. vmi_timer_ops.set_alarm(VMI_PERIODIC, now, cycles_per_hz);
  131. break;
  132. case CLOCK_EVT_MODE_UNUSED:
  133. case CLOCK_EVT_MODE_SHUTDOWN:
  134. switch (evt->mode) {
  135. case CLOCK_EVT_MODE_ONESHOT:
  136. vmi_timer_ops.cancel_alarm(VMI_ONESHOT);
  137. break;
  138. case CLOCK_EVT_MODE_PERIODIC:
  139. vmi_timer_ops.cancel_alarm(VMI_PERIODIC);
  140. break;
  141. default:
  142. break;
  143. }
  144. break;
  145. default:
  146. break;
  147. }
  148. }
  149. static int vmi_timer_next_event(unsigned long delta,
  150. struct clock_event_device *evt)
  151. {
  152. /* Unfortunately, set_next_event interface only passes relative
  153. * expiry, but we want absolute expiry. It'd be better if were
  154. * were passed an aboslute expiry, since a bunch of time may
  155. * have been stolen between the time the delta is computed and
  156. * when we set the alarm below. */
  157. cycle_t now = vmi_timer_ops.get_cycle_counter(vmi_counter(VMI_ONESHOT));
  158. BUG_ON(evt->mode != CLOCK_EVT_MODE_ONESHOT);
  159. vmi_timer_ops.set_alarm(VMI_ONESHOT, now + delta, 0);
  160. return 0;
  161. }
  162. static struct clock_event_device vmi_clockevent = {
  163. .name = "vmi-timer",
  164. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  165. .shift = 22,
  166. .set_mode = vmi_timer_set_mode,
  167. .set_next_event = vmi_timer_next_event,
  168. .rating = 1000,
  169. .irq = 0,
  170. };
  171. static irqreturn_t vmi_timer_interrupt(int irq, void *dev_id)
  172. {
  173. struct clock_event_device *evt = &__get_cpu_var(local_events);
  174. evt->event_handler(evt);
  175. return IRQ_HANDLED;
  176. }
  177. static struct irqaction vmi_clock_action = {
  178. .name = "vmi-timer",
  179. .handler = vmi_timer_interrupt,
  180. .flags = IRQF_DISABLED | IRQF_NOBALANCING,
  181. .mask = CPU_MASK_ALL,
  182. };
  183. static void __devinit vmi_time_init_clockevent(void)
  184. {
  185. cycle_t cycles_per_msec;
  186. struct clock_event_device *evt;
  187. int cpu = smp_processor_id();
  188. evt = &__get_cpu_var(local_events);
  189. /* Use cycles_per_msec since div_sc params are 32-bits. */
  190. cycles_per_msec = vmi_timer_ops.get_cycle_frequency();
  191. (void)do_div(cycles_per_msec, 1000);
  192. memcpy(evt, &vmi_clockevent, sizeof(*evt));
  193. /* Must pick .shift such that .mult fits in 32-bits. Choosing
  194. * .shift to be 22 allows 2^(32-22) cycles per nano-seconds
  195. * before overflow. */
  196. evt->mult = div_sc(cycles_per_msec, NSEC_PER_MSEC, evt->shift);
  197. /* Upper bound is clockevent's use of ulong for cycle deltas. */
  198. evt->max_delta_ns = clockevent_delta2ns(ULONG_MAX, evt);
  199. evt->min_delta_ns = clockevent_delta2ns(1, evt);
  200. evt->cpumask = cpumask_of_cpu(cpu);
  201. printk(KERN_WARNING "vmi: registering clock event %s. mult=%lu shift=%u\n",
  202. evt->name, evt->mult, evt->shift);
  203. clockevents_register_device(evt);
  204. }
  205. void __init vmi_time_init(void)
  206. {
  207. /* Disable PIT: BIOSes start PIT CH0 with 18.2hz peridic. */
  208. outb_p(0x3a, PIT_MODE); /* binary, mode 5, LSB/MSB, ch 0 */
  209. vmi_time_init_clockevent();
  210. setup_irq(0, &vmi_clock_action);
  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_X86_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 cycle_t read_real_cycles(void)
  249. {
  250. return vmi_timer_ops.get_cycle_counter(VMI_CYCLES_REAL);
  251. }
  252. static struct clocksource clocksource_vmi = {
  253. .name = "vmi-timer",
  254. .rating = 450,
  255. .read = read_real_cycles,
  256. .mask = CLOCKSOURCE_MASK(64),
  257. .mult = 0, /* to be set */
  258. .shift = 22,
  259. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  260. };
  261. static int __init init_vmi_clocksource(void)
  262. {
  263. cycle_t cycles_per_msec;
  264. if (!vmi_timer_ops.get_cycle_frequency)
  265. return 0;
  266. /* Use khz2mult rather than hz2mult since hz arg is only 32-bits. */
  267. cycles_per_msec = vmi_timer_ops.get_cycle_frequency();
  268. (void)do_div(cycles_per_msec, 1000);
  269. /* Note that clocksource.{mult, shift} converts in the opposite direction
  270. * as clockevents. */
  271. clocksource_vmi.mult = clocksource_khz2mult(cycles_per_msec,
  272. clocksource_vmi.shift);
  273. printk(KERN_WARNING "vmi: registering clock source khz=%lld\n", cycles_per_msec);
  274. return clocksource_register(&clocksource_vmi);
  275. }
  276. module_init(init_vmi_clocksource);