timer_pm.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 <linux/pci.h>
  17. #include <asm/types.h>
  18. #include <asm/timer.h>
  19. #include <asm/smp.h>
  20. #include <asm/io.h>
  21. #include <asm/arch_hooks.h>
  22. #include <linux/timex.h>
  23. #include "mach_timer.h"
  24. /* Number of PMTMR ticks expected during calibration run */
  25. #define PMTMR_TICKS_PER_SEC 3579545
  26. #define PMTMR_EXPECTED_RATE \
  27. ((CALIBRATE_LATCH * (PMTMR_TICKS_PER_SEC >> 10)) / (CLOCK_TICK_RATE>>10))
  28. /* The I/O port the PMTMR resides at.
  29. * The location is detected during setup_arch(),
  30. * in arch/i386/acpi/boot.c */
  31. u32 pmtmr_ioport = 0;
  32. /* value of the Power timer at last timer interrupt */
  33. static u32 offset_tick;
  34. static u32 offset_delay;
  35. static unsigned long long monotonic_base;
  36. static seqlock_t monotonic_lock = SEQLOCK_UNLOCKED;
  37. #define ACPI_PM_MASK 0xFFFFFF /* limit it to 24 bits */
  38. static int pmtmr_need_workaround __read_mostly = 1;
  39. /*helper function to safely read acpi pm timesource*/
  40. static inline u32 read_pmtmr(void)
  41. {
  42. if (pmtmr_need_workaround) {
  43. u32 v1, v2, v3;
  44. /* It has been reported that because of various broken
  45. * chipsets (ICH4, PIIX4 and PIIX4E) where the ACPI PM time
  46. * source is not latched, so you must read it multiple
  47. * times to insure a safe value is read.
  48. */
  49. do {
  50. v1 = inl(pmtmr_ioport);
  51. v2 = inl(pmtmr_ioport);
  52. v3 = inl(pmtmr_ioport);
  53. } while ((v1 > v2 && v1 < v3) || (v2 > v3 && v2 < v1)
  54. || (v3 > v1 && v3 < v2));
  55. /* mask the output to 24 bits */
  56. return v2 & ACPI_PM_MASK;
  57. }
  58. return inl(pmtmr_ioport) & ACPI_PM_MASK;
  59. }
  60. /*
  61. * Some boards have the PMTMR running way too fast. We check
  62. * the PMTMR rate against PIT channel 2 to catch these cases.
  63. */
  64. static int verify_pmtmr_rate(void)
  65. {
  66. u32 value1, value2;
  67. unsigned long count, delta;
  68. mach_prepare_counter();
  69. value1 = read_pmtmr();
  70. mach_countup(&count);
  71. value2 = read_pmtmr();
  72. delta = (value2 - value1) & ACPI_PM_MASK;
  73. /* Check that the PMTMR delta is within 5% of what we expect */
  74. if (delta < (PMTMR_EXPECTED_RATE * 19) / 20 ||
  75. delta > (PMTMR_EXPECTED_RATE * 21) / 20) {
  76. printk(KERN_INFO "PM-Timer running at invalid rate: %lu%% of normal - aborting.\n", 100UL * delta / PMTMR_EXPECTED_RATE);
  77. return -1;
  78. }
  79. return 0;
  80. }
  81. static int init_pmtmr(char* override)
  82. {
  83. u32 value1, value2;
  84. unsigned int i;
  85. if (override[0] && strncmp(override,"pmtmr",5))
  86. return -ENODEV;
  87. if (!pmtmr_ioport)
  88. return -ENODEV;
  89. /* we use the TSC for delay_pmtmr, so make sure it exists */
  90. if (!cpu_has_tsc)
  91. return -ENODEV;
  92. /* "verify" this timing source */
  93. value1 = read_pmtmr();
  94. for (i = 0; i < 10000; i++) {
  95. value2 = read_pmtmr();
  96. if (value2 == value1)
  97. continue;
  98. if (value2 > value1)
  99. goto pm_good;
  100. if ((value2 < value1) && ((value2) < 0xFFF))
  101. goto pm_good;
  102. printk(KERN_INFO "PM-Timer had inconsistent results: 0x%#x, 0x%#x - aborting.\n", value1, value2);
  103. return -EINVAL;
  104. }
  105. printk(KERN_INFO "PM-Timer had no reasonable result: 0x%#x - aborting.\n", value1);
  106. return -ENODEV;
  107. pm_good:
  108. if (verify_pmtmr_rate() != 0)
  109. return -ENODEV;
  110. init_cpu_khz();
  111. return 0;
  112. }
  113. static inline u32 cyc2us(u32 cycles)
  114. {
  115. /* The Power Management Timer ticks at 3.579545 ticks per microsecond.
  116. * 1 / PM_TIMER_FREQUENCY == 0.27936511 =~ 286/1024 [error: 0.024%]
  117. *
  118. * Even with HZ = 100, delta is at maximum 35796 ticks, so it can
  119. * easily be multiplied with 286 (=0x11E) without having to fear
  120. * u32 overflows.
  121. */
  122. cycles *= 286;
  123. return (cycles >> 10);
  124. }
  125. /*
  126. * this gets called during each timer interrupt
  127. * - Called while holding the writer xtime_lock
  128. */
  129. static void mark_offset_pmtmr(void)
  130. {
  131. u32 lost, delta, last_offset;
  132. static int first_run = 1;
  133. last_offset = offset_tick;
  134. write_seqlock(&monotonic_lock);
  135. offset_tick = read_pmtmr();
  136. /* calculate tick interval */
  137. delta = (offset_tick - last_offset) & ACPI_PM_MASK;
  138. /* convert to usecs */
  139. delta = cyc2us(delta);
  140. /* update the monotonic base value */
  141. monotonic_base += delta * NSEC_PER_USEC;
  142. write_sequnlock(&monotonic_lock);
  143. /* convert to ticks */
  144. delta += offset_delay;
  145. lost = delta / (USEC_PER_SEC / HZ);
  146. offset_delay = delta % (USEC_PER_SEC / HZ);
  147. /* compensate for lost ticks */
  148. if (lost >= 2)
  149. jiffies_64 += lost - 1;
  150. /* don't calculate delay for first run,
  151. or if we've got less then a tick */
  152. if (first_run || (lost < 1)) {
  153. first_run = 0;
  154. offset_delay = 0;
  155. }
  156. }
  157. static int pmtmr_resume(void)
  158. {
  159. write_seqlock(&monotonic_lock);
  160. /* Assume this is the last mark offset time */
  161. offset_tick = read_pmtmr();
  162. write_sequnlock(&monotonic_lock);
  163. return 0;
  164. }
  165. static unsigned long long monotonic_clock_pmtmr(void)
  166. {
  167. u32 last_offset, this_offset;
  168. unsigned long long base, ret;
  169. unsigned seq;
  170. /* atomically read monotonic base & last_offset */
  171. do {
  172. seq = read_seqbegin(&monotonic_lock);
  173. last_offset = offset_tick;
  174. base = monotonic_base;
  175. } while (read_seqretry(&monotonic_lock, seq));
  176. /* Read the pmtmr */
  177. this_offset = read_pmtmr();
  178. /* convert to nanoseconds */
  179. ret = (this_offset - last_offset) & ACPI_PM_MASK;
  180. ret = base + (cyc2us(ret) * NSEC_PER_USEC);
  181. return ret;
  182. }
  183. static void delay_pmtmr(unsigned long loops)
  184. {
  185. unsigned long bclock, now;
  186. rdtscl(bclock);
  187. do
  188. {
  189. rep_nop();
  190. rdtscl(now);
  191. } while ((now-bclock) < loops);
  192. }
  193. /*
  194. * get the offset (in microseconds) from the last call to mark_offset()
  195. * - Called holding a reader xtime_lock
  196. */
  197. static unsigned long get_offset_pmtmr(void)
  198. {
  199. u32 now, offset, delta = 0;
  200. offset = offset_tick;
  201. now = read_pmtmr();
  202. delta = (now - offset)&ACPI_PM_MASK;
  203. return (unsigned long) offset_delay + cyc2us(delta);
  204. }
  205. /* acpi timer_opts struct */
  206. static struct timer_opts timer_pmtmr = {
  207. .name = "pmtmr",
  208. .mark_offset = mark_offset_pmtmr,
  209. .get_offset = get_offset_pmtmr,
  210. .monotonic_clock = monotonic_clock_pmtmr,
  211. .delay = delay_pmtmr,
  212. .read_timer = read_timer_tsc,
  213. .resume = pmtmr_resume,
  214. };
  215. struct init_timer_opts __initdata timer_pmtmr_init = {
  216. .init = init_pmtmr,
  217. .opts = &timer_pmtmr,
  218. };
  219. #ifdef CONFIG_PCI
  220. /*
  221. * PIIX4 Errata:
  222. *
  223. * The power management timer may return improper results when read.
  224. * Although the timer value settles properly after incrementing,
  225. * while incrementing there is a 3 ns window every 69.8 ns where the
  226. * timer value is indeterminate (a 4.2% chance that the data will be
  227. * incorrect when read). As a result, the ACPI free running count up
  228. * timer specification is violated due to erroneous reads.
  229. */
  230. static int __init pmtmr_bug_check(void)
  231. {
  232. static struct pci_device_id gray_list[] __initdata = {
  233. /* these chipsets may have bug. */
  234. { PCI_DEVICE(PCI_VENDOR_ID_INTEL,
  235. PCI_DEVICE_ID_INTEL_82801DB_0) },
  236. { },
  237. };
  238. struct pci_dev *dev;
  239. int pmtmr_has_bug = 0;
  240. u8 rev;
  241. if (cur_timer != &timer_pmtmr || !pmtmr_need_workaround)
  242. return 0;
  243. dev = pci_get_device(PCI_VENDOR_ID_INTEL,
  244. PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
  245. if (dev) {
  246. pci_read_config_byte(dev, PCI_REVISION_ID, &rev);
  247. /* the bug has been fixed in PIIX4M */
  248. if (rev < 3) {
  249. printk(KERN_WARNING "* Found PM-Timer Bug on this "
  250. "chipset. Due to workarounds for a bug,\n"
  251. "* this time source is slow. Consider trying "
  252. "other time sources (clock=)\n");
  253. pmtmr_has_bug = 1;
  254. }
  255. pci_dev_put(dev);
  256. }
  257. if (pci_dev_present(gray_list)) {
  258. printk(KERN_WARNING "* This chipset may have PM-Timer Bug. Due"
  259. " to workarounds for a bug,\n"
  260. "* this time source is slow. If you are sure your timer"
  261. " does not have\n"
  262. "* this bug, please use \"pmtmr_good\" to disable the "
  263. "workaround\n");
  264. pmtmr_has_bug = 1;
  265. }
  266. if (!pmtmr_has_bug)
  267. pmtmr_need_workaround = 0;
  268. return 0;
  269. }
  270. device_initcall(pmtmr_bug_check);
  271. #endif
  272. static int __init pmtr_good_setup(char *__str)
  273. {
  274. pmtmr_need_workaround = 0;
  275. return 1;
  276. }
  277. __setup("pmtmr_good", pmtr_good_setup);
  278. MODULE_LICENSE("GPL");
  279. MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
  280. MODULE_DESCRIPTION("Power Management Timer (PMTMR) as primary timing source for x86");