acpi_cppc.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <stdio.h>
  3. #include <errno.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10. #include "cpupower_intern.h"
  11. #include "acpi_cppc.h"
  12. /* ACPI CPPC sysfs access ***********************************************/
  13. static int acpi_cppc_read_file(unsigned int cpu, const char *fname,
  14. char *buf, size_t buflen)
  15. {
  16. char path[SYSFS_PATH_MAX];
  17. snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/acpi_cppc/%s",
  18. cpu, fname);
  19. return cpupower_read_sysfs(path, buf, buflen);
  20. }
  21. static const char * const acpi_cppc_value_files[] = {
  22. [HIGHEST_PERF] = "highest_perf",
  23. [LOWEST_PERF] = "lowest_perf",
  24. [NOMINAL_PERF] = "nominal_perf",
  25. [LOWEST_NONLINEAR_PERF] = "lowest_nonlinear_perf",
  26. [LOWEST_FREQ] = "lowest_freq",
  27. [NOMINAL_FREQ] = "nominal_freq",
  28. [REFERENCE_PERF] = "reference_perf",
  29. [WRAPAROUND_TIME] = "wraparound_time"
  30. };
  31. unsigned long acpi_cppc_get_data(unsigned int cpu, enum acpi_cppc_value which)
  32. {
  33. unsigned long long value;
  34. unsigned int len;
  35. char linebuf[MAX_LINE_LEN];
  36. char *endp;
  37. if (which >= MAX_CPPC_VALUE_FILES)
  38. return 0;
  39. len = acpi_cppc_read_file(cpu, acpi_cppc_value_files[which],
  40. linebuf, sizeof(linebuf));
  41. if (len == 0)
  42. return 0;
  43. value = strtoull(linebuf, &endp, 0);
  44. if (endp == linebuf || errno == ERANGE)
  45. return 0;
  46. return value;
  47. }