blob: 1442bbada05303bae02adcd3026f57aed4d55a4d [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 */
Mike Travisb38868a2008-07-18 18:11:32 -070033static DEFINE_PER_CPU(unsigned int, cpu_max_freq);
34static DEFINE_PER_CPU(unsigned int, cpu_min_freq);
35static DEFINE_PER_CPU(unsigned int, cpu_cur_freq); /* current CPU freq */
36static DEFINE_PER_CPU(unsigned int, cpu_set_freq); /* CPU freq desired by
37 userspace */
38static DEFINE_PER_CPU(unsigned int, cpu_is_managed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080040static DEFINE_MUTEX (userspace_mutex);
Venki Pallipadic7f652e2007-06-20 14:24:00 -070041static int cpus_using_userspace_governor;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Mike Travisb38868a2008-07-18 18:11:32 -070043#define dprintk(msg...) \
44 cpufreq_debug_printk(CPUFREQ_DEBUG_GOVERNOR, "userspace", msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46/* keep track of frequency transitions */
Dave Jones32ee8c32006-02-28 00:43:23 -050047static int
Linus Torvalds1da177e2005-04-16 15:20:36 -070048userspace_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
49 void *data)
50{
51 struct cpufreq_freqs *freq = data;
52
Mike Travisb38868a2008-07-18 18:11:32 -070053 if (!per_cpu(cpu_is_managed, freq->cpu))
Venki Pallipadic7f652e2007-06-20 14:24:00 -070054 return 0;
55
56 dprintk("saving cpu_cur_freq of cpu %u to be %u kHz\n",
57 freq->cpu, freq->new);
Mike Travisb38868a2008-07-18 18:11:32 -070058 per_cpu(cpu_cur_freq, freq->cpu) = freq->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 return 0;
61}
62
63static struct notifier_block userspace_cpufreq_notifier_block = {
64 .notifier_call = userspace_cpufreq_notifier
65};
66
67
Dave Jones32ee8c32006-02-28 00:43:23 -050068/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 * cpufreq_set - set the CPU frequency
Venki Pallipadi9e769882007-10-26 10:18:21 -070070 * @policy: pointer to policy struct where freq is being set
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 * @freq: target frequency in kHz
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 *
73 * Sets the CPU frequency to freq.
74 */
Venki Pallipadi9e769882007-10-26 10:18:21 -070075static int cpufreq_set(struct cpufreq_policy *policy, unsigned int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 int ret = -EINVAL;
78
Thomas Renningerc0672862006-01-27 16:15:26 +010079 dprintk("cpufreq_set for cpu %u, freq %u kHz\n", policy->cpu, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080081 mutex_lock(&userspace_mutex);
Mike Travisb38868a2008-07-18 18:11:32 -070082 if (!per_cpu(cpu_is_managed, policy->cpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 goto err;
84
Mike Travisb38868a2008-07-18 18:11:32 -070085 per_cpu(cpu_set_freq, policy->cpu) = freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Mike Travisb38868a2008-07-18 18:11:32 -070087 if (freq < per_cpu(cpu_min_freq, policy->cpu))
88 freq = per_cpu(cpu_min_freq, policy->cpu);
89 if (freq > per_cpu(cpu_max_freq, policy->cpu))
90 freq = per_cpu(cpu_max_freq, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 /*
93 * We're safe from concurrent calls to ->target() here
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080094 * as we hold the userspace_mutex lock. If we were calling
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 * cpufreq_driver_target, a deadlock situation might occur:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080096 * A: cpufreq_set (lock userspace_mutex) -> cpufreq_driver_target(lock policy->lock)
97 * B: cpufreq_set_policy(lock policy->lock) -> __cpufreq_governor -> cpufreq_governor_userspace (lock userspace_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 */
Thomas Renningerc0672862006-01-27 16:15:26 +010099 ret = __cpufreq_driver_target(policy, freq, CPUFREQ_RELATION_L);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 err:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800102 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return ret;
104}
105
106
Venki Pallipadi9e769882007-10-26 10:18:21 -0700107static ssize_t show_speed(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Mike Travisb38868a2008-07-18 18:11:32 -0700109 return sprintf(buf, "%u\n", per_cpu(cpu_cur_freq, policy->cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static int cpufreq_governor_userspace(struct cpufreq_policy *policy,
113 unsigned int event)
114{
115 unsigned int cpu = policy->cpu;
Jeff Garzik914f7c32006-10-20 14:31:00 -0700116 int rc = 0;
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 switch (event) {
119 case CPUFREQ_GOV_START:
120 if (!cpu_online(cpu))
121 return -EINVAL;
122 BUG_ON(!policy->cur);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800123 mutex_lock(&userspace_mutex);
Jeff Garzik914f7c32006-10-20 14:31:00 -0700124
Venki Pallipadic7f652e2007-06-20 14:24:00 -0700125 if (cpus_using_userspace_governor == 0) {
126 cpufreq_register_notifier(
127 &userspace_cpufreq_notifier_block,
128 CPUFREQ_TRANSITION_NOTIFIER);
129 }
130 cpus_using_userspace_governor++;
131
Mike Travisb38868a2008-07-18 18:11:32 -0700132 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));
Venki Pallipadi9e769882007-10-26 10:18:21 -0700143
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);
Venki Pallipadic7f652e2007-06-20 14:24:00 -0700148 cpus_using_userspace_governor--;
149 if (cpus_using_userspace_governor == 0) {
150 cpufreq_unregister_notifier(
151 &userspace_cpufreq_notifier_block,
152 CPUFREQ_TRANSITION_NOTIFIER);
153 }
154
Mike Travisb38868a2008-07-18 18:11:32 -0700155 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 dprintk("managing cpu %u stopped\n", cpu);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800160 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 break;
162 case CPUFREQ_GOV_LIMITS:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800163 mutex_lock(&userspace_mutex);
Mike Travisb38868a2008-07-18 18:11:32 -0700164 dprintk("limit event for cpu %u: %u - %u kHz, "
Thomas Renningerc0672862006-01-27 16:15:26 +0100165 "currently %u kHz, last set to %u kHz\n",
166 cpu, policy->min, policy->max,
Mike Travisb38868a2008-07-18 18:11:32 -0700167 per_cpu(cpu_cur_freq, cpu),
168 per_cpu(cpu_set_freq, cpu));
169 if (policy->max < per_cpu(cpu_set_freq, cpu)) {
Thomas Renningerc0672862006-01-27 16:15:26 +0100170 __cpufreq_driver_target(policy, policy->max,
171 CPUFREQ_RELATION_H);
Mike Travisb38868a2008-07-18 18:11:32 -0700172 } else if (policy->min > per_cpu(cpu_set_freq, cpu)) {
Thomas Renningerc0672862006-01-27 16:15:26 +0100173 __cpufreq_driver_target(policy, policy->min,
174 CPUFREQ_RELATION_L);
Mike Travisb38868a2008-07-18 18:11:32 -0700175 } else {
176 __cpufreq_driver_target(policy,
177 per_cpu(cpu_set_freq, cpu),
Thomas Renningerc0672862006-01-27 16:15:26 +0100178 CPUFREQ_RELATION_L);
179 }
Mike Travisb38868a2008-07-18 18:11:32 -0700180 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;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800183 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 break;
185 }
Jeff Garzik914f7c32006-10-20 14:31:00 -0700186 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
189
Sven Wegenerc4d14bc2008-09-20 16:50:08 +0200190#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
191static
192#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193struct cpufreq_governor cpufreq_gov_userspace = {
194 .name = "userspace",
195 .governor = cpufreq_governor_userspace,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700196 .store_setspeed = cpufreq_set,
197 .show_setspeed = show_speed,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 .owner = THIS_MODULE,
199};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201static int __init cpufreq_gov_userspace_init(void)
202{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return cpufreq_register_governor(&cpufreq_gov_userspace);
204}
205
206
207static void __exit cpufreq_gov_userspace_exit(void)
208{
209 cpufreq_unregister_governor(&cpufreq_gov_userspace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
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
Johannes Weiner69157192008-01-17 15:21:08 -0800217#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218fs_initcall(cpufreq_gov_userspace_init);
Johannes Weiner69157192008-01-17 15:21:08 -0800219#else
220module_init(cpufreq_gov_userspace_init);
221#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222module_exit(cpufreq_gov_userspace_exit);