acpi_pm.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * linux/drivers/clocksource/acpi_pm.c
  3. *
  4. * This file contains the ACPI PM based clocksource.
  5. *
  6. * This code was largely moved from the i386 timer_pm.c file
  7. * which was (C) Dominik Brodowski <linux@brodo.de> 2003
  8. * and contained the following comments:
  9. *
  10. * Driver to use the Power Management Timer (PMTMR) available in some
  11. * southbridges as primary timing source for the Linux kernel.
  12. *
  13. * Based on parts of linux/drivers/acpi/hardware/hwtimer.c, timer_pit.c,
  14. * timer_hpet.c, and on Arjan van de Ven's implementation for 2.4.
  15. *
  16. * This file is licensed under the GPL v2.
  17. */
  18. #include <linux/acpi_pmtmr.h>
  19. #include <linux/clocksource.h>
  20. #include <linux/timex.h>
  21. #include <linux/errno.h>
  22. #include <linux/init.h>
  23. #include <linux/pci.h>
  24. #include <linux/delay.h>
  25. #include <asm/io.h>
  26. /*
  27. * The I/O port the PMTMR resides at.
  28. * The location is detected during setup_arch(),
  29. * in arch/i386/kernel/acpi/boot.c
  30. */
  31. u32 pmtmr_ioport __read_mostly;
  32. static inline u32 read_pmtmr(void)
  33. {
  34. /* mask the output to 24 bits */
  35. return inl(pmtmr_ioport) & ACPI_PM_MASK;
  36. }
  37. u32 acpi_pm_read_verified(void)
  38. {
  39. u32 v1 = 0, v2 = 0, v3 = 0;
  40. /*
  41. * It has been reported that because of various broken
  42. * chipsets (ICH4, PIIX4 and PIIX4E) where the ACPI PM clock
  43. * source is not latched, you must read it multiple
  44. * times to ensure a safe value is read:
  45. */
  46. do {
  47. v1 = read_pmtmr();
  48. v2 = read_pmtmr();
  49. v3 = read_pmtmr();
  50. } while (unlikely((v1 > v2 && v1 < v3) || (v2 > v3 && v2 < v1)
  51. || (v3 > v1 && v3 < v2)));
  52. return v2;
  53. }
  54. static u64 acpi_pm_read(struct clocksource *cs)
  55. {
  56. return (u64)read_pmtmr();
  57. }
  58. static struct clocksource clocksource_acpi_pm = {
  59. .name = "acpi_pm",
  60. .rating = 200,
  61. .read = acpi_pm_read,
  62. .mask = (u64)ACPI_PM_MASK,
  63. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  64. };
  65. #ifdef CONFIG_PCI
  66. static int acpi_pm_good;
  67. static int __init acpi_pm_good_setup(char *__str)
  68. {
  69. acpi_pm_good = 1;
  70. return 1;
  71. }
  72. __setup("acpi_pm_good", acpi_pm_good_setup);
  73. static u64 acpi_pm_read_slow(struct clocksource *cs)
  74. {
  75. return (u64)acpi_pm_read_verified();
  76. }
  77. static inline void acpi_pm_need_workaround(void)
  78. {
  79. clocksource_acpi_pm.read = acpi_pm_read_slow;
  80. clocksource_acpi_pm.rating = 120;
  81. }
  82. /*
  83. * PIIX4 Errata:
  84. *
  85. * The power management timer may return improper results when read.
  86. * Although the timer value settles properly after incrementing,
  87. * while incrementing there is a 3 ns window every 69.8 ns where the
  88. * timer value is indeterminate (a 4.2% chance that the data will be
  89. * incorrect when read). As a result, the ACPI free running count up
  90. * timer specification is violated due to erroneous reads.
  91. */
  92. static void acpi_pm_check_blacklist(struct pci_dev *dev)
  93. {
  94. if (acpi_pm_good)
  95. return;
  96. /* the bug has been fixed in PIIX4M */
  97. if (dev->revision < 3) {
  98. pr_warn("* Found PM-Timer Bug on the chipset. Due to workarounds for a bug,\n"
  99. "* this clock source is slow. Consider trying other clock sources\n");
  100. acpi_pm_need_workaround();
  101. }
  102. }
  103. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3,
  104. acpi_pm_check_blacklist);
  105. static void acpi_pm_check_graylist(struct pci_dev *dev)
  106. {
  107. if (acpi_pm_good)
  108. return;
  109. pr_warn("* The chipset may have PM-Timer Bug. Due to workarounds for a bug,\n"
  110. "* this clock source is slow. If you are sure your timer does not have\n"
  111. "* this bug, please use \"acpi_pm_good\" to disable the workaround\n");
  112. acpi_pm_need_workaround();
  113. }
  114. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_0,
  115. acpi_pm_check_graylist);
  116. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_SERVERWORKS, PCI_DEVICE_ID_SERVERWORKS_LE,
  117. acpi_pm_check_graylist);
  118. #endif
  119. #ifndef CONFIG_X86_64
  120. #include <asm/mach_timer.h>
  121. #define PMTMR_EXPECTED_RATE \
  122. ((CALIBRATE_LATCH * (PMTMR_TICKS_PER_SEC >> 10)) / (PIT_TICK_RATE>>10))
  123. /*
  124. * Some boards have the PMTMR running way too fast. We check
  125. * the PMTMR rate against PIT channel 2 to catch these cases.
  126. */
  127. static int verify_pmtmr_rate(void)
  128. {
  129. u64 value1, value2;
  130. unsigned long count, delta;
  131. mach_prepare_counter();
  132. value1 = clocksource_acpi_pm.read(&clocksource_acpi_pm);
  133. mach_countup(&count);
  134. value2 = clocksource_acpi_pm.read(&clocksource_acpi_pm);
  135. delta = (value2 - value1) & ACPI_PM_MASK;
  136. /* Check that the PMTMR delta is within 5% of what we expect */
  137. if (delta < (PMTMR_EXPECTED_RATE * 19) / 20 ||
  138. delta > (PMTMR_EXPECTED_RATE * 21) / 20) {
  139. pr_info("PM-Timer running at invalid rate: %lu%% of normal - aborting.\n",
  140. 100UL * delta / PMTMR_EXPECTED_RATE);
  141. return -1;
  142. }
  143. return 0;
  144. }
  145. #else
  146. #define verify_pmtmr_rate() (0)
  147. #endif
  148. /* Number of monotonicity checks to perform during initialization */
  149. #define ACPI_PM_MONOTONICITY_CHECKS 10
  150. /* Number of reads we try to get two different values */
  151. #define ACPI_PM_READ_CHECKS 10000
  152. static int __init init_acpi_pm_clocksource(void)
  153. {
  154. u64 value1, value2;
  155. unsigned int i, j = 0;
  156. if (!pmtmr_ioport)
  157. return -ENODEV;
  158. /* "verify" this timing source: */
  159. for (j = 0; j < ACPI_PM_MONOTONICITY_CHECKS; j++) {
  160. udelay(100 * j);
  161. value1 = clocksource_acpi_pm.read(&clocksource_acpi_pm);
  162. for (i = 0; i < ACPI_PM_READ_CHECKS; i++) {
  163. value2 = clocksource_acpi_pm.read(&clocksource_acpi_pm);
  164. if (value2 == value1)
  165. continue;
  166. if (value2 > value1)
  167. break;
  168. if ((value2 < value1) && ((value2) < 0xFFF))
  169. break;
  170. pr_info("PM-Timer had inconsistent results: %#llx, %#llx - aborting.\n",
  171. value1, value2);
  172. pmtmr_ioport = 0;
  173. return -EINVAL;
  174. }
  175. if (i == ACPI_PM_READ_CHECKS) {
  176. pr_info("PM-Timer failed consistency check (%#llx) - aborting.\n",
  177. value1);
  178. pmtmr_ioport = 0;
  179. return -ENODEV;
  180. }
  181. }
  182. if (verify_pmtmr_rate() != 0){
  183. pmtmr_ioport = 0;
  184. return -ENODEV;
  185. }
  186. return clocksource_register_hz(&clocksource_acpi_pm,
  187. PMTMR_TICKS_PER_SEC);
  188. }
  189. /* We use fs_initcall because we want the PCI fixups to have run
  190. * but we still need to load before device_initcall
  191. */
  192. fs_initcall(init_acpi_pm_clocksource);
  193. /*
  194. * Allow an override of the IOPort. Stupid BIOSes do not tell us about
  195. * the PMTimer, but we might know where it is.
  196. */
  197. static int __init parse_pmtmr(char *arg)
  198. {
  199. unsigned int base;
  200. int ret;
  201. ret = kstrtouint(arg, 16, &base);
  202. if (ret)
  203. return ret;
  204. pr_info("PMTMR IOPort override: 0x%04x -> 0x%04x\n", pmtmr_ioport,
  205. base);
  206. pmtmr_ioport = base;
  207. return 1;
  208. }
  209. __setup("pmtmr=", parse_pmtmr);