blob: f8cdde4bf6cd86030a8f829d00fdac862d5847f2 [file] [log] [blame]
Thomas Renningerc0672862006-01-27 16:15:26 +01001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/drivers/cpufreq/cpufreq_userspace.c
4 *
5 * Copyright (C) 2001 Russell King
6 * (C) 2002 - 2004 Dominik Brodowski <linux@brodo.de>
7 *
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.
11 *
12 */
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#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>
Arjan van de Ven153d7f32006-07-26 15:40:07 +020021#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/types.h>
23#include <linux/fs.h>
24#include <linux/sysfs.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080025#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include <asm/uaccess.h>
28
29
30/**
31 * A few values needed by the userspace governor
32 */
33static unsigned int cpu_max_freq[NR_CPUS];
34static unsigned int cpu_min_freq[NR_CPUS];
35static unsigned int cpu_cur_freq[NR_CPUS]; /* current CPU freq */
36static unsigned int cpu_set_freq[NR_CPUS]; /* CPU freq desired by userspace */
37static unsigned int cpu_is_managed[NR_CPUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080039static DEFINE_MUTEX (userspace_mutex);
Venki Pallipadic7f652e2007-06-20 14:24:00 -070040static int cpus_using_userspace_governor;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_GOVERNOR, "userspace", msg)
43
44/* keep track of frequency transitions */
Dave Jones32ee8c32006-02-28 00:43:23 -050045static int
Linus Torvalds1da177e2005-04-16 15:20:36 -070046userspace_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
47 void *data)
48{
49 struct cpufreq_freqs *freq = data;
50
Venki Pallipadic7f652e2007-06-20 14:24:00 -070051 if (!cpu_is_managed[freq->cpu])
52 return 0;
53
54 dprintk("saving cpu_cur_freq of cpu %u to be %u kHz\n",
55 freq->cpu, freq->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 cpu_cur_freq[freq->cpu] = freq->new;
57
58 return 0;
59}
60
61static struct notifier_block userspace_cpufreq_notifier_block = {
62 .notifier_call = userspace_cpufreq_notifier
63};
64
65
Dave Jones32ee8c32006-02-28 00:43:23 -050066/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 * cpufreq_set - set the CPU frequency
68 * @freq: target frequency in kHz
69 * @cpu: CPU for which the frequency is to be set
70 *
71 * Sets the CPU frequency to freq.
72 */
Thomas Renningerc0672862006-01-27 16:15:26 +010073static int cpufreq_set(unsigned int freq, struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 int ret = -EINVAL;
76
Thomas Renningerc0672862006-01-27 16:15:26 +010077 dprintk("cpufreq_set for cpu %u, freq %u kHz\n", policy->cpu, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080079 mutex_lock(&userspace_mutex);
Thomas Renningerc0672862006-01-27 16:15:26 +010080 if (!cpu_is_managed[policy->cpu])
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 goto err;
82
Thomas Renningerc0672862006-01-27 16:15:26 +010083 cpu_set_freq[policy->cpu] = freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Thomas Renningerc0672862006-01-27 16:15:26 +010085 if (freq < cpu_min_freq[policy->cpu])
86 freq = cpu_min_freq[policy->cpu];
87 if (freq > cpu_max_freq[policy->cpu])
88 freq = cpu_max_freq[policy->cpu];
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 /*
91 * We're safe from concurrent calls to ->target() here
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080092 * as we hold the userspace_mutex lock. If we were calling
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * cpufreq_driver_target, a deadlock situation might occur:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080094 * A: cpufreq_set (lock userspace_mutex) -> cpufreq_driver_target(lock policy->lock)
95 * B: cpufreq_set_policy(lock policy->lock) -> __cpufreq_governor -> cpufreq_governor_userspace (lock userspace_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 */
Thomas Renningerc0672862006-01-27 16:15:26 +010097 ret = __cpufreq_driver_target(policy, freq, CPUFREQ_RELATION_L);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 err:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800100 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return ret;
102}
103
104
105/************************** sysfs interface ************************/
106static ssize_t show_speed (struct cpufreq_policy *policy, char *buf)
107{
108 return sprintf (buf, "%u\n", cpu_cur_freq[policy->cpu]);
109}
110
Dave Jones32ee8c32006-02-28 00:43:23 -0500111static ssize_t
112store_speed (struct cpufreq_policy *policy, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 unsigned int freq = 0;
115 unsigned int ret;
116
117 ret = sscanf (buf, "%u", &freq);
118 if (ret != 1)
119 return -EINVAL;
120
Thomas Renningerc0672862006-01-27 16:15:26 +0100121 cpufreq_set(freq, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 return count;
124}
125
Dave Jones32ee8c32006-02-28 00:43:23 -0500126static struct freq_attr freq_attr_scaling_setspeed =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Tejun Heo7b595752007-06-14 03:45:17 +0900128 .attr = { .name = "scaling_setspeed", .mode = 0644 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 .show = show_speed,
130 .store = store_speed,
131};
132
133static int cpufreq_governor_userspace(struct cpufreq_policy *policy,
134 unsigned int event)
135{
136 unsigned int cpu = policy->cpu;
Jeff Garzik914f7c32006-10-20 14:31:00 -0700137 int rc = 0;
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 switch (event) {
140 case CPUFREQ_GOV_START:
141 if (!cpu_online(cpu))
142 return -EINVAL;
143 BUG_ON(!policy->cur);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800144 mutex_lock(&userspace_mutex);
Jeff Garzik914f7c32006-10-20 14:31:00 -0700145 rc = sysfs_create_file (&policy->kobj,
146 &freq_attr_scaling_setspeed.attr);
147 if (rc)
148 goto start_out;
149
Venki Pallipadic7f652e2007-06-20 14:24:00 -0700150 if (cpus_using_userspace_governor == 0) {
151 cpufreq_register_notifier(
152 &userspace_cpufreq_notifier_block,
153 CPUFREQ_TRANSITION_NOTIFIER);
154 }
155 cpus_using_userspace_governor++;
156
Dave Jones32ee8c32006-02-28 00:43:23 -0500157 cpu_is_managed[cpu] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 cpu_min_freq[cpu] = policy->min;
159 cpu_max_freq[cpu] = policy->max;
160 cpu_cur_freq[cpu] = policy->cur;
161 cpu_set_freq[cpu] = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 dprintk("managing cpu %u started (%u - %u kHz, currently %u kHz)\n", cpu, cpu_min_freq[cpu], cpu_max_freq[cpu], cpu_cur_freq[cpu]);
Jeff Garzik914f7c32006-10-20 14:31:00 -0700163start_out:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800164 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 break;
166 case CPUFREQ_GOV_STOP:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800167 mutex_lock(&userspace_mutex);
Venki Pallipadic7f652e2007-06-20 14:24:00 -0700168 cpus_using_userspace_governor--;
169 if (cpus_using_userspace_governor == 0) {
170 cpufreq_unregister_notifier(
171 &userspace_cpufreq_notifier_block,
172 CPUFREQ_TRANSITION_NOTIFIER);
173 }
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 cpu_is_managed[cpu] = 0;
176 cpu_min_freq[cpu] = 0;
177 cpu_max_freq[cpu] = 0;
178 cpu_set_freq[cpu] = 0;
179 sysfs_remove_file (&policy->kobj, &freq_attr_scaling_setspeed.attr);
180 dprintk("managing cpu %u stopped\n", cpu);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800181 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 break;
183 case CPUFREQ_GOV_LIMITS:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800184 mutex_lock(&userspace_mutex);
Thomas Renningerc0672862006-01-27 16:15:26 +0100185 dprintk("limit event for cpu %u: %u - %u kHz,"
186 "currently %u kHz, last set to %u kHz\n",
187 cpu, policy->min, policy->max,
188 cpu_cur_freq[cpu], cpu_set_freq[cpu]);
189 if (policy->max < cpu_set_freq[cpu]) {
190 __cpufreq_driver_target(policy, policy->max,
191 CPUFREQ_RELATION_H);
192 }
193 else if (policy->min > cpu_set_freq[cpu]) {
194 __cpufreq_driver_target(policy, policy->min,
195 CPUFREQ_RELATION_L);
196 }
197 else {
198 __cpufreq_driver_target(policy, cpu_set_freq[cpu],
199 CPUFREQ_RELATION_L);
200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 cpu_min_freq[cpu] = policy->min;
202 cpu_max_freq[cpu] = policy->max;
Thomas Renningerc0672862006-01-27 16:15:26 +0100203 cpu_cur_freq[cpu] = policy->cur;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800204 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 break;
206 }
Jeff Garzik914f7c32006-10-20 14:31:00 -0700207 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
210
211struct cpufreq_governor cpufreq_gov_userspace = {
212 .name = "userspace",
213 .governor = cpufreq_governor_userspace,
214 .owner = THIS_MODULE,
215};
216EXPORT_SYMBOL(cpufreq_gov_userspace);
217
218static int __init cpufreq_gov_userspace_init(void)
219{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return cpufreq_register_governor(&cpufreq_gov_userspace);
221}
222
223
224static void __exit cpufreq_gov_userspace_exit(void)
225{
226 cpufreq_unregister_governor(&cpufreq_gov_userspace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
229
230MODULE_AUTHOR ("Dominik Brodowski <linux@brodo.de>, Russell King <rmk@arm.linux.org.uk>");
231MODULE_DESCRIPTION ("CPUfreq policy governor 'userspace'");
232MODULE_LICENSE ("GPL");
233
Johannes Weiner69157192008-01-17 15:21:08 -0800234#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235fs_initcall(cpufreq_gov_userspace_init);
Johannes Weiner69157192008-01-17 15:21:08 -0800236#else
237module_init(cpufreq_gov_userspace_init);
238#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239module_exit(cpufreq_gov_userspace_exit);