blob: bbeb9c0720a665ead5c76462d2f8882461f48e95 [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
Viresh Kumardb701152012-10-23 01:29:03 +020014#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/smp.h>
19#include <linux/init.h>
20#include <linux/spinlock.h>
21#include <linux/interrupt.h>
22#include <linux/cpufreq.h>
Arjan van de Ven153d7f32006-07-26 15:40:07 +020023#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/types.h>
25#include <linux/fs.h>
26#include <linux/sysfs.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080027#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/**
30 * A few values needed by the userspace governor
31 */
Mike Travisb38868a2008-07-18 18:11:32 -070032static DEFINE_PER_CPU(unsigned int, cpu_max_freq);
33static DEFINE_PER_CPU(unsigned int, cpu_min_freq);
34static DEFINE_PER_CPU(unsigned int, cpu_cur_freq); /* current CPU freq */
35static DEFINE_PER_CPU(unsigned int, cpu_set_freq); /* CPU freq desired by
36 userspace */
37static DEFINE_PER_CPU(unsigned int, cpu_is_managed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Dave Jones1bceb8d2009-01-18 01:51:46 -050039static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/* 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,
Dave Jones1bceb8d2009-01-18 01:51:46 -050045 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Dave Jones1bceb8d2009-01-18 01:51:46 -050047 struct cpufreq_freqs *freq = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Mike Travisb38868a2008-07-18 18:11:32 -070049 if (!per_cpu(cpu_is_managed, freq->cpu))
Venki Pallipadic7f652e2007-06-20 14:24:00 -070050 return 0;
51
Afzal Mohammed226dd012012-01-04 10:52:31 +053052 if (val == CPUFREQ_POSTCHANGE) {
53 pr_debug("saving cpu_cur_freq of cpu %u to be %u kHz\n",
54 freq->cpu, freq->new);
55 per_cpu(cpu_cur_freq, freq->cpu) = freq->new;
56 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Dave Jones1bceb8d2009-01-18 01:51:46 -050058 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
61static struct notifier_block userspace_cpufreq_notifier_block = {
Dave Jones1bceb8d2009-01-18 01:51:46 -050062 .notifier_call = userspace_cpufreq_notifier
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
65
Dave Jones32ee8c32006-02-28 00:43:23 -050066/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 * cpufreq_set - set the CPU frequency
Venki Pallipadi9e769882007-10-26 10:18:21 -070068 * @policy: pointer to policy struct where freq is being set
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 * @freq: target frequency in kHz
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 *
71 * Sets the CPU frequency to freq.
72 */
Venki Pallipadi9e769882007-10-26 10:18:21 -070073static int cpufreq_set(struct cpufreq_policy *policy, unsigned int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 int ret = -EINVAL;
76
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020077 pr_debug("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);
Mike Travisb38868a2008-07-18 18:11:32 -070080 if (!per_cpu(cpu_is_managed, policy->cpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 goto err;
82
Mike Travisb38868a2008-07-18 18:11:32 -070083 per_cpu(cpu_set_freq, policy->cpu) = freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Mike Travisb38868a2008-07-18 18:11:32 -070085 if (freq < per_cpu(cpu_min_freq, policy->cpu))
86 freq = per_cpu(cpu_min_freq, policy->cpu);
87 if (freq > per_cpu(cpu_max_freq, policy->cpu))
88 freq = per_cpu(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:
Dave Jones1bceb8d2009-01-18 01:51:46 -050094 * A: cpufreq_set (lock userspace_mutex) ->
95 * cpufreq_driver_target(lock policy->lock)
96 * B: cpufreq_set_policy(lock policy->lock) ->
97 * __cpufreq_governor ->
98 * cpufreq_governor_userspace (lock userspace_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 */
Thomas Renningerc0672862006-01-27 16:15:26 +0100100 ret = __cpufreq_driver_target(policy, freq, CPUFREQ_RELATION_L);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 err:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800103 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return ret;
105}
106
107
Venki Pallipadi9e769882007-10-26 10:18:21 -0700108static ssize_t show_speed(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Mike Travisb38868a2008-07-18 18:11:32 -0700110 return sprintf(buf, "%u\n", per_cpu(cpu_cur_freq, policy->cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static int cpufreq_governor_userspace(struct cpufreq_policy *policy,
114 unsigned int event)
115{
116 unsigned int cpu = policy->cpu;
Jeff Garzik914f7c32006-10-20 14:31:00 -0700117 int rc = 0;
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 switch (event) {
120 case CPUFREQ_GOV_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 BUG_ON(!policy->cur);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800122 mutex_lock(&userspace_mutex);
Jeff Garzik914f7c32006-10-20 14:31:00 -0700123
Venki Pallipadic7f652e2007-06-20 14:24:00 -0700124 if (cpus_using_userspace_governor == 0) {
125 cpufreq_register_notifier(
126 &userspace_cpufreq_notifier_block,
127 CPUFREQ_TRANSITION_NOTIFIER);
128 }
129 cpus_using_userspace_governor++;
130
Mike Travisb38868a2008-07-18 18:11:32 -0700131 per_cpu(cpu_is_managed, cpu) = 1;
132 per_cpu(cpu_min_freq, cpu) = policy->min;
133 per_cpu(cpu_max_freq, cpu) = policy->max;
134 per_cpu(cpu_cur_freq, cpu) = policy->cur;
135 per_cpu(cpu_set_freq, cpu) = policy->cur;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200136 pr_debug("managing cpu %u started "
Mike Travisb38868a2008-07-18 18:11:32 -0700137 "(%u - %u kHz, currently %u kHz)\n",
138 cpu,
139 per_cpu(cpu_min_freq, cpu),
140 per_cpu(cpu_max_freq, cpu),
141 per_cpu(cpu_cur_freq, cpu));
Venki Pallipadi9e769882007-10-26 10:18:21 -0700142
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800143 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 break;
145 case CPUFREQ_GOV_STOP:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800146 mutex_lock(&userspace_mutex);
Venki Pallipadic7f652e2007-06-20 14:24:00 -0700147 cpus_using_userspace_governor--;
148 if (cpus_using_userspace_governor == 0) {
149 cpufreq_unregister_notifier(
150 &userspace_cpufreq_notifier_block,
151 CPUFREQ_TRANSITION_NOTIFIER);
152 }
153
Mike Travisb38868a2008-07-18 18:11:32 -0700154 per_cpu(cpu_is_managed, cpu) = 0;
155 per_cpu(cpu_min_freq, cpu) = 0;
156 per_cpu(cpu_max_freq, cpu) = 0;
157 per_cpu(cpu_set_freq, cpu) = 0;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200158 pr_debug("managing cpu %u stopped\n", cpu);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800159 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 break;
161 case CPUFREQ_GOV_LIMITS:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800162 mutex_lock(&userspace_mutex);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200163 pr_debug("limit event for cpu %u: %u - %u kHz, "
Thomas Renningerc0672862006-01-27 16:15:26 +0100164 "currently %u kHz, last set to %u kHz\n",
165 cpu, policy->min, policy->max,
Mike Travisb38868a2008-07-18 18:11:32 -0700166 per_cpu(cpu_cur_freq, cpu),
167 per_cpu(cpu_set_freq, cpu));
168 if (policy->max < per_cpu(cpu_set_freq, cpu)) {
Thomas Renningerc0672862006-01-27 16:15:26 +0100169 __cpufreq_driver_target(policy, policy->max,
170 CPUFREQ_RELATION_H);
Mike Travisb38868a2008-07-18 18:11:32 -0700171 } else if (policy->min > per_cpu(cpu_set_freq, cpu)) {
Thomas Renningerc0672862006-01-27 16:15:26 +0100172 __cpufreq_driver_target(policy, policy->min,
173 CPUFREQ_RELATION_L);
Mike Travisb38868a2008-07-18 18:11:32 -0700174 } else {
175 __cpufreq_driver_target(policy,
176 per_cpu(cpu_set_freq, cpu),
Thomas Renningerc0672862006-01-27 16:15:26 +0100177 CPUFREQ_RELATION_L);
178 }
Mike Travisb38868a2008-07-18 18:11:32 -0700179 per_cpu(cpu_min_freq, cpu) = policy->min;
180 per_cpu(cpu_max_freq, cpu) = policy->max;
181 per_cpu(cpu_cur_freq, cpu) = policy->cur;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800182 mutex_unlock(&userspace_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 break;
184 }
Jeff Garzik914f7c32006-10-20 14:31:00 -0700185 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
188
Sven Wegenerc4d14bc2008-09-20 16:50:08 +0200189#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
190static
191#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192struct cpufreq_governor cpufreq_gov_userspace = {
193 .name = "userspace",
194 .governor = cpufreq_governor_userspace,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700195 .store_setspeed = cpufreq_set,
196 .show_setspeed = show_speed,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 .owner = THIS_MODULE,
198};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200static int __init cpufreq_gov_userspace_init(void)
201{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
211
Dave Jones1bceb8d2009-01-18 01:51:46 -0500212MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>, "
213 "Russell King <rmk@arm.linux.org.uk>");
214MODULE_DESCRIPTION("CPUfreq policy governor 'userspace'");
215MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
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);