3 * linux/drivers/cpufreq/cpufreq_userspace.c
5 * Copyright (C) 2001 Russell King
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/cpufreq.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
21 static DEFINE_PER_CPU(unsigned int, cpu_is_managed);
22 static DEFINE_MUTEX(userspace_mutex);
25 * cpufreq_set - set the CPU frequency
26 * @policy: pointer to policy struct where freq is being set
27 * @freq: target frequency in kHz
29 * Sets the CPU frequency to freq.
31 static int cpufreq_set(struct cpufreq_policy *policy, unsigned int freq)
35 pr_debug("cpufreq_set for cpu %u, freq %u kHz\n", policy->cpu, freq);
37 mutex_lock(&userspace_mutex);
38 if (!per_cpu(cpu_is_managed, policy->cpu))
42 * We're safe from concurrent calls to ->target() here
43 * as we hold the userspace_mutex lock. If we were calling
44 * cpufreq_driver_target, a deadlock situation might occur:
45 * A: cpufreq_set (lock userspace_mutex) ->
46 * cpufreq_driver_target(lock policy->lock)
47 * B: cpufreq_set_policy(lock policy->lock) ->
48 * __cpufreq_governor ->
49 * cpufreq_governor_userspace (lock userspace_mutex)
51 ret = __cpufreq_driver_target(policy, freq, CPUFREQ_RELATION_L);
54 mutex_unlock(&userspace_mutex);
58 static ssize_t show_speed(struct cpufreq_policy *policy, char *buf)
60 return sprintf(buf, "%u\n", policy->cur);
63 static int cpufreq_governor_userspace(struct cpufreq_policy *policy,
66 unsigned int cpu = policy->cpu;
70 case CPUFREQ_GOV_START:
72 pr_debug("started managing cpu %u\n", cpu);
74 mutex_lock(&userspace_mutex);
75 per_cpu(cpu_is_managed, cpu) = 1;
76 mutex_unlock(&userspace_mutex);
78 case CPUFREQ_GOV_STOP:
79 pr_debug("managing cpu %u stopped\n", cpu);
81 mutex_lock(&userspace_mutex);
82 per_cpu(cpu_is_managed, cpu) = 0;
83 mutex_unlock(&userspace_mutex);
85 case CPUFREQ_GOV_LIMITS:
86 mutex_lock(&userspace_mutex);
87 pr_debug("limit event for cpu %u: %u - %u kHz, currently %u kHz\n",
88 cpu, policy->min, policy->max,
91 if (policy->max < policy->cur)
92 __cpufreq_driver_target(policy, policy->max,
94 else if (policy->min > policy->cur)
95 __cpufreq_driver_target(policy, policy->min,
97 mutex_unlock(&userspace_mutex);
103 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
106 struct cpufreq_governor cpufreq_gov_userspace = {
108 .governor = cpufreq_governor_userspace,
109 .store_setspeed = cpufreq_set,
110 .show_setspeed = show_speed,
111 .owner = THIS_MODULE,
114 static int __init cpufreq_gov_userspace_init(void)
116 return cpufreq_register_governor(&cpufreq_gov_userspace);
119 static void __exit cpufreq_gov_userspace_exit(void)
121 cpufreq_unregister_governor(&cpufreq_gov_userspace);
126 MODULE_DESCRIPTION("CPUfreq policy governor 'userspace'");
127 MODULE_LICENSE("GPL");
129 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
130 fs_initcall(cpufreq_gov_userspace_init);
132 module_init(cpufreq_gov_userspace_init);
134 module_exit(cpufreq_gov_userspace_exit);