x86/amd-iommu: Add per IOMMU reference counting
[linux/fpc-iii.git] / drivers / cpufreq / cpufreq_userspace.c
blob66d2d1d6c80f1dbc0c17388e4dbea81c18c5628e
2 /*
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>
23 #include <linux/fs.h>
24 #include <linux/sysfs.h>
25 #include <linux/mutex.h>
27 /**
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
34 userspace */
35 static DEFINE_PER_CPU(unsigned int, cpu_is_managed);
37 static DEFINE_MUTEX(userspace_mutex);
38 static int cpus_using_userspace_governor;
40 #define dprintk(msg...) \
41 cpufreq_debug_printk(CPUFREQ_DEBUG_GOVERNOR, "userspace", msg)
43 /* keep track of frequency transitions */
44 static int
45 userspace_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
46 void *data)
48 struct cpufreq_freqs *freq = data;
50 if (!per_cpu(cpu_is_managed, freq->cpu))
51 return 0;
53 dprintk("saving cpu_cur_freq of cpu %u to be %u kHz\n",
54 freq->cpu, freq->new);
55 per_cpu(cpu_cur_freq, freq->cpu) = freq->new;
57 return 0;
60 static struct notifier_block userspace_cpufreq_notifier_block = {
61 .notifier_call = userspace_cpufreq_notifier
65 /**
66 * cpufreq_set - set the CPU frequency
67 * @policy: pointer to policy struct where freq is being set
68 * @freq: target frequency in kHz
70 * Sets the CPU frequency to freq.
72 static int cpufreq_set(struct cpufreq_policy *policy, unsigned int freq)
74 int ret = -EINVAL;
76 dprintk("cpufreq_set for cpu %u, freq %u kHz\n", policy->cpu, freq);
78 mutex_lock(&userspace_mutex);
79 if (!per_cpu(cpu_is_managed, policy->cpu))
80 goto err;
82 per_cpu(cpu_set_freq, policy->cpu) = freq;
84 if (freq < per_cpu(cpu_min_freq, policy->cpu))
85 freq = per_cpu(cpu_min_freq, policy->cpu);
86 if (freq > per_cpu(cpu_max_freq, policy->cpu))
87 freq = per_cpu(cpu_max_freq, policy->cpu);
90 * We're safe from concurrent calls to ->target() here
91 * as we hold the userspace_mutex lock. If we were calling
92 * cpufreq_driver_target, a deadlock situation might occur:
93 * A: cpufreq_set (lock userspace_mutex) ->
94 * cpufreq_driver_target(lock policy->lock)
95 * B: cpufreq_set_policy(lock policy->lock) ->
96 * __cpufreq_governor ->
97 * cpufreq_governor_userspace (lock userspace_mutex)
99 ret = __cpufreq_driver_target(policy, freq, CPUFREQ_RELATION_L);
101 err:
102 mutex_unlock(&userspace_mutex);
103 return ret;
107 static ssize_t show_speed(struct cpufreq_policy *policy, char *buf)
109 return sprintf(buf, "%u\n", per_cpu(cpu_cur_freq, policy->cpu));
112 static int cpufreq_governor_userspace(struct cpufreq_policy *policy,
113 unsigned int event)
115 unsigned int cpu = policy->cpu;
116 int rc = 0;
118 switch (event) {
119 case CPUFREQ_GOV_START:
120 if (!cpu_online(cpu))
121 return -EINVAL;
122 BUG_ON(!policy->cur);
123 mutex_lock(&userspace_mutex);
125 if (cpus_using_userspace_governor == 0) {
126 cpufreq_register_notifier(
127 &userspace_cpufreq_notifier_block,
128 CPUFREQ_TRANSITION_NOTIFIER);
130 cpus_using_userspace_governor++;
132 per_cpu(cpu_is_managed, cpu) = 1;
133 per_cpu(cpu_min_freq, cpu) = policy->min;
134 per_cpu(cpu_max_freq, cpu) = policy->max;
135 per_cpu(cpu_cur_freq, cpu) = policy->cur;
136 per_cpu(cpu_set_freq, cpu) = policy->cur;
137 dprintk("managing cpu %u started "
138 "(%u - %u kHz, currently %u kHz)\n",
139 cpu,
140 per_cpu(cpu_min_freq, cpu),
141 per_cpu(cpu_max_freq, cpu),
142 per_cpu(cpu_cur_freq, cpu));
144 mutex_unlock(&userspace_mutex);
145 break;
146 case CPUFREQ_GOV_STOP:
147 mutex_lock(&userspace_mutex);
148 cpus_using_userspace_governor--;
149 if (cpus_using_userspace_governor == 0) {
150 cpufreq_unregister_notifier(
151 &userspace_cpufreq_notifier_block,
152 CPUFREQ_TRANSITION_NOTIFIER);
155 per_cpu(cpu_is_managed, cpu) = 0;
156 per_cpu(cpu_min_freq, cpu) = 0;
157 per_cpu(cpu_max_freq, cpu) = 0;
158 per_cpu(cpu_set_freq, cpu) = 0;
159 dprintk("managing cpu %u stopped\n", cpu);
160 mutex_unlock(&userspace_mutex);
161 break;
162 case CPUFREQ_GOV_LIMITS:
163 mutex_lock(&userspace_mutex);
164 dprintk("limit event for cpu %u: %u - %u kHz, "
165 "currently %u kHz, last set to %u kHz\n",
166 cpu, policy->min, policy->max,
167 per_cpu(cpu_cur_freq, cpu),
168 per_cpu(cpu_set_freq, cpu));
169 if (policy->max < per_cpu(cpu_set_freq, cpu)) {
170 __cpufreq_driver_target(policy, policy->max,
171 CPUFREQ_RELATION_H);
172 } else if (policy->min > per_cpu(cpu_set_freq, cpu)) {
173 __cpufreq_driver_target(policy, policy->min,
174 CPUFREQ_RELATION_L);
175 } else {
176 __cpufreq_driver_target(policy,
177 per_cpu(cpu_set_freq, cpu),
178 CPUFREQ_RELATION_L);
180 per_cpu(cpu_min_freq, cpu) = policy->min;
181 per_cpu(cpu_max_freq, cpu) = policy->max;
182 per_cpu(cpu_cur_freq, cpu) = policy->cur;
183 mutex_unlock(&userspace_mutex);
184 break;
186 return rc;
190 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
191 static
192 #endif
193 struct cpufreq_governor cpufreq_gov_userspace = {
194 .name = "userspace",
195 .governor = cpufreq_governor_userspace,
196 .store_setspeed = cpufreq_set,
197 .show_setspeed = show_speed,
198 .owner = THIS_MODULE,
201 static int __init cpufreq_gov_userspace_init(void)
203 return cpufreq_register_governor(&cpufreq_gov_userspace);
207 static void __exit cpufreq_gov_userspace_exit(void)
209 cpufreq_unregister_governor(&cpufreq_gov_userspace);
213 MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>, "
214 "Russell King <rmk@arm.linux.org.uk>");
215 MODULE_DESCRIPTION("CPUfreq policy governor 'userspace'");
216 MODULE_LICENSE("GPL");
218 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
219 fs_initcall(cpufreq_gov_userspace_init);
220 #else
221 module_init(cpufreq_gov_userspace_init);
222 #endif
223 module_exit(cpufreq_gov_userspace_exit);