blob: 44ae5e5b94cf4a30f4d1ab18287f72ec28caadb1 [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>
21#include <linux/types.h>
22#include <linux/fs.h>
23#include <linux/sysfs.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080024#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <asm/uaccess.h>
27
28
29/**
30 * A few values needed by the userspace governor
31 */
32static unsigned int cpu_max_freq[NR_CPUS];
33static unsigned int cpu_min_freq[NR_CPUS];
34static unsigned int cpu_cur_freq[NR_CPUS]; /* current CPU freq */
35static unsigned int cpu_set_freq[NR_CPUS]; /* CPU freq desired by userspace */
36static unsigned int cpu_is_managed[NR_CPUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080038static DEFINE_MUTEX (userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_GOVERNOR, "userspace", msg)
41
42/* keep track of frequency transitions */
Dave Jones32ee8c32006-02-28 00:43:23 -050043static int
Linus Torvalds1da177e2005-04-16 15:20:36 -070044userspace_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
45 void *data)
46{
47 struct cpufreq_freqs *freq = data;
48
49 dprintk("saving cpu_cur_freq of cpu %u to be %u kHz\n", freq->cpu, freq->new);
50 cpu_cur_freq[freq->cpu] = freq->new;
51
52 return 0;
53}
54
55static struct notifier_block userspace_cpufreq_notifier_block = {
56 .notifier_call = userspace_cpufreq_notifier
57};
58
59
Dave Jones32ee8c32006-02-28 00:43:23 -050060/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 * cpufreq_set - set the CPU frequency
62 * @freq: target frequency in kHz
63 * @cpu: CPU for which the frequency is to be set
64 *
65 * Sets the CPU frequency to freq.
66 */
Thomas Renningerc0672862006-01-27 16:15:26 +010067static int cpufreq_set(unsigned int freq, struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 int ret = -EINVAL;
70
Thomas Renningerc0672862006-01-27 16:15:26 +010071 dprintk("cpufreq_set for cpu %u, freq %u kHz\n", policy->cpu, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080073 mutex_lock(&userspace_mutex);
Thomas Renningerc0672862006-01-27 16:15:26 +010074 if (!cpu_is_managed[policy->cpu])
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 goto err;
76
Thomas Renningerc0672862006-01-27 16:15:26 +010077 cpu_set_freq[policy->cpu] = freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Thomas Renningerc0672862006-01-27 16:15:26 +010079 if (freq < cpu_min_freq[policy->cpu])
80 freq = cpu_min_freq[policy->cpu];
81 if (freq > cpu_max_freq[policy->cpu])
82 freq = cpu_max_freq[policy->cpu];
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 /*
85 * We're safe from concurrent calls to ->target() here
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080086 * as we hold the userspace_mutex lock. If we were calling
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * cpufreq_driver_target, a deadlock situation might occur:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080088 * A: cpufreq_set (lock userspace_mutex) -> cpufreq_driver_target(lock policy->lock)
89 * B: cpufreq_set_policy(lock policy->lock) -> __cpufreq_governor -> cpufreq_governor_userspace (lock userspace_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 */
Thomas Renningerc0672862006-01-27 16:15:26 +010091 ret = __cpufreq_driver_target(policy, freq, CPUFREQ_RELATION_L);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 err:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080094 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return ret;
96}
97
98
99/************************** sysfs interface ************************/
100static ssize_t show_speed (struct cpufreq_policy *policy, char *buf)
101{
102 return sprintf (buf, "%u\n", cpu_cur_freq[policy->cpu]);
103}
104
Dave Jones32ee8c32006-02-28 00:43:23 -0500105static ssize_t
106store_speed (struct cpufreq_policy *policy, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 unsigned int freq = 0;
109 unsigned int ret;
110
111 ret = sscanf (buf, "%u", &freq);
112 if (ret != 1)
113 return -EINVAL;
114
Thomas Renningerc0672862006-01-27 16:15:26 +0100115 cpufreq_set(freq, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 return count;
118}
119
Dave Jones32ee8c32006-02-28 00:43:23 -0500120static struct freq_attr freq_attr_scaling_setspeed =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 .attr = { .name = "scaling_setspeed", .mode = 0644, .owner = THIS_MODULE },
123 .show = show_speed,
124 .store = store_speed,
125};
126
127static int cpufreq_governor_userspace(struct cpufreq_policy *policy,
128 unsigned int event)
129{
130 unsigned int cpu = policy->cpu;
131 switch (event) {
132 case CPUFREQ_GOV_START:
133 if (!cpu_online(cpu))
134 return -EINVAL;
135 BUG_ON(!policy->cur);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800136 mutex_lock(&userspace_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -0500137 cpu_is_managed[cpu] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 cpu_min_freq[cpu] = policy->min;
139 cpu_max_freq[cpu] = policy->max;
140 cpu_cur_freq[cpu] = policy->cur;
141 cpu_set_freq[cpu] = policy->cur;
142 sysfs_create_file (&policy->kobj, &freq_attr_scaling_setspeed.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 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]);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800144 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 break;
146 case CPUFREQ_GOV_STOP:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800147 mutex_lock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 cpu_is_managed[cpu] = 0;
149 cpu_min_freq[cpu] = 0;
150 cpu_max_freq[cpu] = 0;
151 cpu_set_freq[cpu] = 0;
152 sysfs_remove_file (&policy->kobj, &freq_attr_scaling_setspeed.attr);
153 dprintk("managing cpu %u stopped\n", cpu);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800154 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 break;
156 case CPUFREQ_GOV_LIMITS:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800157 mutex_lock(&userspace_mutex);
Thomas Renningerc0672862006-01-27 16:15:26 +0100158 dprintk("limit event for cpu %u: %u - %u kHz,"
159 "currently %u kHz, last set to %u kHz\n",
160 cpu, policy->min, policy->max,
161 cpu_cur_freq[cpu], cpu_set_freq[cpu]);
162 if (policy->max < cpu_set_freq[cpu]) {
163 __cpufreq_driver_target(policy, policy->max,
164 CPUFREQ_RELATION_H);
165 }
166 else if (policy->min > cpu_set_freq[cpu]) {
167 __cpufreq_driver_target(policy, policy->min,
168 CPUFREQ_RELATION_L);
169 }
170 else {
171 __cpufreq_driver_target(policy, cpu_set_freq[cpu],
172 CPUFREQ_RELATION_L);
173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 cpu_min_freq[cpu] = policy->min;
175 cpu_max_freq[cpu] = policy->max;
Thomas Renningerc0672862006-01-27 16:15:26 +0100176 cpu_cur_freq[cpu] = policy->cur;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800177 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 break;
179 }
180 return 0;
181}
182
183
184struct cpufreq_governor cpufreq_gov_userspace = {
185 .name = "userspace",
186 .governor = cpufreq_governor_userspace,
187 .owner = THIS_MODULE,
188};
189EXPORT_SYMBOL(cpufreq_gov_userspace);
190
191static int __init cpufreq_gov_userspace_init(void)
192{
193 cpufreq_register_notifier(&userspace_cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
194 return cpufreq_register_governor(&cpufreq_gov_userspace);
195}
196
197
198static void __exit cpufreq_gov_userspace_exit(void)
199{
200 cpufreq_unregister_governor(&cpufreq_gov_userspace);
201 cpufreq_unregister_notifier(&userspace_cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
202}
203
204
205MODULE_AUTHOR ("Dominik Brodowski <linux@brodo.de>, Russell King <rmk@arm.linux.org.uk>");
206MODULE_DESCRIPTION ("CPUfreq policy governor 'userspace'");
207MODULE_LICENSE ("GPL");
208
209fs_initcall(cpufreq_gov_userspace_init);
210module_exit(cpufreq_gov_userspace_exit);