acpi_pm.c 7.2 KB

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