Skip to content

Commit df8776b

Browse files
wkarnyshuahkh
authored andcommitted
cpupower: Add support for amd_pstate mode change
amd_pstate supports changing of its mode dynamically via `status` sysfs file. Add the same capability in cpupower. To change the mode to active mode use below command: cpupower set --amd-pstate-mode active Acked-by: Huang Rui <ray.huang@amd.com> Reviewed-by: Gautham R. Shenoy <gautham.shenoy@amd.com> Reviewed-by: Mario Limonciello <mario.limonciello@amd.com> Signed-off-by: Wyes Karny <wyes.karny@amd.com> Tested-by: Perry Yuan <Perry.Yuan@amd.com> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent f2ab555 commit df8776b

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
static struct option set_opts[] = {
2020
{"perf-bias", required_argument, NULL, 'b'},
2121
{"epp", required_argument, NULL, 'e'},
22+
{"amd-pstate-mode", required_argument, NULL, 'm'},
2223
{ },
2324
};
2425

@@ -39,12 +40,13 @@ int cmd_set(int argc, char **argv)
3940
struct {
4041
int perf_bias:1;
4142
int epp:1;
43+
int mode:1;
4244
};
4345
int params;
4446
} params;
4547
int perf_bias = 0;
4648
int ret = 0;
47-
char epp[30];
49+
char epp[30], mode[20];
4850

4951
ret = uname(&uts);
5052
if (!ret && (!strcmp(uts.machine, "ppc64le") ||
@@ -58,7 +60,7 @@ int cmd_set(int argc, char **argv)
5860

5961
params.params = 0;
6062
/* parameter parsing */
61-
while ((ret = getopt_long(argc, argv, "b:e:",
63+
while ((ret = getopt_long(argc, argv, "b:e:m:",
6264
set_opts, NULL)) != -1) {
6365
switch (ret) {
6466
case 'b':
@@ -81,6 +83,17 @@ int cmd_set(int argc, char **argv)
8183
}
8284
params.epp = 1;
8385
break;
86+
case 'm':
87+
if (cpupower_cpu_info.vendor != X86_VENDOR_AMD)
88+
print_wrong_arg_exit();
89+
if (params.mode)
90+
print_wrong_arg_exit();
91+
if (sscanf(optarg, "%19s", mode) != 1) {
92+
print_wrong_arg_exit();
93+
return -EINVAL;
94+
}
95+
params.mode = 1;
96+
break;
8497
default:
8598
print_wrong_arg_exit();
8699
}
@@ -89,6 +102,12 @@ int cmd_set(int argc, char **argv)
89102
if (!params.params)
90103
print_wrong_arg_exit();
91104

105+
if (params.mode) {
106+
ret = cpupower_set_amd_pstate_mode(mode);
107+
if (ret)
108+
fprintf(stderr, "Error setting mode\n");
109+
}
110+
92111
/* Default is: set all CPUs */
93112
if (bitmask_isallclear(cpus_chosen))
94113
bitmask_setall(cpus_chosen);
@@ -123,6 +142,7 @@ int cmd_set(int argc, char **argv)
123142
break;
124143
}
125144
}
145+
126146
}
127147
return ret;
128148
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ extern int cpupower_intel_get_perf_bias(unsigned int cpu);
117117
extern unsigned long long msr_intel_get_turbo_ratio(unsigned int cpu);
118118

119119
extern int cpupower_set_epp(unsigned int cpu, char *epp);
120+
extern int cpupower_set_amd_pstate_mode(char *mode);
120121

121122
/* Read/Write msr ****************************/
122123

@@ -177,6 +178,8 @@ static inline unsigned long long msr_intel_get_turbo_ratio(unsigned int cpu)
177178

178179
static inline int cpupower_set_epp(unsigned int cpu, char *epp)
179180
{ return -1; };
181+
static inline int cpupower_set_amd_pstate_mode(char *mode)
182+
{ return -1; };
180183

181184
/* Read/Write msr ****************************/
182185

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,24 @@ int cpupower_set_epp(unsigned int cpu, char *epp)
106106
return 0;
107107
}
108108

109+
int cpupower_set_amd_pstate_mode(char *mode)
110+
{
111+
char path[SYSFS_PATH_MAX];
112+
char linebuf[20] = {};
113+
114+
snprintf(path, sizeof(path), PATH_TO_CPU "amd_pstate/status");
115+
116+
if (!is_valid_path(path))
117+
return -1;
118+
119+
snprintf(linebuf, sizeof(linebuf), "%s\n", mode);
120+
121+
if (cpupower_write_sysfs(path, linebuf, 20) <= 0)
122+
return -1;
123+
124+
return 0;
125+
}
126+
109127
bool cpupower_amd_pstate_enabled(void)
110128
{
111129
char *driver = cpufreq_get_driver(0);

0 commit comments

Comments
 (0)