timer_pm.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * (C) Dominik Brodowski <linux@brodo.de> 2003
  3. *
  4. * Driver to use the Power Management Timer (PMTMR) available in some
  5. * southbridges as primary timing source for the Linux kernel.
  6. *
  7. * Based on parts of linux/drivers/acpi/hardware/hwtimer.c, timer_pit.c,
  8. * timer_hpet.c, and on Arjan van de Ven's implementation for 2.4.
  9. *
  10. * This file is licensed under the GPL v2.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/device.h>
  15. #include <linux/init.h>
  16. #include <asm/types.h>
  17. #include <asm/timer.h>
  18. #include <asm/smp.h>
  19. #include <asm/io.h>
  20. #include <asm/arch_hooks.h>
  21. #include <linux/timex.h>
  22. #include "mach_timer.h"
  23. /* Number of PMTMR ticks expected during calibration run */
  24. #define PMTMR_TICKS_PER_SEC 3579545
  25. #define PMTMR_EXPECTED_RATE \
  26. ((CALIBRATE_LATCH * (PMTMR_TICKS_PER_SEC >> 10)) / (CLOCK_TICK_RATE>>10))
  27. /* The I/O port the PMTMR resides at.
  28. * The location is detected during setup_arch(),
  29. * in arch/i386/acpi/boot.c */
  30. u32 pmtmr_ioport = 0;
  31. /* value of the Power timer at last timer interrupt */
  32. static u32 offset_tick;
  33. static u32 offset_delay;
  34. static unsigned long long monotonic_base;
  35. static seqlock_t monotonic_lock = SEQLOCK_UNLOCKED;
  36. #define ACPI_PM_MASK 0xFFFFFF /* limit it to 24 bits */
  37. /*helper function to safely read acpi pm timesource*/
  38. static inline u32 read_pmtmr(void)
  39. {
  40. u32 v1=0,v2=0,v3=0;
  41. /* It has been reported that because of various broken
  42. * chipsets (ICH4, PIIX4 and PIIX4E) where the ACPI PM time
  43. * source is not latched, so you must read it multiple
  44. * times to insure a safe value is read.
  45. */
  46. do {
  47. v1 = inl(pmtmr_ioport);
  48. v2 = inl(pmtmr_ioport);
  49. v3 = inl(pmtmr_ioport);
  50. } while ((v1 > v2 && v1 < v3) || (v2 > v3 && v2 < v1)
  51. || (v3 > v1 && v3 < v2));
  52. /* mask the output to 24 bits */
  53. return v2 & ACPI_PM_MASK;
  54. }
  55. /*
  56. * Some boards have the PMTMR running way too fast. We check
  57. * the PMTMR rate against PIT channel 2 to catch these cases.
  58. */
  59. static int verify_pmtmr_rate(void)
  60. {
  61. u32 value1, value2;
  62. unsigned long count, delta;
  63. mach_prepare_counter();
  64. value1 = read_pmtmr();
  65. mach_countup(&count);
  66. value2 = read_pmtmr();
  67. delta = (value2 - value1) & ACPI_PM_MASK;
  68. /* Check that the PMTMR delta is within 5% of what we expect */
  69. if (delta < (PMTMR_EXPECTED_RATE * 19) / 20 ||
  70. delta > (PMTMR_EXPECTED_RATE * 21) / 20) {
  71. printk(KERN_INFO "PM-Timer running at invalid rate: %lu%% of normal - aborting.\n", 100UL * delta / PMTMR_EXPECTED_RATE);
  72. return -1;
  73. }
  74. return 0;
  75. }
  76. static int init_pmtmr(char* override)
  77. {
  78. u32 value1, value2;
  79. unsigned int i;
  80. if (override[0] && strncmp(override,"pmtmr",5))
  81. return -ENODEV;
  82. if (!pmtmr_ioport)
  83. return -ENODEV;
  84. /* we use the TSC for delay_pmtmr, so make sure it exists */
  85. if (!cpu_has_tsc)
  86. return -ENODEV;
  87. /* "verify" this timing source */
  88. value1 = read_pmtmr();
  89. for (i = 0; i < 10000; i++) {
  90. value2 = read_pmtmr();
  91. if (value2 == value1)
  92. continue;
  93. if (value2 > value1)
  94. goto pm_good;
  95. if ((value2 < value1) && ((value2) < 0xFFF))
  96. goto pm_good;
  97. printk(KERN_INFO "PM-Timer had inconsistent results: 0x%#x, 0x%#x - aborting.\n", value1, value2);
  98. return -EINVAL;
  99. }
  100. printk(KERN_INFO "PM-Timer had no reasonable result: 0x%#x - aborting.\n", value1);
  101. return -ENODEV;
  102. pm_good:
  103. if (verify_pmtmr_rate() != 0)
  104. return -ENODEV;
  105. init_cpu_khz();
  106. return 0;
  107. }
  108. static inline u32 cyc2us(u32 cycles)
  109. {
  110. /* The Power Management Timer ticks at 3.579545 ticks per microsecond.
  111. * 1 / PM_TIMER_FREQUENCY == 0.27936511 =~ 286/1024 [error: 0.024%]
  112. *
  113. * Even with HZ = 100, delta is at maximum 35796 ticks, so it can
  114. * easily be multiplied with 286 (=0x11E) without having to fear
  115. * u32 overflows.
  116. */
  117. cycles *= 286;
  118. return (cycles >> 10);
  119. }
  120. /*
  121. * this gets called during each timer interrupt
  122. * - Called while holding the writer xtime_lock
  123. */
  124. static void mark_offset_pmtmr(void)
  125. {
  126. u32 lost, delta, last_offset;
  127. static int first_run = 1;
  128. last_offset = offset_tick;
  129. write_seqlock(&monotonic_lock);
  130. offset_tick = read_pmtmr();
  131. /* calculate tick interval */
  132. delta = (offset_tick - last_offset) & ACPI_PM_MASK;
  133. /* convert to usecs */
  134. delta = cyc2us(delta);
  135. /* update the monotonic base value */
  136. monotonic_base += delta * NSEC_PER_USEC;
  137. write_sequnlock(&monotonic_lock);
  138. /* convert to ticks */
  139. delta += offset_delay;
  140. lost = delta / (USEC_PER_SEC / HZ);
  141. offset_delay = delta % (USEC_PER_SEC / HZ);
  142. /* compensate for lost ticks */
  143. if (lost >= 2)
  144. jiffies_64 += lost - 1;
  145. /* don't calculate delay for first run,
  146. or if we've got less then a tick */
  147. if (first_run || (lost < 1)) {
  148. first_run = 0;
  149. offset_delay = 0;
  150. }
  151. }
  152. static unsigned long long monotonic_clock_pmtmr(void)
  153. {
  154. u32 last_offset, this_offset;
  155. unsigned long long base, ret;
  156. unsigned seq;
  157. /* atomically read monotonic base & last_offset */
  158. do {
  159. seq = read_seqbegin(&monotonic_lock);
  160. last_offset = offset_tick;
  161. base = monotonic_base;
  162. } while (read_seqretry(&monotonic_lock, seq));
  163. /* Read the pmtmr */
  164. this_offset = read_pmtmr();
  165. /* convert to nanoseconds */
  166. ret = (this_offset - last_offset) & ACPI_PM_MASK;
  167. ret = base + (cyc2us(ret) * NSEC_PER_USEC);
  168. return ret;
  169. }
  170. static void delay_pmtmr(unsigned long loops)
  171. {
  172. unsigned long bclock, now;
  173. rdtscl(bclock);
  174. do
  175. {
  176. rep_nop();
  177. rdtscl(now);
  178. } while ((now-bclock) < loops);
  179. }
  180. /*
  181. * get the offset (in microseconds) from the last call to mark_offset()
  182. * - Called holding a reader xtime_lock
  183. */
  184. static unsigned long get_offset_pmtmr(void)
  185. {
  186. u32 now, offset, delta = 0;
  187. offset = offset_tick;
  188. now = read_pmtmr();
  189. delta = (now - offset)&ACPI_PM_MASK;
  190. return (unsigned long) offset_delay + cyc2us(delta);
  191. }
  192. /* acpi timer_opts struct */
  193. static struct timer_opts timer_pmtmr = {
  194. .name = "pmtmr",
  195. .mark_offset = mark_offset_pmtmr,
  196. .get_offset = get_offset_pmtmr,
  197. .monotonic_clock = monotonic_clock_pmtmr,
  198. .delay = delay_pmtmr,
  199. .read_timer = read_timer_tsc,
  200. };
  201. struct init_timer_opts __initdata timer_pmtmr_init = {
  202. .init = init_pmtmr,
  203. .opts = &timer_pmtmr,
  204. };
  205. MODULE_LICENSE("GPL");
  206. MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
  207. MODULE_DESCRIPTION("Power Management Timer (PMTMR) as primary timing source for x86");