3 * linux/drivers/cpufreq/cpufreq_userspace.c
5 * Copyright (C) 2001 Russell King
6 * (C) 2002 - 2004 Dominik Brodowski <linux@brodo.de>
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 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/smp.h>
17 #include <linux/init.h>
18 #include <linux/spinlock.h>
19 #include <linux/interrupt.h>
20 #include <linux/cpufreq.h>
21 #include <linux/cpu.h>
22 #include <linux/types.h>
24 #include <linux/sysfs.h>
25 #include <linux/mutex.h>
28 * A few values needed by the userspace governor
30 static DEFINE_PER_CPU(unsigned int, cpu_max_freq
);
31 static DEFINE_PER_CPU(unsigned int, cpu_min_freq
);
32 static DEFINE_PER_CPU(unsigned int, cpu_cur_freq
); /* current CPU freq */
33 static DEFINE_PER_CPU(unsigned int, cpu_set_freq
); /* CPU freq desired by
35 static DEFINE_PER_CPU(unsigned int, cpu_is_managed
);
37 static DEFINE_MUTEX(userspace_mutex
);
38 static int cpus_using_userspace_governor
;
40 /* keep track of frequency transitions */
42 userspace_cpufreq_notifier(struct notifier_block
*nb
, unsigned long val
,
45 struct cpufreq_freqs
*freq
= data
;
47 if (!per_cpu(cpu_is_managed
, freq
->cpu
))
50 pr_debug("saving cpu_cur_freq of cpu %u to be %u kHz\n",
51 freq
->cpu
, freq
->new);
52 per_cpu(cpu_cur_freq
, freq
->cpu
) = freq
->new;
57 static struct notifier_block userspace_cpufreq_notifier_block
= {
58 .notifier_call
= userspace_cpufreq_notifier
63 * cpufreq_set - set the CPU frequency
64 * @policy: pointer to policy struct where freq is being set
65 * @freq: target frequency in kHz
67 * Sets the CPU frequency to freq.
69 static int cpufreq_set(struct cpufreq_policy
*policy
, unsigned int freq
)
73 pr_debug("cpufreq_set for cpu %u, freq %u kHz\n", policy
->cpu
, freq
);
75 mutex_lock(&userspace_mutex
);
76 if (!per_cpu(cpu_is_managed
, policy
->cpu
))
79 per_cpu(cpu_set_freq
, policy
->cpu
) = freq
;
81 if (freq
< per_cpu(cpu_min_freq
, policy
->cpu
))
82 freq
= per_cpu(cpu_min_freq
, policy
->cpu
);
83 if (freq
> per_cpu(cpu_max_freq
, policy
->cpu
))
84 freq
= per_cpu(cpu_max_freq
, policy
->cpu
);
87 * We're safe from concurrent calls to ->target() here
88 * as we hold the userspace_mutex lock. If we were calling
89 * cpufreq_driver_target, a deadlock situation might occur:
90 * A: cpufreq_set (lock userspace_mutex) ->
91 * cpufreq_driver_target(lock policy->lock)
92 * B: cpufreq_set_policy(lock policy->lock) ->
93 * __cpufreq_governor ->
94 * cpufreq_governor_userspace (lock userspace_mutex)
96 ret
= __cpufreq_driver_target(policy
, freq
, CPUFREQ_RELATION_L
);
99 mutex_unlock(&userspace_mutex
);
104 static ssize_t
show_speed(struct cpufreq_policy
*policy
, char *buf
)
106 return sprintf(buf
, "%u\n", per_cpu(cpu_cur_freq
, policy
->cpu
));
109 static int cpufreq_governor_userspace(struct cpufreq_policy
*policy
,
112 unsigned int cpu
= policy
->cpu
;
116 case CPUFREQ_GOV_START
:
117 if (!cpu_online(cpu
))
119 BUG_ON(!policy
->cur
);
120 mutex_lock(&userspace_mutex
);
122 if (cpus_using_userspace_governor
== 0) {
123 cpufreq_register_notifier(
124 &userspace_cpufreq_notifier_block
,
125 CPUFREQ_TRANSITION_NOTIFIER
);
127 cpus_using_userspace_governor
++;
129 per_cpu(cpu_is_managed
, cpu
) = 1;
130 per_cpu(cpu_min_freq
, cpu
) = policy
->min
;
131 per_cpu(cpu_max_freq
, cpu
) = policy
->max
;
132 per_cpu(cpu_cur_freq
, cpu
) = policy
->cur
;
133 per_cpu(cpu_set_freq
, cpu
) = policy
->cur
;
134 pr_debug("managing cpu %u started "
135 "(%u - %u kHz, currently %u kHz)\n",
137 per_cpu(cpu_min_freq
, cpu
),
138 per_cpu(cpu_max_freq
, cpu
),
139 per_cpu(cpu_cur_freq
, cpu
));
141 mutex_unlock(&userspace_mutex
);
143 case CPUFREQ_GOV_STOP
:
144 mutex_lock(&userspace_mutex
);
145 cpus_using_userspace_governor
--;
146 if (cpus_using_userspace_governor
== 0) {
147 cpufreq_unregister_notifier(
148 &userspace_cpufreq_notifier_block
,
149 CPUFREQ_TRANSITION_NOTIFIER
);
152 per_cpu(cpu_is_managed
, cpu
) = 0;
153 per_cpu(cpu_min_freq
, cpu
) = 0;
154 per_cpu(cpu_max_freq
, cpu
) = 0;
155 per_cpu(cpu_set_freq
, cpu
) = 0;
156 pr_debug("managing cpu %u stopped\n", cpu
);
157 mutex_unlock(&userspace_mutex
);
159 case CPUFREQ_GOV_LIMITS
:
160 mutex_lock(&userspace_mutex
);
161 pr_debug("limit event for cpu %u: %u - %u kHz, "
162 "currently %u kHz, last set to %u kHz\n",
163 cpu
, policy
->min
, policy
->max
,
164 per_cpu(cpu_cur_freq
, cpu
),
165 per_cpu(cpu_set_freq
, cpu
));
166 if (policy
->max
< per_cpu(cpu_set_freq
, cpu
)) {
167 __cpufreq_driver_target(policy
, policy
->max
,
169 } else if (policy
->min
> per_cpu(cpu_set_freq
, cpu
)) {
170 __cpufreq_driver_target(policy
, policy
->min
,
173 __cpufreq_driver_target(policy
,
174 per_cpu(cpu_set_freq
, cpu
),
177 per_cpu(cpu_min_freq
, cpu
) = policy
->min
;
178 per_cpu(cpu_max_freq
, cpu
) = policy
->max
;
179 per_cpu(cpu_cur_freq
, cpu
) = policy
->cur
;
180 mutex_unlock(&userspace_mutex
);
187 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
190 struct cpufreq_governor cpufreq_gov_userspace
= {
192 .governor
= cpufreq_governor_userspace
,
193 .store_setspeed
= cpufreq_set
,
194 .show_setspeed
= show_speed
,
195 .owner
= THIS_MODULE
,
198 static int __init
cpufreq_gov_userspace_init(void)
200 return cpufreq_register_governor(&cpufreq_gov_userspace
);
204 static void __exit
cpufreq_gov_userspace_exit(void)
206 cpufreq_unregister_governor(&cpufreq_gov_userspace
);
210 MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>, "
211 "Russell King <rmk@arm.linux.org.uk>");
212 MODULE_DESCRIPTION("CPUfreq policy governor 'userspace'");
213 MODULE_LICENSE("GPL");
215 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
216 fs_initcall(cpufreq_gov_userspace_init
);
218 module_init(cpufreq_gov_userspace_init
);
220 module_exit(cpufreq_gov_userspace_exit
);