0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/cpu.h>
0010 #include <linux/acpi.h>
0011 #include <asm/msr.h>
0012 #include <asm/tsc.h>
0013
0014 struct lpit_residency_info {
0015 struct acpi_generic_address gaddr;
0016 u64 frequency;
0017 void __iomem *iomem_addr;
0018 };
0019
0020
0021 static struct lpit_residency_info residency_info_mem;
0022 static struct lpit_residency_info residency_info_ffh;
0023
0024 static int lpit_read_residency_counter_us(u64 *counter, bool io_mem)
0025 {
0026 int err;
0027
0028 if (io_mem) {
0029 u64 count = 0;
0030 int error;
0031
0032 error = acpi_os_read_iomem(residency_info_mem.iomem_addr, &count,
0033 residency_info_mem.gaddr.bit_width);
0034 if (error)
0035 return error;
0036
0037 *counter = div64_u64(count * 1000000ULL, residency_info_mem.frequency);
0038 return 0;
0039 }
0040
0041 err = rdmsrl_safe(residency_info_ffh.gaddr.address, counter);
0042 if (!err) {
0043 u64 mask = GENMASK_ULL(residency_info_ffh.gaddr.bit_offset +
0044 residency_info_ffh.gaddr. bit_width - 1,
0045 residency_info_ffh.gaddr.bit_offset);
0046
0047 *counter &= mask;
0048 *counter >>= residency_info_ffh.gaddr.bit_offset;
0049 *counter = div64_u64(*counter * 1000000ULL, residency_info_ffh.frequency);
0050 return 0;
0051 }
0052
0053 return -ENODATA;
0054 }
0055
0056 static ssize_t low_power_idle_system_residency_us_show(struct device *dev,
0057 struct device_attribute *attr,
0058 char *buf)
0059 {
0060 u64 counter;
0061 int ret;
0062
0063 ret = lpit_read_residency_counter_us(&counter, true);
0064 if (ret)
0065 return ret;
0066
0067 return sprintf(buf, "%llu\n", counter);
0068 }
0069 static DEVICE_ATTR_RO(low_power_idle_system_residency_us);
0070
0071 static ssize_t low_power_idle_cpu_residency_us_show(struct device *dev,
0072 struct device_attribute *attr,
0073 char *buf)
0074 {
0075 u64 counter;
0076 int ret;
0077
0078 ret = lpit_read_residency_counter_us(&counter, false);
0079 if (ret)
0080 return ret;
0081
0082 return sprintf(buf, "%llu\n", counter);
0083 }
0084 static DEVICE_ATTR_RO(low_power_idle_cpu_residency_us);
0085
0086 int lpit_read_residency_count_address(u64 *address)
0087 {
0088 if (!residency_info_mem.gaddr.address)
0089 return -EINVAL;
0090
0091 *address = residency_info_mem.gaddr.address;
0092
0093 return 0;
0094 }
0095 EXPORT_SYMBOL_GPL(lpit_read_residency_count_address);
0096
0097 static void lpit_update_residency(struct lpit_residency_info *info,
0098 struct acpi_lpit_native *lpit_native)
0099 {
0100 info->frequency = lpit_native->counter_frequency ?
0101 lpit_native->counter_frequency : tsc_khz * 1000;
0102 if (!info->frequency)
0103 info->frequency = 1;
0104
0105 info->gaddr = lpit_native->residency_counter;
0106 if (info->gaddr.space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
0107 info->iomem_addr = ioremap(info->gaddr.address,
0108 info->gaddr.bit_width / 8);
0109 if (!info->iomem_addr)
0110 return;
0111
0112
0113 sysfs_add_file_to_group(&cpu_subsys.dev_root->kobj,
0114 &dev_attr_low_power_idle_system_residency_us.attr,
0115 "cpuidle");
0116 } else if (info->gaddr.space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) {
0117
0118 sysfs_add_file_to_group(&cpu_subsys.dev_root->kobj,
0119 &dev_attr_low_power_idle_cpu_residency_us.attr,
0120 "cpuidle");
0121 }
0122 }
0123
0124 static void lpit_process(u64 begin, u64 end)
0125 {
0126 while (begin + sizeof(struct acpi_lpit_native) <= end) {
0127 struct acpi_lpit_native *lpit_native = (struct acpi_lpit_native *)begin;
0128
0129 if (!lpit_native->header.type && !lpit_native->header.flags) {
0130 if (lpit_native->residency_counter.space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY &&
0131 !residency_info_mem.gaddr.address) {
0132 lpit_update_residency(&residency_info_mem, lpit_native);
0133 } else if (lpit_native->residency_counter.space_id == ACPI_ADR_SPACE_FIXED_HARDWARE &&
0134 !residency_info_ffh.gaddr.address) {
0135 lpit_update_residency(&residency_info_ffh, lpit_native);
0136 }
0137 }
0138 begin += lpit_native->header.length;
0139 }
0140 }
0141
0142 void acpi_init_lpit(void)
0143 {
0144 acpi_status status;
0145 struct acpi_table_lpit *lpit;
0146
0147 status = acpi_get_table(ACPI_SIG_LPIT, 0, (struct acpi_table_header **)&lpit);
0148 if (ACPI_FAILURE(status))
0149 return;
0150
0151 lpit_process((u64)lpit + sizeof(*lpit),
0152 (u64)lpit + lpit->header.length);
0153
0154 acpi_put_table((struct acpi_table_header *)lpit);
0155 }