^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * acpi_lpit.c - LPIT table processing functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2017 Intel Corporation. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <asm/msr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <asm/tsc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) struct lpit_residency_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) struct acpi_generic_address gaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) u64 frequency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) void __iomem *iomem_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /* Storage for an memory mapped and FFH based entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static struct lpit_residency_info residency_info_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static struct lpit_residency_info residency_info_ffh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static int lpit_read_residency_counter_us(u64 *counter, bool io_mem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if (io_mem) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) u64 count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) error = acpi_os_read_iomem(residency_info_mem.iomem_addr, &count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) residency_info_mem.gaddr.bit_width);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) *counter = div64_u64(count * 1000000ULL, residency_info_mem.frequency);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) err = rdmsrl_safe(residency_info_ffh.gaddr.address, counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) u64 mask = GENMASK_ULL(residency_info_ffh.gaddr.bit_offset +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) residency_info_ffh.gaddr. bit_width - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) residency_info_ffh.gaddr.bit_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) *counter &= mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) *counter >>= residency_info_ffh.gaddr.bit_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) *counter = div64_u64(*counter * 1000000ULL, residency_info_ffh.frequency);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static ssize_t low_power_idle_system_residency_us_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) u64 counter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) ret = lpit_read_residency_counter_us(&counter, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return sprintf(buf, "%llu\n", counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static DEVICE_ATTR_RO(low_power_idle_system_residency_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static ssize_t low_power_idle_cpu_residency_us_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) u64 counter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) ret = lpit_read_residency_counter_us(&counter, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return sprintf(buf, "%llu\n", counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static DEVICE_ATTR_RO(low_power_idle_cpu_residency_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) int lpit_read_residency_count_address(u64 *address)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (!residency_info_mem.gaddr.address)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) *address = residency_info_mem.gaddr.address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) EXPORT_SYMBOL_GPL(lpit_read_residency_count_address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static void lpit_update_residency(struct lpit_residency_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct acpi_lpit_native *lpit_native)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) info->frequency = lpit_native->counter_frequency ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) lpit_native->counter_frequency : tsc_khz * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (!info->frequency)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) info->frequency = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) info->gaddr = lpit_native->residency_counter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (info->gaddr.space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) info->iomem_addr = ioremap(info->gaddr.address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) info->gaddr.bit_width / 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (!info->iomem_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (!(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /* Silently fail, if cpuidle attribute group is not present */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) sysfs_add_file_to_group(&cpu_subsys.dev_root->kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) &dev_attr_low_power_idle_system_residency_us.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) "cpuidle");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) } else if (info->gaddr.space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (!(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /* Silently fail, if cpuidle attribute group is not present */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) sysfs_add_file_to_group(&cpu_subsys.dev_root->kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) &dev_attr_low_power_idle_cpu_residency_us.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) "cpuidle");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static void lpit_process(u64 begin, u64 end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) while (begin + sizeof(struct acpi_lpit_native) <= end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct acpi_lpit_native *lpit_native = (struct acpi_lpit_native *)begin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (!lpit_native->header.type && !lpit_native->header.flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (lpit_native->residency_counter.space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) !residency_info_mem.gaddr.address) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) lpit_update_residency(&residency_info_mem, lpit_native);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) } else if (lpit_native->residency_counter.space_id == ACPI_ADR_SPACE_FIXED_HARDWARE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) !residency_info_ffh.gaddr.address) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) lpit_update_residency(&residency_info_ffh, lpit_native);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) begin += lpit_native->header.length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) void acpi_init_lpit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) acpi_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct acpi_table_lpit *lpit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) status = acpi_get_table(ACPI_SIG_LPIT, 0, (struct acpi_table_header **)&lpit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (ACPI_FAILURE(status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) lpit_process((u64)lpit + sizeof(*lpit),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) (u64)lpit + lpit->header.length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) acpi_put_table((struct acpi_table_header *)lpit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }