blob: 860345c7799ab1188c21945756ec6e696019b10e [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_GOVERNOR, "userspace", msg)
42
43/* keep track of frequency transitions */
Dave Jones32ee8c32006-02-28 00:43:23 -050044static int
Linus Torvalds1da177e2005-04-16 15:20:36 -070045userspace_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
46 void *data)
47{
48 struct cpufreq_freqs *freq = data;
49
50 dprintk("saving cpu_cur_freq of cpu %u to be %u kHz\n", freq->cpu, freq->new);
51 cpu_cur_freq[freq->cpu] = freq->new;
52
53 return 0;
54}
55
56static struct notifier_block userspace_cpufreq_notifier_block = {
57 .notifier_call = userspace_cpufreq_notifier
58};
59
60
Dave Jones32ee8c32006-02-28 00:43:23 -050061/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 * cpufreq_set - set the CPU frequency
63 * @freq: target frequency in kHz
64 * @cpu: CPU for which the frequency is to be set
65 *
66 * Sets the CPU frequency to freq.
67 */
Thomas Renningerc0672862006-01-27 16:15:26 +010068static int cpufreq_set(unsigned int freq, struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 int ret = -EINVAL;
71
Thomas Renningerc0672862006-01-27 16:15:26 +010072 dprintk("cpufreq_set for cpu %u, freq %u kHz\n", policy->cpu, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080074 mutex_lock(&userspace_mutex);
Thomas Renningerc0672862006-01-27 16:15:26 +010075 if (!cpu_is_managed[policy->cpu])
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 goto err;
77
Thomas Renningerc0672862006-01-27 16:15:26 +010078 cpu_set_freq[policy->cpu] = freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Thomas Renningerc0672862006-01-27 16:15:26 +010080 if (freq < cpu_min_freq[policy->cpu])
81 freq = cpu_min_freq[policy->cpu];
82 if (freq > cpu_max_freq[policy->cpu])
83 freq = cpu_max_freq[policy->cpu];
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 /*
86 * We're safe from concurrent calls to ->target() here
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080087 * as we hold the userspace_mutex lock. If we were calling
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * cpufreq_driver_target, a deadlock situation might occur:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080089 * A: cpufreq_set (lock userspace_mutex) -> cpufreq_driver_target(lock policy->lock)
90 * B: cpufreq_set_policy(lock policy->lock) -> __cpufreq_governor -> cpufreq_governor_userspace (lock userspace_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 */
Thomas Renningerc0672862006-01-27 16:15:26 +010092 ret = __cpufreq_driver_target(policy, freq, CPUFREQ_RELATION_L);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 err:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080095 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return ret;
97}
98
99
100/************************** sysfs interface ************************/
101static ssize_t show_speed (struct cpufreq_policy *policy, char *buf)
102{
103 return sprintf (buf, "%u\n", cpu_cur_freq[policy->cpu]);
104}
105
Dave Jones32ee8c32006-02-28 00:43:23 -0500106static ssize_t
107store_speed (struct cpufreq_policy *policy, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 unsigned int freq = 0;
110 unsigned int ret;
111
112 ret = sscanf (buf, "%u", &freq);
113 if (ret != 1)
114 return -EINVAL;
115
Thomas Renningerc0672862006-01-27 16:15:26 +0100116 cpufreq_set(freq, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 return count;
119}
120
Dave Jones32ee8c32006-02-28 00:43:23 -0500121static struct freq_attr freq_attr_scaling_setspeed =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 .attr = { .name = "scaling_setspeed", .mode = 0644, .owner = THIS_MODULE },
124 .show = show_speed,
125 .store = store_speed,
126};
127
128static int cpufreq_governor_userspace(struct cpufreq_policy *policy,
129 unsigned int event)
130{
131 unsigned int cpu = policy->cpu;
Jeff Garzik914f7c32006-10-20 14:31:00 -0700132 int rc = 0;
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 switch (event) {
135 case CPUFREQ_GOV_START:
136 if (!cpu_online(cpu))
137 return -EINVAL;
138 BUG_ON(!policy->cur);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800139 mutex_lock(&userspace_mutex);
Jeff Garzik914f7c32006-10-20 14:31:00 -0700140 rc = sysfs_create_file (&policy->kobj,
141 &freq_attr_scaling_setspeed.attr);
142 if (rc)
143 goto start_out;
144
Dave Jones32ee8c32006-02-28 00:43:23 -0500145 cpu_is_managed[cpu] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 cpu_min_freq[cpu] = policy->min;
147 cpu_max_freq[cpu] = policy->max;
148 cpu_cur_freq[cpu] = policy->cur;
149 cpu_set_freq[cpu] = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 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 -0700151start_out:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800152 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 break;
154 case CPUFREQ_GOV_STOP:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800155 mutex_lock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 cpu_is_managed[cpu] = 0;
157 cpu_min_freq[cpu] = 0;
158 cpu_max_freq[cpu] = 0;
159 cpu_set_freq[cpu] = 0;
160 sysfs_remove_file (&policy->kobj, &freq_attr_scaling_setspeed.attr);
161 dprintk("managing cpu %u stopped\n", cpu);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800162 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 break;
164 case CPUFREQ_GOV_LIMITS:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800165 mutex_lock(&userspace_mutex);
Thomas Renningerc0672862006-01-27 16:15:26 +0100166 dprintk("limit event for cpu %u: %u - %u kHz,"
167 "currently %u kHz, last set to %u kHz\n",
168 cpu, policy->min, policy->max,
169 cpu_cur_freq[cpu], cpu_set_freq[cpu]);
170 if (policy->max < cpu_set_freq[cpu]) {
171 __cpufreq_driver_target(policy, policy->max,
172 CPUFREQ_RELATION_H);
173 }
174 else if (policy->min > cpu_set_freq[cpu]) {
175 __cpufreq_driver_target(policy, policy->min,
176 CPUFREQ_RELATION_L);
177 }
178 else {
179 __cpufreq_driver_target(policy, cpu_set_freq[cpu],
180 CPUFREQ_RELATION_L);
181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 cpu_min_freq[cpu] = policy->min;
183 cpu_max_freq[cpu] = policy->max;
Thomas Renningerc0672862006-01-27 16:15:26 +0100184 cpu_cur_freq[cpu] = policy->cur;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800185 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 break;
187 }
Jeff Garzik914f7c32006-10-20 14:31:00 -0700188 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
191
192struct cpufreq_governor cpufreq_gov_userspace = {
193 .name = "userspace",
194 .governor = cpufreq_governor_userspace,
195 .owner = THIS_MODULE,
196};
197EXPORT_SYMBOL(cpufreq_gov_userspace);
198
199static int __init cpufreq_gov_userspace_init(void)
200{
201 cpufreq_register_notifier(&userspace_cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
202 return cpufreq_register_governor(&cpufreq_gov_userspace);
203}
204
205
206static void __exit cpufreq_gov_userspace_exit(void)
207{
208 cpufreq_unregister_governor(&cpufreq_gov_userspace);
209 cpufreq_unregister_notifier(&userspace_cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
210}
211
212
213MODULE_AUTHOR ("Dominik Brodowski <linux@brodo.de>, Russell King <rmk@arm.linux.org.uk>");
214MODULE_DESCRIPTION ("CPUfreq policy governor 'userspace'");
215MODULE_LICENSE ("GPL");
216
217fs_initcall(cpufreq_gov_userspace_init);
218module_exit(cpufreq_gov_userspace_exit);