blob: f231015904c0a6584ed0fb4c57d78de4636746b4 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070027/**
28 * A few values needed by the userspace governor
29 */
Mike Travisb38868a2008-07-18 18:11:32 -070030static DEFINE_PER_CPU(unsigned int, cpu_max_freq);
31static DEFINE_PER_CPU(unsigned int, cpu_min_freq);
32static DEFINE_PER_CPU(unsigned int, cpu_cur_freq); /* current CPU freq */
33static DEFINE_PER_CPU(unsigned int, cpu_set_freq); /* CPU freq desired by
34 userspace */
35static DEFINE_PER_CPU(unsigned int, cpu_is_managed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Dave Jones1bceb8d2009-01-18 01:51:46 -050037static DEFINE_MUTEX(userspace_mutex);
Venki Pallipadic7f652e2007-06-20 14:24:00 -070038static int cpus_using_userspace_governor;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/* keep track of frequency transitions */
Dave Jones32ee8c32006-02-28 00:43:23 -050041static int
Linus Torvalds1da177e2005-04-16 15:20:36 -070042userspace_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
Dave Jones1bceb8d2009-01-18 01:51:46 -050043 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Dave Jones1bceb8d2009-01-18 01:51:46 -050045 struct cpufreq_freqs *freq = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Mike Travisb38868a2008-07-18 18:11:32 -070047 if (!per_cpu(cpu_is_managed, freq->cpu))
Venki Pallipadic7f652e2007-06-20 14:24:00 -070048 return 0;
49
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020050 pr_debug("saving cpu_cur_freq of cpu %u to be %u kHz\n",
Venki Pallipadic7f652e2007-06-20 14:24:00 -070051 freq->cpu, freq->new);
Mike Travisb38868a2008-07-18 18:11:32 -070052 per_cpu(cpu_cur_freq, freq->cpu) = freq->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Dave Jones1bceb8d2009-01-18 01:51:46 -050054 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
57static struct notifier_block userspace_cpufreq_notifier_block = {
Dave Jones1bceb8d2009-01-18 01:51:46 -050058 .notifier_call = userspace_cpufreq_notifier
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
61
Dave Jones32ee8c32006-02-28 00:43:23 -050062/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * cpufreq_set - set the CPU frequency
Venki Pallipadi9e769882007-10-26 10:18:21 -070064 * @policy: pointer to policy struct where freq is being set
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 * @freq: target frequency in kHz
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 *
67 * Sets the CPU frequency to freq.
68 */
Venki Pallipadi9e769882007-10-26 10:18:21 -070069static int cpufreq_set(struct cpufreq_policy *policy, unsigned int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
71 int ret = -EINVAL;
72
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020073 pr_debug("cpufreq_set for cpu %u, freq %u kHz\n", policy->cpu, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080075 mutex_lock(&userspace_mutex);
Mike Travisb38868a2008-07-18 18:11:32 -070076 if (!per_cpu(cpu_is_managed, policy->cpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 goto err;
78
Mike Travisb38868a2008-07-18 18:11:32 -070079 per_cpu(cpu_set_freq, policy->cpu) = freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Mike Travisb38868a2008-07-18 18:11:32 -070081 if (freq < per_cpu(cpu_min_freq, policy->cpu))
82 freq = per_cpu(cpu_min_freq, policy->cpu);
83 if (freq > per_cpu(cpu_max_freq, policy->cpu))
84 freq = per_cpu(cpu_max_freq, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 /*
87 * We're safe from concurrent calls to ->target() here
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080088 * as we hold the userspace_mutex lock. If we were calling
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * cpufreq_driver_target, a deadlock situation might occur:
Dave Jones1bceb8d2009-01-18 01:51:46 -050090 * A: cpufreq_set (lock userspace_mutex) ->
91 * cpufreq_driver_target(lock policy->lock)
92 * B: cpufreq_set_policy(lock policy->lock) ->
93 * __cpufreq_governor ->
94 * cpufreq_governor_userspace (lock userspace_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 */
Thomas Renningerc0672862006-01-27 16:15:26 +010096 ret = __cpufreq_driver_target(policy, freq, CPUFREQ_RELATION_L);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 err:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080099 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return ret;
101}
102
103
Venki Pallipadi9e769882007-10-26 10:18:21 -0700104static ssize_t show_speed(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Mike Travisb38868a2008-07-18 18:11:32 -0700106 return sprintf(buf, "%u\n", per_cpu(cpu_cur_freq, policy->cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static int cpufreq_governor_userspace(struct cpufreq_policy *policy,
110 unsigned int event)
111{
112 unsigned int cpu = policy->cpu;
Jeff Garzik914f7c32006-10-20 14:31:00 -0700113 int rc = 0;
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 switch (event) {
116 case CPUFREQ_GOV_START:
117 if (!cpu_online(cpu))
118 return -EINVAL;
119 BUG_ON(!policy->cur);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800120 mutex_lock(&userspace_mutex);
Jeff Garzik914f7c32006-10-20 14:31:00 -0700121
Venki Pallipadic7f652e2007-06-20 14:24:00 -0700122 if (cpus_using_userspace_governor == 0) {
123 cpufreq_register_notifier(
124 &userspace_cpufreq_notifier_block,
125 CPUFREQ_TRANSITION_NOTIFIER);
126 }
127 cpus_using_userspace_governor++;
128
Mike Travisb38868a2008-07-18 18:11:32 -0700129 per_cpu(cpu_is_managed, cpu) = 1;
130 per_cpu(cpu_min_freq, cpu) = policy->min;
131 per_cpu(cpu_max_freq, cpu) = policy->max;
132 per_cpu(cpu_cur_freq, cpu) = policy->cur;
133 per_cpu(cpu_set_freq, cpu) = policy->cur;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200134 pr_debug("managing cpu %u started "
Mike Travisb38868a2008-07-18 18:11:32 -0700135 "(%u - %u kHz, currently %u kHz)\n",
136 cpu,
137 per_cpu(cpu_min_freq, cpu),
138 per_cpu(cpu_max_freq, cpu),
139 per_cpu(cpu_cur_freq, cpu));
Venki Pallipadi9e769882007-10-26 10:18:21 -0700140
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800141 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 break;
143 case CPUFREQ_GOV_STOP:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800144 mutex_lock(&userspace_mutex);
Venki Pallipadic7f652e2007-06-20 14:24:00 -0700145 cpus_using_userspace_governor--;
146 if (cpus_using_userspace_governor == 0) {
147 cpufreq_unregister_notifier(
148 &userspace_cpufreq_notifier_block,
149 CPUFREQ_TRANSITION_NOTIFIER);
150 }
151
Mike Travisb38868a2008-07-18 18:11:32 -0700152 per_cpu(cpu_is_managed, cpu) = 0;
153 per_cpu(cpu_min_freq, cpu) = 0;
154 per_cpu(cpu_max_freq, cpu) = 0;
155 per_cpu(cpu_set_freq, cpu) = 0;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200156 pr_debug("managing cpu %u stopped\n", cpu);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800157 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 break;
159 case CPUFREQ_GOV_LIMITS:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800160 mutex_lock(&userspace_mutex);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200161 pr_debug("limit event for cpu %u: %u - %u kHz, "
Thomas Renningerc0672862006-01-27 16:15:26 +0100162 "currently %u kHz, last set to %u kHz\n",
163 cpu, policy->min, policy->max,
Mike Travisb38868a2008-07-18 18:11:32 -0700164 per_cpu(cpu_cur_freq, cpu),
165 per_cpu(cpu_set_freq, cpu));
166 if (policy->max < per_cpu(cpu_set_freq, cpu)) {
Thomas Renningerc0672862006-01-27 16:15:26 +0100167 __cpufreq_driver_target(policy, policy->max,
168 CPUFREQ_RELATION_H);
Mike Travisb38868a2008-07-18 18:11:32 -0700169 } else if (policy->min > per_cpu(cpu_set_freq, cpu)) {
Thomas Renningerc0672862006-01-27 16:15:26 +0100170 __cpufreq_driver_target(policy, policy->min,
171 CPUFREQ_RELATION_L);
Mike Travisb38868a2008-07-18 18:11:32 -0700172 } else {
173 __cpufreq_driver_target(policy,
174 per_cpu(cpu_set_freq, cpu),
Thomas Renningerc0672862006-01-27 16:15:26 +0100175 CPUFREQ_RELATION_L);
176 }
Mike Travisb38868a2008-07-18 18:11:32 -0700177 per_cpu(cpu_min_freq, cpu) = policy->min;
178 per_cpu(cpu_max_freq, cpu) = policy->max;
179 per_cpu(cpu_cur_freq, cpu) = policy->cur;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800180 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 break;
182 }
Jeff Garzik914f7c32006-10-20 14:31:00 -0700183 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
186
Sven Wegenerc4d14bc2008-09-20 16:50:08 +0200187#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
188static
189#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190struct cpufreq_governor cpufreq_gov_userspace = {
191 .name = "userspace",
192 .governor = cpufreq_governor_userspace,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700193 .store_setspeed = cpufreq_set,
194 .show_setspeed = show_speed,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 .owner = THIS_MODULE,
196};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198static int __init cpufreq_gov_userspace_init(void)
199{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return cpufreq_register_governor(&cpufreq_gov_userspace);
201}
202
203
204static void __exit cpufreq_gov_userspace_exit(void)
205{
206 cpufreq_unregister_governor(&cpufreq_gov_userspace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
209
Dave Jones1bceb8d2009-01-18 01:51:46 -0500210MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>, "
211 "Russell King <rmk@arm.linux.org.uk>");
212MODULE_DESCRIPTION("CPUfreq policy governor 'userspace'");
213MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Johannes Weiner69157192008-01-17 15:21:08 -0800215#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216fs_initcall(cpufreq_gov_userspace_init);
Johannes Weiner69157192008-01-17 15:21:08 -0800217#else
218module_init(cpufreq_gov_userspace_init);
219#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220module_exit(cpufreq_gov_userspace_exit);