acpi_pm.c 6.3 KB

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