18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * linux/drivers/clocksource/acpi_pm.c 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * This file contains the ACPI PM based clocksource. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * This code was largely moved from the i386 timer_pm.c file 88c2ecf20Sopenharmony_ci * which was (C) Dominik Brodowski <linux@brodo.de> 2003 98c2ecf20Sopenharmony_ci * and contained the following comments: 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * Driver to use the Power Management Timer (PMTMR) available in some 128c2ecf20Sopenharmony_ci * southbridges as primary timing source for the Linux kernel. 138c2ecf20Sopenharmony_ci * 148c2ecf20Sopenharmony_ci * Based on parts of linux/drivers/acpi/hardware/hwtimer.c, timer_pit.c, 158c2ecf20Sopenharmony_ci * timer_hpet.c, and on Arjan van de Ven's implementation for 2.4. 168c2ecf20Sopenharmony_ci */ 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci#include <linux/acpi_pmtmr.h> 198c2ecf20Sopenharmony_ci#include <linux/clocksource.h> 208c2ecf20Sopenharmony_ci#include <linux/timex.h> 218c2ecf20Sopenharmony_ci#include <linux/errno.h> 228c2ecf20Sopenharmony_ci#include <linux/init.h> 238c2ecf20Sopenharmony_ci#include <linux/pci.h> 248c2ecf20Sopenharmony_ci#include <linux/delay.h> 258c2ecf20Sopenharmony_ci#include <asm/io.h> 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci/* 288c2ecf20Sopenharmony_ci * The I/O port the PMTMR resides at. 298c2ecf20Sopenharmony_ci * The location is detected during setup_arch(), 308c2ecf20Sopenharmony_ci * in arch/i386/kernel/acpi/boot.c 318c2ecf20Sopenharmony_ci */ 328c2ecf20Sopenharmony_ciu32 pmtmr_ioport __read_mostly; 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_cistatic inline u32 read_pmtmr(void) 358c2ecf20Sopenharmony_ci{ 368c2ecf20Sopenharmony_ci /* mask the output to 24 bits */ 378c2ecf20Sopenharmony_ci return inl(pmtmr_ioport) & ACPI_PM_MASK; 388c2ecf20Sopenharmony_ci} 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ciu32 acpi_pm_read_verified(void) 418c2ecf20Sopenharmony_ci{ 428c2ecf20Sopenharmony_ci u32 v1 = 0, v2 = 0, v3 = 0; 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci /* 458c2ecf20Sopenharmony_ci * It has been reported that because of various broken 468c2ecf20Sopenharmony_ci * chipsets (ICH4, PIIX4 and PIIX4E) where the ACPI PM clock 478c2ecf20Sopenharmony_ci * source is not latched, you must read it multiple 488c2ecf20Sopenharmony_ci * times to ensure a safe value is read: 498c2ecf20Sopenharmony_ci */ 508c2ecf20Sopenharmony_ci do { 518c2ecf20Sopenharmony_ci v1 = read_pmtmr(); 528c2ecf20Sopenharmony_ci v2 = read_pmtmr(); 538c2ecf20Sopenharmony_ci v3 = read_pmtmr(); 548c2ecf20Sopenharmony_ci } while (unlikely((v1 > v2 && v1 < v3) || (v2 > v3 && v2 < v1) 558c2ecf20Sopenharmony_ci || (v3 > v1 && v3 < v2))); 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci return v2; 588c2ecf20Sopenharmony_ci} 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_cistatic u64 acpi_pm_read(struct clocksource *cs) 618c2ecf20Sopenharmony_ci{ 628c2ecf20Sopenharmony_ci return (u64)read_pmtmr(); 638c2ecf20Sopenharmony_ci} 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_cistatic struct clocksource clocksource_acpi_pm = { 668c2ecf20Sopenharmony_ci .name = "acpi_pm", 678c2ecf20Sopenharmony_ci .rating = 200, 688c2ecf20Sopenharmony_ci .read = acpi_pm_read, 698c2ecf20Sopenharmony_ci .mask = (u64)ACPI_PM_MASK, 708c2ecf20Sopenharmony_ci .flags = CLOCK_SOURCE_IS_CONTINUOUS, 718c2ecf20Sopenharmony_ci}; 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci#ifdef CONFIG_PCI 758c2ecf20Sopenharmony_cistatic int acpi_pm_good; 768c2ecf20Sopenharmony_cistatic int __init acpi_pm_good_setup(char *__str) 778c2ecf20Sopenharmony_ci{ 788c2ecf20Sopenharmony_ci acpi_pm_good = 1; 798c2ecf20Sopenharmony_ci return 1; 808c2ecf20Sopenharmony_ci} 818c2ecf20Sopenharmony_ci__setup("acpi_pm_good", acpi_pm_good_setup); 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_cistatic u64 acpi_pm_read_slow(struct clocksource *cs) 848c2ecf20Sopenharmony_ci{ 858c2ecf20Sopenharmony_ci return (u64)acpi_pm_read_verified(); 868c2ecf20Sopenharmony_ci} 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_cistatic inline void acpi_pm_need_workaround(void) 898c2ecf20Sopenharmony_ci{ 908c2ecf20Sopenharmony_ci clocksource_acpi_pm.read = acpi_pm_read_slow; 918c2ecf20Sopenharmony_ci clocksource_acpi_pm.rating = 120; 928c2ecf20Sopenharmony_ci} 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci/* 958c2ecf20Sopenharmony_ci * PIIX4 Errata: 968c2ecf20Sopenharmony_ci * 978c2ecf20Sopenharmony_ci * The power management timer may return improper results when read. 988c2ecf20Sopenharmony_ci * Although the timer value settles properly after incrementing, 998c2ecf20Sopenharmony_ci * while incrementing there is a 3 ns window every 69.8 ns where the 1008c2ecf20Sopenharmony_ci * timer value is indeterminate (a 4.2% chance that the data will be 1018c2ecf20Sopenharmony_ci * incorrect when read). As a result, the ACPI free running count up 1028c2ecf20Sopenharmony_ci * timer specification is violated due to erroneous reads. 1038c2ecf20Sopenharmony_ci */ 1048c2ecf20Sopenharmony_cistatic void acpi_pm_check_blacklist(struct pci_dev *dev) 1058c2ecf20Sopenharmony_ci{ 1068c2ecf20Sopenharmony_ci if (acpi_pm_good) 1078c2ecf20Sopenharmony_ci return; 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci /* the bug has been fixed in PIIX4M */ 1108c2ecf20Sopenharmony_ci if (dev->revision < 3) { 1118c2ecf20Sopenharmony_ci pr_warn("* Found PM-Timer Bug on the chipset. Due to workarounds for a bug,\n" 1128c2ecf20Sopenharmony_ci "* this clock source is slow. Consider trying other clock sources\n"); 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci acpi_pm_need_workaround(); 1158c2ecf20Sopenharmony_ci } 1168c2ecf20Sopenharmony_ci} 1178c2ecf20Sopenharmony_ciDECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3, 1188c2ecf20Sopenharmony_ci acpi_pm_check_blacklist); 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_cistatic void acpi_pm_check_graylist(struct pci_dev *dev) 1218c2ecf20Sopenharmony_ci{ 1228c2ecf20Sopenharmony_ci if (acpi_pm_good) 1238c2ecf20Sopenharmony_ci return; 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci pr_warn("* The chipset may have PM-Timer Bug. Due to workarounds for a bug,\n" 1268c2ecf20Sopenharmony_ci "* this clock source is slow. If you are sure your timer does not have\n" 1278c2ecf20Sopenharmony_ci "* this bug, please use \"acpi_pm_good\" to disable the workaround\n"); 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci acpi_pm_need_workaround(); 1308c2ecf20Sopenharmony_ci} 1318c2ecf20Sopenharmony_ciDECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_0, 1328c2ecf20Sopenharmony_ci acpi_pm_check_graylist); 1338c2ecf20Sopenharmony_ciDECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_SERVERWORKS, PCI_DEVICE_ID_SERVERWORKS_LE, 1348c2ecf20Sopenharmony_ci acpi_pm_check_graylist); 1358c2ecf20Sopenharmony_ci#endif 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci#ifndef CONFIG_X86_64 1388c2ecf20Sopenharmony_ci#include <asm/mach_timer.h> 1398c2ecf20Sopenharmony_ci#define PMTMR_EXPECTED_RATE \ 1408c2ecf20Sopenharmony_ci ((CALIBRATE_LATCH * (PMTMR_TICKS_PER_SEC >> 10)) / (PIT_TICK_RATE>>10)) 1418c2ecf20Sopenharmony_ci/* 1428c2ecf20Sopenharmony_ci * Some boards have the PMTMR running way too fast. We check 1438c2ecf20Sopenharmony_ci * the PMTMR rate against PIT channel 2 to catch these cases. 1448c2ecf20Sopenharmony_ci */ 1458c2ecf20Sopenharmony_cistatic int verify_pmtmr_rate(void) 1468c2ecf20Sopenharmony_ci{ 1478c2ecf20Sopenharmony_ci u64 value1, value2; 1488c2ecf20Sopenharmony_ci unsigned long count, delta; 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci mach_prepare_counter(); 1518c2ecf20Sopenharmony_ci value1 = clocksource_acpi_pm.read(&clocksource_acpi_pm); 1528c2ecf20Sopenharmony_ci mach_countup(&count); 1538c2ecf20Sopenharmony_ci value2 = clocksource_acpi_pm.read(&clocksource_acpi_pm); 1548c2ecf20Sopenharmony_ci delta = (value2 - value1) & ACPI_PM_MASK; 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci /* Check that the PMTMR delta is within 5% of what we expect */ 1578c2ecf20Sopenharmony_ci if (delta < (PMTMR_EXPECTED_RATE * 19) / 20 || 1588c2ecf20Sopenharmony_ci delta > (PMTMR_EXPECTED_RATE * 21) / 20) { 1598c2ecf20Sopenharmony_ci pr_info("PM-Timer running at invalid rate: %lu%% of normal - aborting.\n", 1608c2ecf20Sopenharmony_ci 100UL * delta / PMTMR_EXPECTED_RATE); 1618c2ecf20Sopenharmony_ci return -1; 1628c2ecf20Sopenharmony_ci } 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci return 0; 1658c2ecf20Sopenharmony_ci} 1668c2ecf20Sopenharmony_ci#else 1678c2ecf20Sopenharmony_ci#define verify_pmtmr_rate() (0) 1688c2ecf20Sopenharmony_ci#endif 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci/* Number of monotonicity checks to perform during initialization */ 1718c2ecf20Sopenharmony_ci#define ACPI_PM_MONOTONICITY_CHECKS 10 1728c2ecf20Sopenharmony_ci/* Number of reads we try to get two different values */ 1738c2ecf20Sopenharmony_ci#define ACPI_PM_READ_CHECKS 10000 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_cistatic int __init init_acpi_pm_clocksource(void) 1768c2ecf20Sopenharmony_ci{ 1778c2ecf20Sopenharmony_ci u64 value1, value2; 1788c2ecf20Sopenharmony_ci unsigned int i, j = 0; 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci if (!pmtmr_ioport) 1818c2ecf20Sopenharmony_ci return -ENODEV; 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci /* "verify" this timing source: */ 1848c2ecf20Sopenharmony_ci for (j = 0; j < ACPI_PM_MONOTONICITY_CHECKS; j++) { 1858c2ecf20Sopenharmony_ci udelay(100 * j); 1868c2ecf20Sopenharmony_ci value1 = clocksource_acpi_pm.read(&clocksource_acpi_pm); 1878c2ecf20Sopenharmony_ci for (i = 0; i < ACPI_PM_READ_CHECKS; i++) { 1888c2ecf20Sopenharmony_ci value2 = clocksource_acpi_pm.read(&clocksource_acpi_pm); 1898c2ecf20Sopenharmony_ci if (value2 == value1) 1908c2ecf20Sopenharmony_ci continue; 1918c2ecf20Sopenharmony_ci if (value2 > value1) 1928c2ecf20Sopenharmony_ci break; 1938c2ecf20Sopenharmony_ci if ((value2 < value1) && ((value2) < 0xFFF)) 1948c2ecf20Sopenharmony_ci break; 1958c2ecf20Sopenharmony_ci pr_info("PM-Timer had inconsistent results: %#llx, %#llx - aborting.\n", 1968c2ecf20Sopenharmony_ci value1, value2); 1978c2ecf20Sopenharmony_ci pmtmr_ioport = 0; 1988c2ecf20Sopenharmony_ci return -EINVAL; 1998c2ecf20Sopenharmony_ci } 2008c2ecf20Sopenharmony_ci if (i == ACPI_PM_READ_CHECKS) { 2018c2ecf20Sopenharmony_ci pr_info("PM-Timer failed consistency check (%#llx) - aborting.\n", 2028c2ecf20Sopenharmony_ci value1); 2038c2ecf20Sopenharmony_ci pmtmr_ioport = 0; 2048c2ecf20Sopenharmony_ci return -ENODEV; 2058c2ecf20Sopenharmony_ci } 2068c2ecf20Sopenharmony_ci } 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci if (verify_pmtmr_rate() != 0){ 2098c2ecf20Sopenharmony_ci pmtmr_ioport = 0; 2108c2ecf20Sopenharmony_ci return -ENODEV; 2118c2ecf20Sopenharmony_ci } 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci return clocksource_register_hz(&clocksource_acpi_pm, 2148c2ecf20Sopenharmony_ci PMTMR_TICKS_PER_SEC); 2158c2ecf20Sopenharmony_ci} 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci/* We use fs_initcall because we want the PCI fixups to have run 2188c2ecf20Sopenharmony_ci * but we still need to load before device_initcall 2198c2ecf20Sopenharmony_ci */ 2208c2ecf20Sopenharmony_cifs_initcall(init_acpi_pm_clocksource); 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci/* 2238c2ecf20Sopenharmony_ci * Allow an override of the IOPort. Stupid BIOSes do not tell us about 2248c2ecf20Sopenharmony_ci * the PMTimer, but we might know where it is. 2258c2ecf20Sopenharmony_ci */ 2268c2ecf20Sopenharmony_cistatic int __init parse_pmtmr(char *arg) 2278c2ecf20Sopenharmony_ci{ 2288c2ecf20Sopenharmony_ci unsigned int base; 2298c2ecf20Sopenharmony_ci int ret; 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci ret = kstrtouint(arg, 16, &base); 2328c2ecf20Sopenharmony_ci if (ret) { 2338c2ecf20Sopenharmony_ci pr_warn("PMTMR: invalid 'pmtmr=' value: '%s'\n", arg); 2348c2ecf20Sopenharmony_ci return 1; 2358c2ecf20Sopenharmony_ci } 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci pr_info("PMTMR IOPort override: 0x%04x -> 0x%04x\n", pmtmr_ioport, 2388c2ecf20Sopenharmony_ci base); 2398c2ecf20Sopenharmony_ci pmtmr_ioport = base; 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_ci return 1; 2428c2ecf20Sopenharmony_ci} 2438c2ecf20Sopenharmony_ci__setup("pmtmr=", parse_pmtmr); 244