i8253.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * i8253 PIT clocksource
  4. */
  5. #include <linux/clockchips.h>
  6. #include <linux/init.h>
  7. #include <linux/io.h>
  8. #include <linux/spinlock.h>
  9. #include <linux/timex.h>
  10. #include <linux/module.h>
  11. #include <linux/i8253.h>
  12. #include <linux/smp.h>
  13. /*
  14. * Protects access to I/O ports
  15. *
  16. * 0040-0043 : timer0, i8253 / i8254
  17. * 0061-0061 : NMI Control Register which contains two speaker control bits.
  18. */
  19. DEFINE_RAW_SPINLOCK(i8253_lock);
  20. EXPORT_SYMBOL(i8253_lock);
  21. #ifdef CONFIG_CLKSRC_I8253
  22. /*
  23. * Since the PIT overflows every tick, its not very useful
  24. * to just read by itself. So use jiffies to emulate a free
  25. * running counter:
  26. */
  27. static u64 i8253_read(struct clocksource *cs)
  28. {
  29. static int old_count;
  30. static u32 old_jifs;
  31. unsigned long flags;
  32. int count;
  33. u32 jifs;
  34. raw_spin_lock_irqsave(&i8253_lock, flags);
  35. /*
  36. * Although our caller may have the read side of jiffies_lock,
  37. * this is now a seqlock, and we are cheating in this routine
  38. * by having side effects on state that we cannot undo if
  39. * there is a collision on the seqlock and our caller has to
  40. * retry. (Namely, old_jifs and old_count.) So we must treat
  41. * jiffies as volatile despite the lock. We read jiffies
  42. * before latching the timer count to guarantee that although
  43. * the jiffies value might be older than the count (that is,
  44. * the counter may underflow between the last point where
  45. * jiffies was incremented and the point where we latch the
  46. * count), it cannot be newer.
  47. */
  48. jifs = jiffies;
  49. outb_p(0x00, PIT_MODE); /* latch the count ASAP */
  50. count = inb_p(PIT_CH0); /* read the latched count */
  51. count |= inb_p(PIT_CH0) << 8;
  52. /* VIA686a test code... reset the latch if count > max + 1 */
  53. if (count > PIT_LATCH) {
  54. outb_p(0x34, PIT_MODE);
  55. outb_p(PIT_LATCH & 0xff, PIT_CH0);
  56. outb_p(PIT_LATCH >> 8, PIT_CH0);
  57. count = PIT_LATCH - 1;
  58. }
  59. /*
  60. * It's possible for count to appear to go the wrong way for a
  61. * couple of reasons:
  62. *
  63. * 1. The timer counter underflows, but we haven't handled the
  64. * resulting interrupt and incremented jiffies yet.
  65. * 2. Hardware problem with the timer, not giving us continuous time,
  66. * the counter does small "jumps" upwards on some Pentium systems,
  67. * (see c't 95/10 page 335 for Neptun bug.)
  68. *
  69. * Previous attempts to handle these cases intelligently were
  70. * buggy, so we just do the simple thing now.
  71. */
  72. if (count > old_count && jifs == old_jifs)
  73. count = old_count;
  74. old_count = count;
  75. old_jifs = jifs;
  76. raw_spin_unlock_irqrestore(&i8253_lock, flags);
  77. count = (PIT_LATCH - 1) - count;
  78. return (u64)(jifs * PIT_LATCH) + count;
  79. }
  80. static struct clocksource i8253_cs = {
  81. .name = "pit",
  82. .rating = 110,
  83. .read = i8253_read,
  84. .mask = CLOCKSOURCE_MASK(32),
  85. };
  86. int __init clocksource_i8253_init(void)
  87. {
  88. return clocksource_register_hz(&i8253_cs, PIT_TICK_RATE);
  89. }
  90. #endif
  91. #ifdef CONFIG_CLKEVT_I8253
  92. void clockevent_i8253_disable(void)
  93. {
  94. guard(raw_spinlock_irqsave)(&i8253_lock);
  95. /*
  96. * Writing the MODE register should stop the counter, according to
  97. * the datasheet. This appears to work on real hardware (well, on
  98. * modern Intel and AMD boxes; I didn't dig the Pegasos out of the
  99. * shed).
  100. *
  101. * However, some virtual implementations differ, and the MODE change
  102. * doesn't have any effect until either the counter is written (KVM
  103. * in-kernel PIT) or the next interrupt (QEMU). And in those cases,
  104. * it may not stop the *count*, only the interrupts. Although in
  105. * the virt case, that probably doesn't matter, as the value of the
  106. * counter will only be calculated on demand if the guest reads it;
  107. * it's the interrupts which cause steal time.
  108. *
  109. * Hyper-V apparently has a bug where even in mode 0, the IRQ keeps
  110. * firing repeatedly if the counter is running. But it *does* do the
  111. * right thing when the MODE register is written.
  112. *
  113. * So: write the MODE and then load the counter, which ensures that
  114. * the IRQ is stopped on those buggy virt implementations. And then
  115. * write the MODE again, which is the right way to stop it.
  116. */
  117. outb_p(0x30, PIT_MODE);
  118. outb_p(0, PIT_CH0);
  119. outb_p(0, PIT_CH0);
  120. outb_p(0x30, PIT_MODE);
  121. }
  122. static int pit_shutdown(struct clock_event_device *evt)
  123. {
  124. if (!clockevent_state_oneshot(evt) && !clockevent_state_periodic(evt))
  125. return 0;
  126. clockevent_i8253_disable();
  127. return 0;
  128. }
  129. static int pit_set_oneshot(struct clock_event_device *evt)
  130. {
  131. raw_spin_lock(&i8253_lock);
  132. outb_p(0x38, PIT_MODE);
  133. raw_spin_unlock(&i8253_lock);
  134. return 0;
  135. }
  136. static int pit_set_periodic(struct clock_event_device *evt)
  137. {
  138. raw_spin_lock(&i8253_lock);
  139. /* binary, mode 2, LSB/MSB, ch 0 */
  140. outb_p(0x34, PIT_MODE);
  141. outb_p(PIT_LATCH & 0xff, PIT_CH0); /* LSB */
  142. outb_p(PIT_LATCH >> 8, PIT_CH0); /* MSB */
  143. raw_spin_unlock(&i8253_lock);
  144. return 0;
  145. }
  146. /*
  147. * Program the next event in oneshot mode
  148. *
  149. * Delta is given in PIT ticks
  150. */
  151. static int pit_next_event(unsigned long delta, struct clock_event_device *evt)
  152. {
  153. raw_spin_lock(&i8253_lock);
  154. outb_p(delta & 0xff , PIT_CH0); /* LSB */
  155. outb_p(delta >> 8 , PIT_CH0); /* MSB */
  156. raw_spin_unlock(&i8253_lock);
  157. return 0;
  158. }
  159. /*
  160. * On UP the PIT can serve all of the possible timer functions. On SMP systems
  161. * it can be solely used for the global tick.
  162. */
  163. struct clock_event_device i8253_clockevent = {
  164. .name = "pit",
  165. .features = CLOCK_EVT_FEAT_PERIODIC,
  166. .set_state_shutdown = pit_shutdown,
  167. .set_state_periodic = pit_set_periodic,
  168. .set_next_event = pit_next_event,
  169. };
  170. /*
  171. * Initialize the conversion factor and the min/max deltas of the clock event
  172. * structure and register the clock event source with the framework.
  173. */
  174. void __init clockevent_i8253_init(bool oneshot)
  175. {
  176. if (oneshot) {
  177. i8253_clockevent.features |= CLOCK_EVT_FEAT_ONESHOT;
  178. i8253_clockevent.set_state_oneshot = pit_set_oneshot;
  179. }
  180. /*
  181. * Start pit with the boot cpu mask. x86 might make it global
  182. * when it is used as broadcast device later.
  183. */
  184. i8253_clockevent.cpumask = cpumask_of(smp_processor_id());
  185. clockevents_config_and_register(&i8253_clockevent, PIT_TICK_RATE,
  186. 0xF, 0x7FFF);
  187. }
  188. #endif