acpi_lpit.c 4.2 KB

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