Skip to content

Commit fb1fe10

Browse files
committed
cpufreq: intel_pstate: Operation mode control from sysfs
Make it possible to change the operation mode of intel_pstate with the help of a new sysfs attribute called "status". There are three possible configurations that can be selected using this attribute: "off" - The driver is not in use at this time. "active" - The driver works as a P-state governor (default). "passive" - The driver works as a regular cpufreq one and collaborates with the generic cpufreq governors (it sets P-states as requested by those governors). [This is the same mode the driver can be started in by passing intel_pstate=passive in the kernel command line.] The current setting is returned by reads from this attribute. Writing one of the above strings to it changes the operation mode as indicated by that string, if possible. If HW-managed P-states (HWP) feature is enabled, it is not possible to change the driver's operation mode and attempts to write to this attribute will fail. Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent 0c30b65 commit fb1fe10

2 files changed

Lines changed: 195 additions & 44 deletions

File tree

‎Documentation/cpu-freq/intel-pstate.txt‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,21 @@ Sysfs will show :
8585
Refer to "Intel® 64 and IA-32 Architectures Software Developer’s Manual
8686
Volume 3: System Programming Guide" to understand ratios.
8787

88+
There is one more sysfs attribute in /sys/devices/system/cpu/intel_pstate/
89+
that can be used for controlling the operation mode of the driver:
90+
91+
status: Three settings are possible:
92+
"off" - The driver is not in use at this time.
93+
"active" - The driver works as a P-state governor (default).
94+
"passive" - The driver works as a regular cpufreq one and collaborates
95+
with the generic cpufreq governors (it sets P-states as
96+
requested by those governors).
97+
The current setting is returned by reads from this attribute. Writing one
98+
of the above strings to it changes the operation mode as indicated by that
99+
string, if possible. If HW-managed P-states (HWP) are enabled, it is not
100+
possible to change the driver's operation mode and attempts to write to
101+
this attribute will fail.
102+
88103
cpufreq sysfs for Intel P-State
89104

90105
Since this driver registers with cpufreq, cpufreq sysfs is also presented.

‎drivers/cpufreq/intel_pstate.c‎

Lines changed: 180 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,6 @@ static void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
541541

542542
acpi_processor_unregister_performance(policy->cpu);
543543
}
544-
545544
#else
546545
static inline void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
547546
{
@@ -1010,35 +1009,57 @@ static int pid_param_get(void *data, u64 *val)
10101009
}
10111010
DEFINE_SIMPLE_ATTRIBUTE(fops_pid_param, pid_param_get, pid_param_set, "%llu\n");
10121011

1012+
static struct dentry *debugfs_parent;
1013+
10131014
struct pid_param {
10141015
char *name;
10151016
void *value;
1017+
struct dentry *dentry;
10161018
};
10171019

10181020
static struct pid_param pid_files[] = {
1019-
{"sample_rate_ms", &pid_params.sample_rate_ms},
1020-
{"d_gain_pct", &pid_params.d_gain_pct},
1021-
{"i_gain_pct", &pid_params.i_gain_pct},
1022-
{"deadband", &pid_params.deadband},
1023-
{"setpoint", &pid_params.setpoint},
1024-
{"p_gain_pct", &pid_params.p_gain_pct},
1025-
{NULL, NULL}
1021+
{"sample_rate_ms", &pid_params.sample_rate_ms, },
1022+
{"d_gain_pct", &pid_params.d_gain_pct, },
1023+
{"i_gain_pct", &pid_params.i_gain_pct, },
1024+
{"deadband", &pid_params.deadband, },
1025+
{"setpoint", &pid_params.setpoint, },
1026+
{"p_gain_pct", &pid_params.p_gain_pct, },
1027+
{NULL, NULL, }
10261028
};
10271029

1028-
static void __init intel_pstate_debug_expose_params(void)
1030+
static void intel_pstate_debug_expose_params(void)
10291031
{
1030-
struct dentry *debugfs_parent;
1031-
int i = 0;
1032+
int i;
10321033

10331034
debugfs_parent = debugfs_create_dir("pstate_snb", NULL);
10341035
if (IS_ERR_OR_NULL(debugfs_parent))
10351036
return;
1036-
while (pid_files[i].name) {
1037-
debugfs_create_file(pid_files[i].name, 0660,
1038-
debugfs_parent, pid_files[i].value,
1039-
&fops_pid_param);
1040-
i++;
1037+
1038+
for (i = 0; pid_files[i].name; i++) {
1039+
struct dentry *dentry;
1040+
1041+
dentry = debugfs_create_file(pid_files[i].name, 0660,
1042+
debugfs_parent, pid_files[i].value,
1043+
&fops_pid_param);
1044+
if (!IS_ERR(dentry))
1045+
pid_files[i].dentry = dentry;
1046+
}
1047+
}
1048+
1049+
static void intel_pstate_debug_hide_params(void)
1050+
{
1051+
int i;
1052+
1053+
if (IS_ERR_OR_NULL(debugfs_parent))
1054+
return;
1055+
1056+
for (i = 0; pid_files[i].name; i++) {
1057+
debugfs_remove(pid_files[i].dentry);
1058+
pid_files[i].dentry = NULL;
10411059
}
1060+
1061+
debugfs_remove(debugfs_parent);
1062+
debugfs_parent = NULL;
10421063
}
10431064

10441065
/************************** debugfs end ************************/
@@ -1051,6 +1072,34 @@ static void __init intel_pstate_debug_expose_params(void)
10511072
return sprintf(buf, "%u\n", limits->object); \
10521073
}
10531074

1075+
static ssize_t intel_pstate_show_status(char *buf);
1076+
static int intel_pstate_update_status(const char *buf, size_t size);
1077+
1078+
static ssize_t show_status(struct kobject *kobj,
1079+
struct attribute *attr, char *buf)
1080+
{
1081+
ssize_t ret;
1082+
1083+
mutex_lock(&intel_pstate_driver_lock);
1084+
ret = intel_pstate_show_status(buf);
1085+
mutex_unlock(&intel_pstate_driver_lock);
1086+
1087+
return ret;
1088+
}
1089+
1090+
static ssize_t store_status(struct kobject *a, struct attribute *b,
1091+
const char *buf, size_t count)
1092+
{
1093+
char *p = memchr(buf, '\n', count);
1094+
int ret;
1095+
1096+
mutex_lock(&intel_pstate_driver_lock);
1097+
ret = intel_pstate_update_status(buf, p ? p - buf : count);
1098+
mutex_unlock(&intel_pstate_driver_lock);
1099+
1100+
return ret < 0 ? ret : count;
1101+
}
1102+
10541103
static ssize_t show_turbo_pct(struct kobject *kobj,
10551104
struct attribute *attr, char *buf)
10561105
{
@@ -1236,13 +1285,15 @@ static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b,
12361285
show_one(max_perf_pct, max_perf_pct);
12371286
show_one(min_perf_pct, min_perf_pct);
12381287

1288+
define_one_global_rw(status);
12391289
define_one_global_rw(no_turbo);
12401290
define_one_global_rw(max_perf_pct);
12411291
define_one_global_rw(min_perf_pct);
12421292
define_one_global_ro(turbo_pct);
12431293
define_one_global_ro(num_pstates);
12441294

12451295
static struct attribute *intel_pstate_attributes[] = {
1296+
&status.attr,
12461297
&no_turbo.attr,
12471298
&turbo_pct.attr,
12481299
&num_pstates.attr,
@@ -2329,6 +2380,111 @@ static struct cpufreq_driver intel_cpufreq = {
23292380

23302381
static struct cpufreq_driver *intel_pstate_driver = &intel_pstate;
23312382

2383+
static void intel_pstate_driver_cleanup(void)
2384+
{
2385+
unsigned int cpu;
2386+
2387+
get_online_cpus();
2388+
for_each_online_cpu(cpu) {
2389+
if (all_cpu_data[cpu]) {
2390+
if (intel_pstate_driver == &intel_pstate)
2391+
intel_pstate_clear_update_util_hook(cpu);
2392+
2393+
kfree(all_cpu_data[cpu]);
2394+
all_cpu_data[cpu] = NULL;
2395+
}
2396+
}
2397+
put_online_cpus();
2398+
}
2399+
2400+
static int intel_pstate_register_driver(void)
2401+
{
2402+
int ret;
2403+
2404+
ret = cpufreq_register_driver(intel_pstate_driver);
2405+
if (ret) {
2406+
intel_pstate_driver_cleanup();
2407+
return ret;
2408+
}
2409+
2410+
mutex_lock(&intel_pstate_limits_lock);
2411+
driver_registered = true;
2412+
mutex_unlock(&intel_pstate_limits_lock);
2413+
2414+
if (intel_pstate_driver == &intel_pstate && !hwp_active &&
2415+
pstate_funcs.get_target_pstate != get_target_pstate_use_cpu_load)
2416+
intel_pstate_debug_expose_params();
2417+
2418+
return 0;
2419+
}
2420+
2421+
static int intel_pstate_unregister_driver(void)
2422+
{
2423+
if (hwp_active)
2424+
return -EBUSY;
2425+
2426+
if (intel_pstate_driver == &intel_pstate && !hwp_active &&
2427+
pstate_funcs.get_target_pstate != get_target_pstate_use_cpu_load)
2428+
intel_pstate_debug_hide_params();
2429+
2430+
mutex_lock(&intel_pstate_limits_lock);
2431+
driver_registered = false;
2432+
mutex_unlock(&intel_pstate_limits_lock);
2433+
2434+
cpufreq_unregister_driver(intel_pstate_driver);
2435+
intel_pstate_driver_cleanup();
2436+
2437+
return 0;
2438+
}
2439+
2440+
static ssize_t intel_pstate_show_status(char *buf)
2441+
{
2442+
if (!driver_registered)
2443+
return sprintf(buf, "off\n");
2444+
2445+
return sprintf(buf, "%s\n", intel_pstate_driver == &intel_pstate ?
2446+
"active" : "passive");
2447+
}
2448+
2449+
static int intel_pstate_update_status(const char *buf, size_t size)
2450+
{
2451+
int ret;
2452+
2453+
if (size == 3 && !strncmp(buf, "off", size))
2454+
return driver_registered ?
2455+
intel_pstate_unregister_driver() : -EINVAL;
2456+
2457+
if (size == 6 && !strncmp(buf, "active", size)) {
2458+
if (driver_registered) {
2459+
if (intel_pstate_driver == &intel_pstate)
2460+
return 0;
2461+
2462+
ret = intel_pstate_unregister_driver();
2463+
if (ret)
2464+
return ret;
2465+
}
2466+
2467+
intel_pstate_driver = &intel_pstate;
2468+
return intel_pstate_register_driver();
2469+
}
2470+
2471+
if (size == 7 && !strncmp(buf, "passive", size)) {
2472+
if (driver_registered) {
2473+
if (intel_pstate_driver != &intel_pstate)
2474+
return 0;
2475+
2476+
ret = intel_pstate_unregister_driver();
2477+
if (ret)
2478+
return ret;
2479+
}
2480+
2481+
intel_pstate_driver = &intel_cpufreq;
2482+
return intel_pstate_register_driver();
2483+
}
2484+
2485+
return -EINVAL;
2486+
}
2487+
23322488
static int no_load __initdata;
23332489
static int no_hwp __initdata;
23342490
static int hwp_only __initdata;
@@ -2516,9 +2672,9 @@ static const struct x86_cpu_id hwp_support_ids[] __initconst = {
25162672

25172673
static int __init intel_pstate_init(void)
25182674
{
2519-
int cpu, rc = 0;
25202675
const struct x86_cpu_id *id;
25212676
struct cpu_defaults *cpu_def;
2677+
int rc = 0;
25222678

25232679
if (no_load)
25242680
return -ENODEV;
@@ -2550,49 +2706,29 @@ static int __init intel_pstate_init(void)
25502706
if (intel_pstate_platform_pwr_mgmt_exists())
25512707
return -ENODEV;
25522708

2709+
if (!hwp_active && hwp_only)
2710+
return -ENOTSUPP;
2711+
25532712
pr_info("Intel P-state driver initializing\n");
25542713

25552714
all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus());
25562715
if (!all_cpu_data)
25572716
return -ENOMEM;
25582717

2559-
if (!hwp_active && hwp_only)
2560-
goto out;
2561-
25622718
intel_pstate_request_control_from_smm();
25632719

25642720
intel_pstate_sysfs_expose_params();
25652721

2566-
rc = cpufreq_register_driver(intel_pstate_driver);
2567-
if (rc)
2568-
goto out;
2569-
25702722
mutex_lock(&intel_pstate_driver_lock);
2571-
driver_registered = true;
2723+
rc = intel_pstate_register_driver();
25722724
mutex_unlock(&intel_pstate_driver_lock);
2573-
2574-
if (intel_pstate_driver == &intel_pstate && !hwp_active &&
2575-
pstate_funcs.get_target_pstate != get_target_pstate_use_cpu_load)
2576-
intel_pstate_debug_expose_params();
2725+
if (rc)
2726+
return rc;
25772727

25782728
if (hwp_active)
25792729
pr_info("HWP enabled\n");
25802730

2581-
return rc;
2582-
out:
2583-
get_online_cpus();
2584-
for_each_online_cpu(cpu) {
2585-
if (all_cpu_data[cpu]) {
2586-
if (intel_pstate_driver == &intel_pstate)
2587-
intel_pstate_clear_update_util_hook(cpu);
2588-
2589-
kfree(all_cpu_data[cpu]);
2590-
}
2591-
}
2592-
2593-
put_online_cpus();
2594-
vfree(all_cpu_data);
2595-
return -ENODEV;
2731+
return 0;
25962732
}
25972733
device_initcall(intel_pstate_init);
25982734

0 commit comments

Comments
 (0)