Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 1 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #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 Ven | 153d7f3 | 2006-07-26 15:40:07 +0200 | [diff] [blame] | 21 | #include <linux/cpu.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/types.h> |
| 23 | #include <linux/fs.h> |
| 24 | #include <linux/sysfs.h> |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 25 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | /** |
| 28 | * A few values needed by the userspace governor |
| 29 | */ |
Mike Travis | b38868a | 2008-07-18 18:11:32 -0700 | [diff] [blame] | 30 | static DEFINE_PER_CPU(unsigned int, cpu_max_freq); |
| 31 | static DEFINE_PER_CPU(unsigned int, cpu_min_freq); |
| 32 | static DEFINE_PER_CPU(unsigned int, cpu_cur_freq); /* current CPU freq */ |
| 33 | static DEFINE_PER_CPU(unsigned int, cpu_set_freq); /* CPU freq desired by |
| 34 | userspace */ |
| 35 | static DEFINE_PER_CPU(unsigned int, cpu_is_managed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Dave Jones | 1bceb8d | 2009-01-18 01:51:46 -0500 | [diff] [blame] | 37 | static DEFINE_MUTEX(userspace_mutex); |
Venki Pallipadi | c7f652e | 2007-06-20 14:24:00 -0700 | [diff] [blame] | 38 | static int cpus_using_userspace_governor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | /* keep track of frequency transitions */ |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 41 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | userspace_cpufreq_notifier(struct notifier_block *nb, unsigned long val, |
Dave Jones | 1bceb8d | 2009-01-18 01:51:46 -0500 | [diff] [blame] | 43 | void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | { |
Dave Jones | 1bceb8d | 2009-01-18 01:51:46 -0500 | [diff] [blame] | 45 | struct cpufreq_freqs *freq = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Mike Travis | b38868a | 2008-07-18 18:11:32 -0700 | [diff] [blame] | 47 | if (!per_cpu(cpu_is_managed, freq->cpu)) |
Venki Pallipadi | c7f652e | 2007-06-20 14:24:00 -0700 | [diff] [blame] | 48 | return 0; |
| 49 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 50 | pr_debug("saving cpu_cur_freq of cpu %u to be %u kHz\n", |
Venki Pallipadi | c7f652e | 2007-06-20 14:24:00 -0700 | [diff] [blame] | 51 | freq->cpu, freq->new); |
Mike Travis | b38868a | 2008-07-18 18:11:32 -0700 | [diff] [blame] | 52 | per_cpu(cpu_cur_freq, freq->cpu) = freq->new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Dave Jones | 1bceb8d | 2009-01-18 01:51:46 -0500 | [diff] [blame] | 54 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static struct notifier_block userspace_cpufreq_notifier_block = { |
Dave Jones | 1bceb8d | 2009-01-18 01:51:46 -0500 | [diff] [blame] | 58 | .notifier_call = userspace_cpufreq_notifier |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 62 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | * cpufreq_set - set the CPU frequency |
Venki Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 64 | * @policy: pointer to policy struct where freq is being set |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | * @freq: target frequency in kHz |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | * |
| 67 | * Sets the CPU frequency to freq. |
| 68 | */ |
Venki Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 69 | static int cpufreq_set(struct cpufreq_policy *policy, unsigned int freq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | { |
| 71 | int ret = -EINVAL; |
| 72 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 73 | pr_debug("cpufreq_set for cpu %u, freq %u kHz\n", policy->cpu, freq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 75 | mutex_lock(&userspace_mutex); |
Mike Travis | b38868a | 2008-07-18 18:11:32 -0700 | [diff] [blame] | 76 | if (!per_cpu(cpu_is_managed, policy->cpu)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | goto err; |
| 78 | |
Mike Travis | b38868a | 2008-07-18 18:11:32 -0700 | [diff] [blame] | 79 | per_cpu(cpu_set_freq, policy->cpu) = freq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Mike Travis | b38868a | 2008-07-18 18:11:32 -0700 | [diff] [blame] | 81 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
| 86 | /* |
| 87 | * We're safe from concurrent calls to ->target() here |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 88 | * as we hold the userspace_mutex lock. If we were calling |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | * cpufreq_driver_target, a deadlock situation might occur: |
Dave Jones | 1bceb8d | 2009-01-18 01:51:46 -0500 | [diff] [blame] | 90 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | */ |
Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 96 | ret = __cpufreq_driver_target(policy, freq, CPUFREQ_RELATION_L); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
| 98 | err: |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 99 | mutex_unlock(&userspace_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | return ret; |
| 101 | } |
| 102 | |
| 103 | |
Venki Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 104 | static ssize_t show_speed(struct cpufreq_policy *policy, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | { |
Mike Travis | b38868a | 2008-07-18 18:11:32 -0700 | [diff] [blame] | 106 | return sprintf(buf, "%u\n", per_cpu(cpu_cur_freq, policy->cpu)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | static int cpufreq_governor_userspace(struct cpufreq_policy *policy, |
| 110 | unsigned int event) |
| 111 | { |
| 112 | unsigned int cpu = policy->cpu; |
Jeff Garzik | 914f7c3 | 2006-10-20 14:31:00 -0700 | [diff] [blame] | 113 | int rc = 0; |
| 114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | switch (event) { |
| 116 | case CPUFREQ_GOV_START: |
| 117 | if (!cpu_online(cpu)) |
| 118 | return -EINVAL; |
| 119 | BUG_ON(!policy->cur); |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 120 | mutex_lock(&userspace_mutex); |
Jeff Garzik | 914f7c3 | 2006-10-20 14:31:00 -0700 | [diff] [blame] | 121 | |
Venki Pallipadi | c7f652e | 2007-06-20 14:24:00 -0700 | [diff] [blame] | 122 | 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 Travis | b38868a | 2008-07-18 18:11:32 -0700 | [diff] [blame] | 129 | 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 Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 134 | pr_debug("managing cpu %u started " |
Mike Travis | b38868a | 2008-07-18 18:11:32 -0700 | [diff] [blame] | 135 | "(%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 Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 140 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 141 | mutex_unlock(&userspace_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | break; |
| 143 | case CPUFREQ_GOV_STOP: |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 144 | mutex_lock(&userspace_mutex); |
Venki Pallipadi | c7f652e | 2007-06-20 14:24:00 -0700 | [diff] [blame] | 145 | 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 Travis | b38868a | 2008-07-18 18:11:32 -0700 | [diff] [blame] | 152 | 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 Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 156 | pr_debug("managing cpu %u stopped\n", cpu); |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 157 | mutex_unlock(&userspace_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | break; |
| 159 | case CPUFREQ_GOV_LIMITS: |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 160 | mutex_lock(&userspace_mutex); |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 161 | pr_debug("limit event for cpu %u: %u - %u kHz, " |
Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 162 | "currently %u kHz, last set to %u kHz\n", |
| 163 | cpu, policy->min, policy->max, |
Mike Travis | b38868a | 2008-07-18 18:11:32 -0700 | [diff] [blame] | 164 | per_cpu(cpu_cur_freq, cpu), |
| 165 | per_cpu(cpu_set_freq, cpu)); |
| 166 | if (policy->max < per_cpu(cpu_set_freq, cpu)) { |
Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 167 | __cpufreq_driver_target(policy, policy->max, |
| 168 | CPUFREQ_RELATION_H); |
Mike Travis | b38868a | 2008-07-18 18:11:32 -0700 | [diff] [blame] | 169 | } else if (policy->min > per_cpu(cpu_set_freq, cpu)) { |
Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 170 | __cpufreq_driver_target(policy, policy->min, |
| 171 | CPUFREQ_RELATION_L); |
Mike Travis | b38868a | 2008-07-18 18:11:32 -0700 | [diff] [blame] | 172 | } else { |
| 173 | __cpufreq_driver_target(policy, |
| 174 | per_cpu(cpu_set_freq, cpu), |
Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 175 | CPUFREQ_RELATION_L); |
| 176 | } |
Mike Travis | b38868a | 2008-07-18 18:11:32 -0700 | [diff] [blame] | 177 | 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.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 180 | mutex_unlock(&userspace_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | break; |
| 182 | } |
Jeff Garzik | 914f7c3 | 2006-10-20 14:31:00 -0700 | [diff] [blame] | 183 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | |
Sven Wegener | c4d14bc | 2008-09-20 16:50:08 +0200 | [diff] [blame] | 187 | #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE |
| 188 | static |
| 189 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | struct cpufreq_governor cpufreq_gov_userspace = { |
| 191 | .name = "userspace", |
| 192 | .governor = cpufreq_governor_userspace, |
Venki Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 193 | .store_setspeed = cpufreq_set, |
| 194 | .show_setspeed = show_speed, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | .owner = THIS_MODULE, |
| 196 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
| 198 | static int __init cpufreq_gov_userspace_init(void) |
| 199 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | return cpufreq_register_governor(&cpufreq_gov_userspace); |
| 201 | } |
| 202 | |
| 203 | |
| 204 | static void __exit cpufreq_gov_userspace_exit(void) |
| 205 | { |
| 206 | cpufreq_unregister_governor(&cpufreq_gov_userspace); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | |
Dave Jones | 1bceb8d | 2009-01-18 01:51:46 -0500 | [diff] [blame] | 210 | MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>, " |
| 211 | "Russell King <rmk@arm.linux.org.uk>"); |
| 212 | MODULE_DESCRIPTION("CPUfreq policy governor 'userspace'"); |
| 213 | MODULE_LICENSE("GPL"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
Johannes Weiner | 6915719 | 2008-01-17 15:21:08 -0800 | [diff] [blame] | 215 | #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | fs_initcall(cpufreq_gov_userspace_init); |
Johannes Weiner | 6915719 | 2008-01-17 15:21:08 -0800 | [diff] [blame] | 217 | #else |
| 218 | module_init(cpufreq_gov_userspace_init); |
| 219 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | module_exit(cpufreq_gov_userspace_exit); |