Skip to content

Commit 8113ab2

Browse files
committed
tools/power/cpupower: Read energy_perf_bias from sysfs
... instead of poking at the MSR. For that, move the accessor functions to misc.c and add a sysfs-writing function too. There should be no functional changes resulting from this. Signed-off-by: Borislav Petkov <[email protected]> Reviewed-by: Shuah Khan <[email protected]> Cc: Thomas Renninger <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 632211c commit 8113ab2

File tree

7 files changed

+81
-35
lines changed

7 files changed

+81
-35
lines changed

tools/power/cpupower/lib/cpupower.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
unsigned int cpupower_read_sysfs(const char *path, char *buf, size_t buflen)
1818
{
19-
int fd;
2019
ssize_t numread;
20+
int fd;
2121

2222
fd = open(path, O_RDONLY);
2323
if (fd == -1)
@@ -35,6 +35,27 @@ unsigned int cpupower_read_sysfs(const char *path, char *buf, size_t buflen)
3535
return (unsigned int) numread;
3636
}
3737

38+
unsigned int cpupower_write_sysfs(const char *path, char *buf, size_t buflen)
39+
{
40+
ssize_t numwritten;
41+
int fd;
42+
43+
fd = open(path, O_WRONLY);
44+
if (fd == -1)
45+
return 0;
46+
47+
numwritten = write(fd, buf, buflen - 1);
48+
if (numwritten < 1) {
49+
perror(path);
50+
close(fd);
51+
return -1;
52+
}
53+
54+
close(fd);
55+
56+
return (unsigned int) numwritten;
57+
}
58+
3859
/*
3960
* Detect whether a CPU is online
4061
*
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
/* SPDX-License-Identifier: GPL-2.0 */
22
#define PATH_TO_CPU "/sys/devices/system/cpu/"
3+
4+
#ifndef MAX_LINE_LEN
35
#define MAX_LINE_LEN 4096
6+
#endif
7+
48
#define SYSFS_PATH_MAX 255
59

610
unsigned int cpupower_read_sysfs(const char *path, char *buf, size_t buflen);
11+
unsigned int cpupower_write_sysfs(const char *path, char *buf, size_t buflen);

tools/power/cpupower/utils/cpupower-info.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ int cmd_info(int argc, char **argv)
101101
}
102102

103103
if (params.perf_bias) {
104-
ret = msr_intel_get_perf_bias(cpu);
104+
ret = cpupower_intel_get_perf_bias(cpu);
105105
if (ret < 0) {
106106
fprintf(stderr,
107107
_("Could not read perf-bias value[%d]\n"), ret);

tools/power/cpupower/utils/cpupower-set.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ int cmd_set(int argc, char **argv)
9595
}
9696

9797
if (params.perf_bias) {
98-
ret = msr_intel_set_perf_bias(cpu, perf_bias);
98+
ret = cpupower_intel_set_perf_bias(cpu, perf_bias);
9999
if (ret) {
100100
fprintf(stderr, _("Error setting perf-bias "
101101
"value on CPU %d\n"), cpu);

tools/power/cpupower/utils/helpers/helpers.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ extern struct cpupower_cpu_info cpupower_cpu_info;
105105
extern int read_msr(int cpu, unsigned int idx, unsigned long long *val);
106106
extern int write_msr(int cpu, unsigned int idx, unsigned long long val);
107107

108-
extern int msr_intel_set_perf_bias(unsigned int cpu, unsigned int val);
109-
extern int msr_intel_get_perf_bias(unsigned int cpu);
108+
extern int cpupower_intel_set_perf_bias(unsigned int cpu, unsigned int val);
109+
extern int cpupower_intel_get_perf_bias(unsigned int cpu);
110110
extern unsigned long long msr_intel_get_turbo_ratio(unsigned int cpu);
111111

112112
/* Read/Write msr ****************************/
@@ -150,9 +150,9 @@ static inline int read_msr(int cpu, unsigned int idx, unsigned long long *val)
150150
{ return -1; };
151151
static inline int write_msr(int cpu, unsigned int idx, unsigned long long val)
152152
{ return -1; };
153-
static inline int msr_intel_set_perf_bias(unsigned int cpu, unsigned int val)
153+
static inline int cpupower_intel_set_perf_bias(unsigned int cpu, unsigned int val)
154154
{ return -1; };
155-
static inline int msr_intel_get_perf_bias(unsigned int cpu)
155+
static inline int cpupower_intel_get_perf_bias(unsigned int cpu)
156156
{ return -1; };
157157
static inline unsigned long long msr_intel_get_turbo_ratio(unsigned int cpu)
158158
{ return 0; };

tools/power/cpupower/utils/helpers/misc.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <stdio.h>
4+
#include <errno.h>
5+
#include <stdlib.h>
6+
27
#if defined(__i386__) || defined(__x86_64__)
38

49
#include "helpers/helpers.h"
10+
#include "helpers/sysfs.h"
11+
12+
#include "cpupower_intern.h"
513

614
#define MSR_AMD_HWCR 0xc0010015
715

@@ -40,4 +48,44 @@ int cpufreq_has_boost_support(unsigned int cpu, int *support, int *active,
4048
*support = *active = 1;
4149
return 0;
4250
}
51+
52+
int cpupower_intel_get_perf_bias(unsigned int cpu)
53+
{
54+
char linebuf[MAX_LINE_LEN];
55+
char path[SYSFS_PATH_MAX];
56+
unsigned long val;
57+
char *endp;
58+
59+
if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_PERF_BIAS))
60+
return -1;
61+
62+
snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/power/energy_perf_bias", cpu);
63+
64+
if (cpupower_read_sysfs(path, linebuf, MAX_LINE_LEN) == 0)
65+
return -1;
66+
67+
val = strtol(linebuf, &endp, 0);
68+
if (endp == linebuf || errno == ERANGE)
69+
return -1;
70+
71+
return val;
72+
}
73+
74+
int cpupower_intel_set_perf_bias(unsigned int cpu, unsigned int val)
75+
{
76+
char path[SYSFS_PATH_MAX];
77+
char linebuf[3] = {};
78+
79+
if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_PERF_BIAS))
80+
return -1;
81+
82+
snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/power/energy_perf_bias", cpu);
83+
snprintf(linebuf, sizeof(linebuf), "%d", val);
84+
85+
if (cpupower_write_sysfs(path, linebuf, 3) <= 0)
86+
return -1;
87+
88+
return 0;
89+
}
90+
4391
#endif /* #if defined(__i386__) || defined(__x86_64__) */

tools/power/cpupower/utils/helpers/msr.c

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
/* Intel specific MSRs */
1212
#define MSR_IA32_PERF_STATUS 0x198
1313
#define MSR_IA32_MISC_ENABLES 0x1a0
14-
#define MSR_IA32_ENERGY_PERF_BIAS 0x1b0
1514
#define MSR_NEHALEM_TURBO_RATIO_LIMIT 0x1ad
1615

1716
/*
@@ -73,33 +72,6 @@ int write_msr(int cpu, unsigned int idx, unsigned long long val)
7372
return -1;
7473
}
7574

76-
int msr_intel_get_perf_bias(unsigned int cpu)
77-
{
78-
unsigned long long val;
79-
int ret;
80-
81-
if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_PERF_BIAS))
82-
return -1;
83-
84-
ret = read_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &val);
85-
if (ret)
86-
return ret;
87-
return val;
88-
}
89-
90-
int msr_intel_set_perf_bias(unsigned int cpu, unsigned int val)
91-
{
92-
int ret;
93-
94-
if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_PERF_BIAS))
95-
return -1;
96-
97-
ret = write_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, val);
98-
if (ret)
99-
return ret;
100-
return 0;
101-
}
102-
10375
unsigned long long msr_intel_get_turbo_ratio(unsigned int cpu)
10476
{
10577
unsigned long long val;

0 commit comments

Comments
 (0)