Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/cpufreq/cpufreq.c |
| 3 | * |
| 4 | * Copyright (C) 2001 Russell King |
| 5 | * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de> |
| 6 | * |
Ashok Raj | c32b6b8 | 2005-10-30 14:59:54 -0800 | [diff] [blame] | 7 | * Oct 2005 - Ashok Raj <ashok.raj@intel.com> |
| 8 | * Added handling for CPU hotplug |
| 9 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #include <linux/config.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/notifier.h> |
| 21 | #include <linux/cpufreq.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/spinlock.h> |
| 25 | #include <linux/device.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/cpu.h> |
| 28 | #include <linux/completion.h> |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 29 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, "cpufreq-core", msg) |
| 32 | |
| 33 | /** |
| 34 | * The "cpufreq driver" - the arch- or hardware-dependend low |
| 35 | * level driver of CPUFreq support, and its spinlock. This lock |
| 36 | * also protects the cpufreq_cpu_data array. |
| 37 | */ |
Dave Jones | 7d5e350 | 2006-02-02 17:03:42 -0500 | [diff] [blame] | 38 | static struct cpufreq_driver *cpufreq_driver; |
| 39 | static struct cpufreq_policy *cpufreq_cpu_data[NR_CPUS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | static DEFINE_SPINLOCK(cpufreq_driver_lock); |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | /* internal prototypes */ |
| 43 | static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event); |
| 44 | static void handle_update(void *data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Two notifier lists: the "policy" list is involved in the |
| 48 | * validation process for a new CPU frequency policy; the |
| 49 | * "transition" list for kernel code that needs to handle |
| 50 | * changes to devices when the CPU clock speed changes. |
| 51 | * The mutex locks both lists. |
| 52 | */ |
Dave Jones | 7d5e350 | 2006-02-02 17:03:42 -0500 | [diff] [blame] | 53 | static struct notifier_block *cpufreq_policy_notifier_list; |
| 54 | static struct notifier_block *cpufreq_transition_notifier_list; |
| 55 | static DECLARE_RWSEM (cpufreq_notifier_rwsem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | |
| 58 | static LIST_HEAD(cpufreq_governor_list); |
Dave Jones | 7d5e350 | 2006-02-02 17:03:42 -0500 | [diff] [blame] | 59 | static DEFINE_MUTEX (cpufreq_governor_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Dave Jones | 7d5e350 | 2006-02-02 17:03:42 -0500 | [diff] [blame] | 61 | struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | { |
| 63 | struct cpufreq_policy *data; |
| 64 | unsigned long flags; |
| 65 | |
| 66 | if (cpu >= NR_CPUS) |
| 67 | goto err_out; |
| 68 | |
| 69 | /* get the cpufreq driver */ |
| 70 | spin_lock_irqsave(&cpufreq_driver_lock, flags); |
| 71 | |
| 72 | if (!cpufreq_driver) |
| 73 | goto err_out_unlock; |
| 74 | |
| 75 | if (!try_module_get(cpufreq_driver->owner)) |
| 76 | goto err_out_unlock; |
| 77 | |
| 78 | |
| 79 | /* get the CPU */ |
| 80 | data = cpufreq_cpu_data[cpu]; |
| 81 | |
| 82 | if (!data) |
| 83 | goto err_out_put_module; |
| 84 | |
| 85 | if (!kobject_get(&data->kobj)) |
| 86 | goto err_out_put_module; |
| 87 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | spin_unlock_irqrestore(&cpufreq_driver_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | return data; |
| 90 | |
Dave Jones | 7d5e350 | 2006-02-02 17:03:42 -0500 | [diff] [blame] | 91 | err_out_put_module: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | module_put(cpufreq_driver->owner); |
Dave Jones | 7d5e350 | 2006-02-02 17:03:42 -0500 | [diff] [blame] | 93 | err_out_unlock: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | spin_unlock_irqrestore(&cpufreq_driver_lock, flags); |
Dave Jones | 7d5e350 | 2006-02-02 17:03:42 -0500 | [diff] [blame] | 95 | err_out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | return NULL; |
| 97 | } |
| 98 | EXPORT_SYMBOL_GPL(cpufreq_cpu_get); |
| 99 | |
Dave Jones | 7d5e350 | 2006-02-02 17:03:42 -0500 | [diff] [blame] | 100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | void cpufreq_cpu_put(struct cpufreq_policy *data) |
| 102 | { |
| 103 | kobject_put(&data->kobj); |
| 104 | module_put(cpufreq_driver->owner); |
| 105 | } |
| 106 | EXPORT_SYMBOL_GPL(cpufreq_cpu_put); |
| 107 | |
| 108 | |
| 109 | /********************************************************************* |
| 110 | * UNIFIED DEBUG HELPERS * |
| 111 | *********************************************************************/ |
| 112 | #ifdef CONFIG_CPU_FREQ_DEBUG |
| 113 | |
| 114 | /* what part(s) of the CPUfreq subsystem are debugged? */ |
| 115 | static unsigned int debug; |
| 116 | |
| 117 | /* is the debug output ratelimit'ed using printk_ratelimit? User can |
| 118 | * set or modify this value. |
| 119 | */ |
| 120 | static unsigned int debug_ratelimit = 1; |
| 121 | |
| 122 | /* is the printk_ratelimit'ing enabled? It's enabled after a successful |
| 123 | * loading of a cpufreq driver, temporarily disabled when a new policy |
| 124 | * is set, and disabled upon cpufreq driver removal |
| 125 | */ |
| 126 | static unsigned int disable_ratelimit = 1; |
| 127 | static DEFINE_SPINLOCK(disable_ratelimit_lock); |
| 128 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 129 | static void cpufreq_debug_enable_ratelimit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | { |
| 131 | unsigned long flags; |
| 132 | |
| 133 | spin_lock_irqsave(&disable_ratelimit_lock, flags); |
| 134 | if (disable_ratelimit) |
| 135 | disable_ratelimit--; |
| 136 | spin_unlock_irqrestore(&disable_ratelimit_lock, flags); |
| 137 | } |
| 138 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 139 | static void cpufreq_debug_disable_ratelimit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | { |
| 141 | unsigned long flags; |
| 142 | |
| 143 | spin_lock_irqsave(&disable_ratelimit_lock, flags); |
| 144 | disable_ratelimit++; |
| 145 | spin_unlock_irqrestore(&disable_ratelimit_lock, flags); |
| 146 | } |
| 147 | |
| 148 | void cpufreq_debug_printk(unsigned int type, const char *prefix, const char *fmt, ...) |
| 149 | { |
| 150 | char s[256]; |
| 151 | va_list args; |
| 152 | unsigned int len; |
| 153 | unsigned long flags; |
| 154 | |
| 155 | WARN_ON(!prefix); |
| 156 | if (type & debug) { |
| 157 | spin_lock_irqsave(&disable_ratelimit_lock, flags); |
| 158 | if (!disable_ratelimit && debug_ratelimit && !printk_ratelimit()) { |
| 159 | spin_unlock_irqrestore(&disable_ratelimit_lock, flags); |
| 160 | return; |
| 161 | } |
| 162 | spin_unlock_irqrestore(&disable_ratelimit_lock, flags); |
| 163 | |
| 164 | len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix); |
| 165 | |
| 166 | va_start(args, fmt); |
| 167 | len += vsnprintf(&s[len], (256 - len), fmt, args); |
| 168 | va_end(args); |
| 169 | |
| 170 | printk(s); |
| 171 | |
| 172 | WARN_ON(len < 5); |
| 173 | } |
| 174 | } |
| 175 | EXPORT_SYMBOL(cpufreq_debug_printk); |
| 176 | |
| 177 | |
| 178 | module_param(debug, uint, 0644); |
| 179 | MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core, 2 to debug drivers, and 4 to debug governors."); |
| 180 | |
| 181 | module_param(debug_ratelimit, uint, 0644); |
| 182 | MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging: set to 0 to disable ratelimiting."); |
| 183 | |
| 184 | #else /* !CONFIG_CPU_FREQ_DEBUG */ |
| 185 | |
| 186 | static inline void cpufreq_debug_enable_ratelimit(void) { return; } |
| 187 | static inline void cpufreq_debug_disable_ratelimit(void) { return; } |
| 188 | |
| 189 | #endif /* CONFIG_CPU_FREQ_DEBUG */ |
| 190 | |
| 191 | |
| 192 | /********************************************************************* |
| 193 | * EXTERNALLY AFFECTING FREQUENCY CHANGES * |
| 194 | *********************************************************************/ |
| 195 | |
| 196 | /** |
| 197 | * adjust_jiffies - adjust the system "loops_per_jiffy" |
| 198 | * |
| 199 | * This function alters the system "loops_per_jiffy" for the clock |
| 200 | * speed change. Note that loops_per_jiffy cannot be updated on SMP |
| 201 | * systems as each CPU might be scaled differently. So, use the arch |
| 202 | * per-CPU loops_per_jiffy value wherever possible. |
| 203 | */ |
| 204 | #ifndef CONFIG_SMP |
| 205 | static unsigned long l_p_j_ref; |
| 206 | static unsigned int l_p_j_ref_freq; |
| 207 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 208 | static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | { |
| 210 | if (ci->flags & CPUFREQ_CONST_LOOPS) |
| 211 | return; |
| 212 | |
| 213 | if (!l_p_j_ref_freq) { |
| 214 | l_p_j_ref = loops_per_jiffy; |
| 215 | l_p_j_ref_freq = ci->old; |
| 216 | dprintk("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq); |
| 217 | } |
| 218 | if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) || |
| 219 | (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) || |
Benjamin Herrenschmidt | 42d4dc3 | 2005-04-29 07:40:12 -0700 | [diff] [blame] | 220 | (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, ci->new); |
| 222 | dprintk("scaling loops_per_jiffy to %lu for frequency %u kHz\n", loops_per_jiffy, ci->new); |
| 223 | } |
| 224 | } |
| 225 | #else |
| 226 | static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) { return; } |
| 227 | #endif |
| 228 | |
| 229 | |
| 230 | /** |
Dave Jones | e4472cb | 2006-01-31 15:53:55 -0800 | [diff] [blame] | 231 | * cpufreq_notify_transition - call notifier chain and adjust_jiffies |
| 232 | * on frequency transition. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | * |
Dave Jones | e4472cb | 2006-01-31 15:53:55 -0800 | [diff] [blame] | 234 | * This function calls the transition notifiers and the "adjust_jiffies" |
| 235 | * function. It is called twice on all CPU frequency changes that have |
| 236 | * external effects. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | */ |
| 238 | void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state) |
| 239 | { |
Dave Jones | e4472cb | 2006-01-31 15:53:55 -0800 | [diff] [blame] | 240 | struct cpufreq_policy *policy; |
| 241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | BUG_ON(irqs_disabled()); |
| 243 | |
| 244 | freqs->flags = cpufreq_driver->flags; |
Dave Jones | e4472cb | 2006-01-31 15:53:55 -0800 | [diff] [blame] | 245 | dprintk("notification %u of frequency transition to %u kHz\n", |
| 246 | state, freqs->new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
| 248 | down_read(&cpufreq_notifier_rwsem); |
Dave Jones | e4472cb | 2006-01-31 15:53:55 -0800 | [diff] [blame] | 249 | |
| 250 | policy = cpufreq_cpu_data[freqs->cpu]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | switch (state) { |
Dave Jones | e4472cb | 2006-01-31 15:53:55 -0800 | [diff] [blame] | 252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | case CPUFREQ_PRECHANGE: |
Dave Jones | e4472cb | 2006-01-31 15:53:55 -0800 | [diff] [blame] | 254 | /* detect if the driver reported a value as "old frequency" |
| 255 | * which is not equal to what the cpufreq core thinks is |
| 256 | * "old frequency". |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | */ |
| 258 | if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { |
Dave Jones | e4472cb | 2006-01-31 15:53:55 -0800 | [diff] [blame] | 259 | if ((policy) && (policy->cpu == freqs->cpu) && |
| 260 | (policy->cur) && (policy->cur != freqs->old)) { |
| 261 | dprintk(KERN_WARNING "Warning: CPU frequency is" |
| 262 | " %u, cpufreq assumed %u kHz.\n", |
| 263 | freqs->old, policy->cur); |
| 264 | freqs->old = policy->cur; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | } |
| 266 | } |
Dave Jones | e4472cb | 2006-01-31 15:53:55 -0800 | [diff] [blame] | 267 | notifier_call_chain(&cpufreq_transition_notifier_list, |
| 268 | CPUFREQ_PRECHANGE, freqs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | adjust_jiffies(CPUFREQ_PRECHANGE, freqs); |
| 270 | break; |
Dave Jones | e4472cb | 2006-01-31 15:53:55 -0800 | [diff] [blame] | 271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | case CPUFREQ_POSTCHANGE: |
| 273 | adjust_jiffies(CPUFREQ_POSTCHANGE, freqs); |
Dave Jones | e4472cb | 2006-01-31 15:53:55 -0800 | [diff] [blame] | 274 | notifier_call_chain(&cpufreq_transition_notifier_list, |
| 275 | CPUFREQ_POSTCHANGE, freqs); |
| 276 | if (likely(policy) && likely(policy->cpu == freqs->cpu)) |
| 277 | policy->cur = freqs->new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | break; |
| 279 | } |
| 280 | up_read(&cpufreq_notifier_rwsem); |
| 281 | } |
| 282 | EXPORT_SYMBOL_GPL(cpufreq_notify_transition); |
| 283 | |
| 284 | |
| 285 | |
| 286 | /********************************************************************* |
| 287 | * SYSFS INTERFACE * |
| 288 | *********************************************************************/ |
| 289 | |
| 290 | /** |
| 291 | * cpufreq_parse_governor - parse a governor string |
| 292 | */ |
| 293 | static int cpufreq_parse_governor (char *str_governor, unsigned int *policy, |
| 294 | struct cpufreq_governor **governor) |
| 295 | { |
| 296 | if (!cpufreq_driver) |
| 297 | return -EINVAL; |
| 298 | if (cpufreq_driver->setpolicy) { |
| 299 | if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) { |
| 300 | *policy = CPUFREQ_POLICY_PERFORMANCE; |
| 301 | return 0; |
| 302 | } else if (!strnicmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) { |
| 303 | *policy = CPUFREQ_POLICY_POWERSAVE; |
| 304 | return 0; |
| 305 | } |
| 306 | return -EINVAL; |
| 307 | } else { |
| 308 | struct cpufreq_governor *t; |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 309 | mutex_lock(&cpufreq_governor_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | if (!cpufreq_driver || !cpufreq_driver->target) |
| 311 | goto out; |
| 312 | list_for_each_entry(t, &cpufreq_governor_list, governor_list) { |
| 313 | if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN)) { |
| 314 | *governor = t; |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 315 | mutex_unlock(&cpufreq_governor_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | return 0; |
| 317 | } |
| 318 | } |
Dave Jones | 7d5e350 | 2006-02-02 17:03:42 -0500 | [diff] [blame] | 319 | out: |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 320 | mutex_unlock(&cpufreq_governor_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | } |
| 322 | return -EINVAL; |
| 323 | } |
| 324 | EXPORT_SYMBOL_GPL(cpufreq_parse_governor); |
| 325 | |
| 326 | |
| 327 | /* drivers/base/cpu.c */ |
| 328 | extern struct sysdev_class cpu_sysdev_class; |
| 329 | |
| 330 | |
| 331 | /** |
| 332 | * cpufreq_per_cpu_attr_read() / show_##file_name() - print out cpufreq information |
| 333 | * |
| 334 | * Write out information from cpufreq_driver->policy[cpu]; object must be |
| 335 | * "unsigned int". |
| 336 | */ |
| 337 | |
| 338 | #define show_one(file_name, object) \ |
| 339 | static ssize_t show_##file_name \ |
| 340 | (struct cpufreq_policy * policy, char *buf) \ |
| 341 | { \ |
| 342 | return sprintf (buf, "%u\n", policy->object); \ |
| 343 | } |
| 344 | |
| 345 | show_one(cpuinfo_min_freq, cpuinfo.min_freq); |
| 346 | show_one(cpuinfo_max_freq, cpuinfo.max_freq); |
| 347 | show_one(scaling_min_freq, min); |
| 348 | show_one(scaling_max_freq, max); |
| 349 | show_one(scaling_cur_freq, cur); |
| 350 | |
| 351 | /** |
| 352 | * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access |
| 353 | */ |
| 354 | #define store_one(file_name, object) \ |
| 355 | static ssize_t store_##file_name \ |
| 356 | (struct cpufreq_policy * policy, const char *buf, size_t count) \ |
| 357 | { \ |
| 358 | unsigned int ret = -EINVAL; \ |
| 359 | struct cpufreq_policy new_policy; \ |
| 360 | \ |
| 361 | ret = cpufreq_get_policy(&new_policy, policy->cpu); \ |
| 362 | if (ret) \ |
| 363 | return -EINVAL; \ |
| 364 | \ |
| 365 | ret = sscanf (buf, "%u", &new_policy.object); \ |
| 366 | if (ret != 1) \ |
| 367 | return -EINVAL; \ |
| 368 | \ |
| 369 | ret = cpufreq_set_policy(&new_policy); \ |
| 370 | \ |
| 371 | return ret ? ret : count; \ |
| 372 | } |
| 373 | |
| 374 | store_one(scaling_min_freq,min); |
| 375 | store_one(scaling_max_freq,max); |
| 376 | |
| 377 | /** |
| 378 | * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware |
| 379 | */ |
| 380 | static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, char *buf) |
| 381 | { |
| 382 | unsigned int cur_freq = cpufreq_get(policy->cpu); |
| 383 | if (!cur_freq) |
| 384 | return sprintf(buf, "<unknown>"); |
| 385 | return sprintf(buf, "%u\n", cur_freq); |
| 386 | } |
| 387 | |
| 388 | |
| 389 | /** |
| 390 | * show_scaling_governor - show the current policy for the specified CPU |
| 391 | */ |
| 392 | static ssize_t show_scaling_governor (struct cpufreq_policy * policy, char *buf) |
| 393 | { |
| 394 | if(policy->policy == CPUFREQ_POLICY_POWERSAVE) |
| 395 | return sprintf(buf, "powersave\n"); |
| 396 | else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) |
| 397 | return sprintf(buf, "performance\n"); |
| 398 | else if (policy->governor) |
| 399 | return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name); |
| 400 | return -EINVAL; |
| 401 | } |
| 402 | |
| 403 | |
| 404 | /** |
| 405 | * store_scaling_governor - store policy for the specified CPU |
| 406 | */ |
| 407 | static ssize_t store_scaling_governor (struct cpufreq_policy * policy, |
| 408 | const char *buf, size_t count) |
| 409 | { |
| 410 | unsigned int ret = -EINVAL; |
| 411 | char str_governor[16]; |
| 412 | struct cpufreq_policy new_policy; |
| 413 | |
| 414 | ret = cpufreq_get_policy(&new_policy, policy->cpu); |
| 415 | if (ret) |
| 416 | return ret; |
| 417 | |
| 418 | ret = sscanf (buf, "%15s", str_governor); |
| 419 | if (ret != 1) |
| 420 | return -EINVAL; |
| 421 | |
| 422 | if (cpufreq_parse_governor(str_governor, &new_policy.policy, &new_policy.governor)) |
| 423 | return -EINVAL; |
| 424 | |
| 425 | ret = cpufreq_set_policy(&new_policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | return ret ? ret : count; |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * show_scaling_driver - show the cpufreq driver currently loaded |
| 431 | */ |
| 432 | static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf) |
| 433 | { |
| 434 | return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name); |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * show_scaling_available_governors - show the available CPUfreq governors |
| 439 | */ |
| 440 | static ssize_t show_scaling_available_governors (struct cpufreq_policy * policy, |
| 441 | char *buf) |
| 442 | { |
| 443 | ssize_t i = 0; |
| 444 | struct cpufreq_governor *t; |
| 445 | |
| 446 | if (!cpufreq_driver->target) { |
| 447 | i += sprintf(buf, "performance powersave"); |
| 448 | goto out; |
| 449 | } |
| 450 | |
| 451 | list_for_each_entry(t, &cpufreq_governor_list, governor_list) { |
| 452 | if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2))) |
| 453 | goto out; |
| 454 | i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name); |
| 455 | } |
Dave Jones | 7d5e350 | 2006-02-02 17:03:42 -0500 | [diff] [blame] | 456 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | i += sprintf(&buf[i], "\n"); |
| 458 | return i; |
| 459 | } |
| 460 | /** |
| 461 | * show_affected_cpus - show the CPUs affected by each transition |
| 462 | */ |
| 463 | static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf) |
| 464 | { |
| 465 | ssize_t i = 0; |
| 466 | unsigned int cpu; |
| 467 | |
| 468 | for_each_cpu_mask(cpu, policy->cpus) { |
| 469 | if (i) |
| 470 | i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " "); |
| 471 | i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu); |
| 472 | if (i >= (PAGE_SIZE - 5)) |
| 473 | break; |
| 474 | } |
| 475 | i += sprintf(&buf[i], "\n"); |
| 476 | return i; |
| 477 | } |
| 478 | |
| 479 | |
| 480 | #define define_one_ro(_name) \ |
| 481 | static struct freq_attr _name = \ |
| 482 | __ATTR(_name, 0444, show_##_name, NULL) |
| 483 | |
| 484 | #define define_one_ro0400(_name) \ |
| 485 | static struct freq_attr _name = \ |
| 486 | __ATTR(_name, 0400, show_##_name, NULL) |
| 487 | |
| 488 | #define define_one_rw(_name) \ |
| 489 | static struct freq_attr _name = \ |
| 490 | __ATTR(_name, 0644, show_##_name, store_##_name) |
| 491 | |
| 492 | define_one_ro0400(cpuinfo_cur_freq); |
| 493 | define_one_ro(cpuinfo_min_freq); |
| 494 | define_one_ro(cpuinfo_max_freq); |
| 495 | define_one_ro(scaling_available_governors); |
| 496 | define_one_ro(scaling_driver); |
| 497 | define_one_ro(scaling_cur_freq); |
| 498 | define_one_ro(affected_cpus); |
| 499 | define_one_rw(scaling_min_freq); |
| 500 | define_one_rw(scaling_max_freq); |
| 501 | define_one_rw(scaling_governor); |
| 502 | |
| 503 | static struct attribute * default_attrs[] = { |
| 504 | &cpuinfo_min_freq.attr, |
| 505 | &cpuinfo_max_freq.attr, |
| 506 | &scaling_min_freq.attr, |
| 507 | &scaling_max_freq.attr, |
| 508 | &affected_cpus.attr, |
| 509 | &scaling_governor.attr, |
| 510 | &scaling_driver.attr, |
| 511 | &scaling_available_governors.attr, |
| 512 | NULL |
| 513 | }; |
| 514 | |
| 515 | #define to_policy(k) container_of(k,struct cpufreq_policy,kobj) |
| 516 | #define to_attr(a) container_of(a,struct freq_attr,attr) |
| 517 | |
| 518 | static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf) |
| 519 | { |
| 520 | struct cpufreq_policy * policy = to_policy(kobj); |
| 521 | struct freq_attr * fattr = to_attr(attr); |
| 522 | ssize_t ret; |
| 523 | policy = cpufreq_cpu_get(policy->cpu); |
| 524 | if (!policy) |
| 525 | return -EINVAL; |
Dmitry Torokhov | 70f2817 | 2005-04-29 01:27:34 -0500 | [diff] [blame] | 526 | ret = fattr->show ? fattr->show(policy,buf) : -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | cpufreq_cpu_put(policy); |
| 528 | return ret; |
| 529 | } |
| 530 | |
| 531 | static ssize_t store(struct kobject * kobj, struct attribute * attr, |
| 532 | const char * buf, size_t count) |
| 533 | { |
| 534 | struct cpufreq_policy * policy = to_policy(kobj); |
| 535 | struct freq_attr * fattr = to_attr(attr); |
| 536 | ssize_t ret; |
| 537 | policy = cpufreq_cpu_get(policy->cpu); |
| 538 | if (!policy) |
| 539 | return -EINVAL; |
Dmitry Torokhov | 70f2817 | 2005-04-29 01:27:34 -0500 | [diff] [blame] | 540 | ret = fattr->store ? fattr->store(policy,buf,count) : -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | cpufreq_cpu_put(policy); |
| 542 | return ret; |
| 543 | } |
| 544 | |
| 545 | static void cpufreq_sysfs_release(struct kobject * kobj) |
| 546 | { |
| 547 | struct cpufreq_policy * policy = to_policy(kobj); |
| 548 | dprintk("last reference is dropped\n"); |
| 549 | complete(&policy->kobj_unregister); |
| 550 | } |
| 551 | |
| 552 | static struct sysfs_ops sysfs_ops = { |
| 553 | .show = show, |
| 554 | .store = store, |
| 555 | }; |
| 556 | |
| 557 | static struct kobj_type ktype_cpufreq = { |
| 558 | .sysfs_ops = &sysfs_ops, |
| 559 | .default_attrs = default_attrs, |
| 560 | .release = cpufreq_sysfs_release, |
| 561 | }; |
| 562 | |
| 563 | |
| 564 | /** |
| 565 | * cpufreq_add_dev - add a CPU device |
| 566 | * |
| 567 | * Adds the cpufreq interface for a CPU device. |
| 568 | */ |
| 569 | static int cpufreq_add_dev (struct sys_device * sys_dev) |
| 570 | { |
| 571 | unsigned int cpu = sys_dev->id; |
| 572 | int ret = 0; |
| 573 | struct cpufreq_policy new_policy; |
| 574 | struct cpufreq_policy *policy; |
| 575 | struct freq_attr **drv_attr; |
| 576 | unsigned long flags; |
| 577 | unsigned int j; |
| 578 | |
Ashok Raj | c32b6b8 | 2005-10-30 14:59:54 -0800 | [diff] [blame] | 579 | if (cpu_is_offline(cpu)) |
| 580 | return 0; |
| 581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | cpufreq_debug_disable_ratelimit(); |
| 583 | dprintk("adding CPU %u\n", cpu); |
| 584 | |
| 585 | #ifdef CONFIG_SMP |
| 586 | /* check whether a different CPU already registered this |
| 587 | * CPU because it is in the same boat. */ |
| 588 | policy = cpufreq_cpu_get(cpu); |
| 589 | if (unlikely(policy)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | dprintk("CPU already managed, adding link\n"); |
| 591 | sysfs_create_link(&sys_dev->kobj, &policy->kobj, "cpufreq"); |
| 592 | cpufreq_debug_enable_ratelimit(); |
| 593 | return 0; |
| 594 | } |
| 595 | #endif |
| 596 | |
| 597 | if (!try_module_get(cpufreq_driver->owner)) { |
| 598 | ret = -EINVAL; |
| 599 | goto module_out; |
| 600 | } |
| 601 | |
Dave Jones | e98df50 | 2005-10-20 15:17:43 -0700 | [diff] [blame] | 602 | policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | if (!policy) { |
| 604 | ret = -ENOMEM; |
| 605 | goto nomem_out; |
| 606 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | |
| 608 | policy->cpu = cpu; |
| 609 | policy->cpus = cpumask_of_cpu(cpu); |
| 610 | |
Arjan van de Ven | 83933af | 2006-01-14 16:01:49 +0100 | [diff] [blame] | 611 | mutex_init(&policy->lock); |
| 612 | mutex_lock(&policy->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | init_completion(&policy->kobj_unregister); |
| 614 | INIT_WORK(&policy->update, handle_update, (void *)(long)cpu); |
| 615 | |
| 616 | /* call driver. From then on the cpufreq must be able |
| 617 | * to accept all calls to ->verify and ->setpolicy for this CPU |
| 618 | */ |
| 619 | ret = cpufreq_driver->init(policy); |
| 620 | if (ret) { |
| 621 | dprintk("initialization failed\n"); |
Andrew Morton | f3876c1 | 2006-01-18 13:40:54 -0800 | [diff] [blame] | 622 | mutex_unlock(&policy->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | goto err_out; |
| 624 | } |
| 625 | |
| 626 | memcpy(&new_policy, policy, sizeof(struct cpufreq_policy)); |
| 627 | |
| 628 | /* prepare interface data */ |
| 629 | policy->kobj.parent = &sys_dev->kobj; |
| 630 | policy->kobj.ktype = &ktype_cpufreq; |
| 631 | strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN); |
| 632 | |
| 633 | ret = kobject_register(&policy->kobj); |
Andrew Morton | f3876c1 | 2006-01-18 13:40:54 -0800 | [diff] [blame] | 634 | if (ret) { |
| 635 | mutex_unlock(&policy->lock); |
Venkatesh Pallipadi | 8085e1f | 2005-08-25 13:14:06 -0700 | [diff] [blame] | 636 | goto err_out_driver_exit; |
Andrew Morton | f3876c1 | 2006-01-18 13:40:54 -0800 | [diff] [blame] | 637 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | /* set up files for this cpu device */ |
| 639 | drv_attr = cpufreq_driver->attr; |
| 640 | while ((drv_attr) && (*drv_attr)) { |
| 641 | sysfs_create_file(&policy->kobj, &((*drv_attr)->attr)); |
| 642 | drv_attr++; |
| 643 | } |
| 644 | if (cpufreq_driver->get) |
| 645 | sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr); |
| 646 | if (cpufreq_driver->target) |
| 647 | sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr); |
| 648 | |
| 649 | spin_lock_irqsave(&cpufreq_driver_lock, flags); |
| 650 | for_each_cpu_mask(j, policy->cpus) |
| 651 | cpufreq_cpu_data[j] = policy; |
| 652 | spin_unlock_irqrestore(&cpufreq_driver_lock, flags); |
| 653 | policy->governor = NULL; /* to assure that the starting sequence is |
| 654 | * run in cpufreq_set_policy */ |
Arjan van de Ven | 83933af | 2006-01-14 16:01:49 +0100 | [diff] [blame] | 655 | mutex_unlock(&policy->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | |
| 657 | /* set default policy */ |
| 658 | |
| 659 | ret = cpufreq_set_policy(&new_policy); |
| 660 | if (ret) { |
| 661 | dprintk("setting policy failed\n"); |
| 662 | goto err_out_unregister; |
| 663 | } |
| 664 | |
| 665 | module_put(cpufreq_driver->owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | dprintk("initialization complete\n"); |
| 667 | cpufreq_debug_enable_ratelimit(); |
| 668 | |
| 669 | return 0; |
| 670 | |
| 671 | |
| 672 | err_out_unregister: |
| 673 | spin_lock_irqsave(&cpufreq_driver_lock, flags); |
| 674 | for_each_cpu_mask(j, policy->cpus) |
| 675 | cpufreq_cpu_data[j] = NULL; |
| 676 | spin_unlock_irqrestore(&cpufreq_driver_lock, flags); |
| 677 | |
| 678 | kobject_unregister(&policy->kobj); |
| 679 | wait_for_completion(&policy->kobj_unregister); |
| 680 | |
Venkatesh Pallipadi | 8085e1f | 2005-08-25 13:14:06 -0700 | [diff] [blame] | 681 | err_out_driver_exit: |
| 682 | if (cpufreq_driver->exit) |
| 683 | cpufreq_driver->exit(policy); |
| 684 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | err_out: |
| 686 | kfree(policy); |
| 687 | |
| 688 | nomem_out: |
| 689 | module_put(cpufreq_driver->owner); |
Ashok Raj | c32b6b8 | 2005-10-30 14:59:54 -0800 | [diff] [blame] | 690 | module_out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | cpufreq_debug_enable_ratelimit(); |
| 692 | return ret; |
| 693 | } |
| 694 | |
| 695 | |
| 696 | /** |
| 697 | * cpufreq_remove_dev - remove a CPU device |
| 698 | * |
| 699 | * Removes the cpufreq interface for a CPU device. |
| 700 | */ |
| 701 | static int cpufreq_remove_dev (struct sys_device * sys_dev) |
| 702 | { |
| 703 | unsigned int cpu = sys_dev->id; |
| 704 | unsigned long flags; |
| 705 | struct cpufreq_policy *data; |
| 706 | #ifdef CONFIG_SMP |
Grant Coady | e738cf6 | 2005-11-21 21:32:28 -0800 | [diff] [blame] | 707 | struct sys_device *cpu_sys_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | unsigned int j; |
| 709 | #endif |
| 710 | |
| 711 | cpufreq_debug_disable_ratelimit(); |
| 712 | dprintk("unregistering CPU %u\n", cpu); |
| 713 | |
| 714 | spin_lock_irqsave(&cpufreq_driver_lock, flags); |
| 715 | data = cpufreq_cpu_data[cpu]; |
| 716 | |
| 717 | if (!data) { |
| 718 | spin_unlock_irqrestore(&cpufreq_driver_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | cpufreq_debug_enable_ratelimit(); |
| 720 | return -EINVAL; |
| 721 | } |
| 722 | cpufreq_cpu_data[cpu] = NULL; |
| 723 | |
| 724 | |
| 725 | #ifdef CONFIG_SMP |
| 726 | /* if this isn't the CPU which is the parent of the kobj, we |
| 727 | * only need to unlink, put and exit |
| 728 | */ |
| 729 | if (unlikely(cpu != data->cpu)) { |
| 730 | dprintk("removing link\n"); |
| 731 | spin_unlock_irqrestore(&cpufreq_driver_lock, flags); |
| 732 | sysfs_remove_link(&sys_dev->kobj, "cpufreq"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | cpufreq_cpu_put(data); |
| 734 | cpufreq_debug_enable_ratelimit(); |
| 735 | return 0; |
| 736 | } |
| 737 | #endif |
| 738 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | |
| 740 | if (!kobject_get(&data->kobj)) { |
| 741 | spin_unlock_irqrestore(&cpufreq_driver_lock, flags); |
| 742 | cpufreq_debug_enable_ratelimit(); |
| 743 | return -EFAULT; |
| 744 | } |
| 745 | |
| 746 | #ifdef CONFIG_SMP |
| 747 | /* if we have other CPUs still registered, we need to unlink them, |
| 748 | * or else wait_for_completion below will lock up. Clean the |
| 749 | * cpufreq_cpu_data[] while holding the lock, and remove the sysfs |
| 750 | * links afterwards. |
| 751 | */ |
| 752 | if (unlikely(cpus_weight(data->cpus) > 1)) { |
| 753 | for_each_cpu_mask(j, data->cpus) { |
| 754 | if (j == cpu) |
| 755 | continue; |
| 756 | cpufreq_cpu_data[j] = NULL; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | spin_unlock_irqrestore(&cpufreq_driver_lock, flags); |
| 761 | |
| 762 | if (unlikely(cpus_weight(data->cpus) > 1)) { |
| 763 | for_each_cpu_mask(j, data->cpus) { |
| 764 | if (j == cpu) |
| 765 | continue; |
| 766 | dprintk("removing link for cpu %u\n", j); |
Ashok Raj | d434fca | 2005-10-30 14:59:52 -0800 | [diff] [blame] | 767 | cpu_sys_dev = get_cpu_sysdev(j); |
| 768 | sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | cpufreq_cpu_put(data); |
| 770 | } |
| 771 | } |
| 772 | #else |
| 773 | spin_unlock_irqrestore(&cpufreq_driver_lock, flags); |
| 774 | #endif |
| 775 | |
Arjan van de Ven | 83933af | 2006-01-14 16:01:49 +0100 | [diff] [blame] | 776 | mutex_lock(&data->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | if (cpufreq_driver->target) |
| 778 | __cpufreq_governor(data, CPUFREQ_GOV_STOP); |
Arjan van de Ven | 83933af | 2006-01-14 16:01:49 +0100 | [diff] [blame] | 779 | mutex_unlock(&data->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | |
| 781 | kobject_unregister(&data->kobj); |
| 782 | |
| 783 | kobject_put(&data->kobj); |
| 784 | |
| 785 | /* we need to make sure that the underlying kobj is actually |
| 786 | * not referenced anymore by anybody before we proceed with |
| 787 | * unloading. |
| 788 | */ |
| 789 | dprintk("waiting for dropping of refcount\n"); |
| 790 | wait_for_completion(&data->kobj_unregister); |
| 791 | dprintk("wait complete\n"); |
| 792 | |
| 793 | if (cpufreq_driver->exit) |
| 794 | cpufreq_driver->exit(data); |
| 795 | |
| 796 | kfree(data); |
| 797 | |
| 798 | cpufreq_debug_enable_ratelimit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | return 0; |
| 800 | } |
| 801 | |
| 802 | |
| 803 | static void handle_update(void *data) |
| 804 | { |
| 805 | unsigned int cpu = (unsigned int)(long)data; |
| 806 | dprintk("handle_update for cpu %u called\n", cpu); |
| 807 | cpufreq_update_policy(cpu); |
| 808 | } |
| 809 | |
| 810 | /** |
| 811 | * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble. |
| 812 | * @cpu: cpu number |
| 813 | * @old_freq: CPU frequency the kernel thinks the CPU runs at |
| 814 | * @new_freq: CPU frequency the CPU actually runs at |
| 815 | * |
| 816 | * We adjust to current frequency first, and need to clean up later. So either call |
| 817 | * to cpufreq_update_policy() or schedule handle_update()). |
| 818 | */ |
| 819 | static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigned int new_freq) |
| 820 | { |
| 821 | struct cpufreq_freqs freqs; |
| 822 | |
Dave Jones | 78ee998 | 2005-05-31 19:03:43 -0700 | [diff] [blame] | 823 | dprintk(KERN_WARNING "Warning: CPU frequency out of sync: cpufreq and timing " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | "core thinks of %u, is %u kHz.\n", old_freq, new_freq); |
| 825 | |
| 826 | freqs.cpu = cpu; |
| 827 | freqs.old = old_freq; |
| 828 | freqs.new = new_freq; |
| 829 | cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE); |
| 830 | cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE); |
| 831 | } |
| 832 | |
| 833 | |
| 834 | /** |
Venkatesh Pallipadi | 95235ca | 2005-12-02 10:43:20 -0800 | [diff] [blame] | 835 | * cpufreq_quick_get - get the CPU frequency (in kHz) frpm policy->cur |
| 836 | * @cpu: CPU number |
| 837 | * |
| 838 | * This is the last known freq, without actually getting it from the driver. |
| 839 | * Return value will be same as what is shown in scaling_cur_freq in sysfs. |
| 840 | */ |
| 841 | unsigned int cpufreq_quick_get(unsigned int cpu) |
| 842 | { |
| 843 | struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); |
| 844 | unsigned int ret = 0; |
| 845 | |
| 846 | if (policy) { |
Arjan van de Ven | 83933af | 2006-01-14 16:01:49 +0100 | [diff] [blame] | 847 | mutex_lock(&policy->lock); |
Venkatesh Pallipadi | 95235ca | 2005-12-02 10:43:20 -0800 | [diff] [blame] | 848 | ret = policy->cur; |
Arjan van de Ven | 83933af | 2006-01-14 16:01:49 +0100 | [diff] [blame] | 849 | mutex_unlock(&policy->lock); |
Venkatesh Pallipadi | 95235ca | 2005-12-02 10:43:20 -0800 | [diff] [blame] | 850 | cpufreq_cpu_put(policy); |
| 851 | } |
| 852 | |
| 853 | return (ret); |
| 854 | } |
| 855 | EXPORT_SYMBOL(cpufreq_quick_get); |
| 856 | |
| 857 | |
| 858 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | * cpufreq_get - get the current CPU frequency (in kHz) |
| 860 | * @cpu: CPU number |
| 861 | * |
| 862 | * Get the CPU current (static) CPU frequency |
| 863 | */ |
| 864 | unsigned int cpufreq_get(unsigned int cpu) |
| 865 | { |
| 866 | struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); |
| 867 | unsigned int ret = 0; |
| 868 | |
| 869 | if (!policy) |
| 870 | return 0; |
| 871 | |
| 872 | if (!cpufreq_driver->get) |
| 873 | goto out; |
| 874 | |
Arjan van de Ven | 83933af | 2006-01-14 16:01:49 +0100 | [diff] [blame] | 875 | mutex_lock(&policy->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | |
| 877 | ret = cpufreq_driver->get(cpu); |
| 878 | |
Dave Jones | 7d5e350 | 2006-02-02 17:03:42 -0500 | [diff] [blame] | 879 | if (ret && policy->cur && !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | /* verify no discrepancy between actual and saved value exists */ |
| 881 | if (unlikely(ret != policy->cur)) { |
| 882 | cpufreq_out_of_sync(cpu, policy->cur, ret); |
| 883 | schedule_work(&policy->update); |
| 884 | } |
| 885 | } |
| 886 | |
Arjan van de Ven | 83933af | 2006-01-14 16:01:49 +0100 | [diff] [blame] | 887 | mutex_unlock(&policy->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | |
Dave Jones | 7d5e350 | 2006-02-02 17:03:42 -0500 | [diff] [blame] | 889 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | cpufreq_cpu_put(policy); |
| 891 | |
| 892 | return (ret); |
| 893 | } |
| 894 | EXPORT_SYMBOL(cpufreq_get); |
| 895 | |
| 896 | |
| 897 | /** |
Benjamin Herrenschmidt | 42d4dc3 | 2005-04-29 07:40:12 -0700 | [diff] [blame] | 898 | * cpufreq_suspend - let the low level driver prepare for suspend |
| 899 | */ |
| 900 | |
Bernard Blackham | e00d996 | 2005-07-07 17:56:42 -0700 | [diff] [blame] | 901 | static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg) |
Benjamin Herrenschmidt | 42d4dc3 | 2005-04-29 07:40:12 -0700 | [diff] [blame] | 902 | { |
| 903 | int cpu = sysdev->id; |
| 904 | unsigned int ret = 0; |
| 905 | unsigned int cur_freq = 0; |
| 906 | struct cpufreq_policy *cpu_policy; |
| 907 | |
| 908 | dprintk("resuming cpu %u\n", cpu); |
| 909 | |
| 910 | if (!cpu_online(cpu)) |
| 911 | return 0; |
| 912 | |
| 913 | /* we may be lax here as interrupts are off. Nonetheless |
| 914 | * we need to grab the correct cpu policy, as to check |
| 915 | * whether we really run on this CPU. |
| 916 | */ |
| 917 | |
| 918 | cpu_policy = cpufreq_cpu_get(cpu); |
| 919 | if (!cpu_policy) |
| 920 | return -EINVAL; |
| 921 | |
| 922 | /* only handle each CPU group once */ |
| 923 | if (unlikely(cpu_policy->cpu != cpu)) { |
| 924 | cpufreq_cpu_put(cpu_policy); |
| 925 | return 0; |
| 926 | } |
| 927 | |
| 928 | if (cpufreq_driver->suspend) { |
Bernard Blackham | e00d996 | 2005-07-07 17:56:42 -0700 | [diff] [blame] | 929 | ret = cpufreq_driver->suspend(cpu_policy, pmsg); |
Benjamin Herrenschmidt | 42d4dc3 | 2005-04-29 07:40:12 -0700 | [diff] [blame] | 930 | if (ret) { |
| 931 | printk(KERN_ERR "cpufreq: suspend failed in ->suspend " |
| 932 | "step on CPU %u\n", cpu_policy->cpu); |
| 933 | cpufreq_cpu_put(cpu_policy); |
| 934 | return ret; |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | |
| 939 | if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS) |
| 940 | goto out; |
| 941 | |
| 942 | if (cpufreq_driver->get) |
| 943 | cur_freq = cpufreq_driver->get(cpu_policy->cpu); |
| 944 | |
| 945 | if (!cur_freq || !cpu_policy->cur) { |
| 946 | printk(KERN_ERR "cpufreq: suspend failed to assert current " |
| 947 | "frequency is what timing core thinks it is.\n"); |
| 948 | goto out; |
| 949 | } |
| 950 | |
| 951 | if (unlikely(cur_freq != cpu_policy->cur)) { |
| 952 | struct cpufreq_freqs freqs; |
| 953 | |
| 954 | if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN)) |
Dave Jones | 78ee998 | 2005-05-31 19:03:43 -0700 | [diff] [blame] | 955 | dprintk(KERN_DEBUG "Warning: CPU frequency is %u, " |
Benjamin Herrenschmidt | 42d4dc3 | 2005-04-29 07:40:12 -0700 | [diff] [blame] | 956 | "cpufreq assumed %u kHz.\n", |
| 957 | cur_freq, cpu_policy->cur); |
| 958 | |
| 959 | freqs.cpu = cpu; |
| 960 | freqs.old = cpu_policy->cur; |
| 961 | freqs.new = cur_freq; |
| 962 | |
| 963 | notifier_call_chain(&cpufreq_transition_notifier_list, |
| 964 | CPUFREQ_SUSPENDCHANGE, &freqs); |
| 965 | adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs); |
| 966 | |
| 967 | cpu_policy->cur = cur_freq; |
| 968 | } |
| 969 | |
Dave Jones | 7d5e350 | 2006-02-02 17:03:42 -0500 | [diff] [blame] | 970 | out: |
Benjamin Herrenschmidt | 42d4dc3 | 2005-04-29 07:40:12 -0700 | [diff] [blame] | 971 | cpufreq_cpu_put(cpu_policy); |
| 972 | return 0; |
| 973 | } |
| 974 | |
| 975 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | * cpufreq_resume - restore proper CPU frequency handling after resume |
| 977 | * |
| 978 | * 1.) resume CPUfreq hardware support (cpufreq_driver->resume()) |
| 979 | * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync |
Benjamin Herrenschmidt | 42d4dc3 | 2005-04-29 07:40:12 -0700 | [diff] [blame] | 980 | * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are |
| 981 | * restored. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | */ |
| 983 | static int cpufreq_resume(struct sys_device * sysdev) |
| 984 | { |
| 985 | int cpu = sysdev->id; |
| 986 | unsigned int ret = 0; |
| 987 | struct cpufreq_policy *cpu_policy; |
| 988 | |
| 989 | dprintk("resuming cpu %u\n", cpu); |
| 990 | |
| 991 | if (!cpu_online(cpu)) |
| 992 | return 0; |
| 993 | |
| 994 | /* we may be lax here as interrupts are off. Nonetheless |
| 995 | * we need to grab the correct cpu policy, as to check |
| 996 | * whether we really run on this CPU. |
| 997 | */ |
| 998 | |
| 999 | cpu_policy = cpufreq_cpu_get(cpu); |
| 1000 | if (!cpu_policy) |
| 1001 | return -EINVAL; |
| 1002 | |
| 1003 | /* only handle each CPU group once */ |
| 1004 | if (unlikely(cpu_policy->cpu != cpu)) { |
| 1005 | cpufreq_cpu_put(cpu_policy); |
| 1006 | return 0; |
| 1007 | } |
| 1008 | |
| 1009 | if (cpufreq_driver->resume) { |
| 1010 | ret = cpufreq_driver->resume(cpu_policy); |
| 1011 | if (ret) { |
| 1012 | printk(KERN_ERR "cpufreq: resume failed in ->resume " |
| 1013 | "step on CPU %u\n", cpu_policy->cpu); |
| 1014 | cpufreq_cpu_put(cpu_policy); |
| 1015 | return ret; |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { |
| 1020 | unsigned int cur_freq = 0; |
| 1021 | |
| 1022 | if (cpufreq_driver->get) |
| 1023 | cur_freq = cpufreq_driver->get(cpu_policy->cpu); |
| 1024 | |
| 1025 | if (!cur_freq || !cpu_policy->cur) { |
Benjamin Herrenschmidt | 42d4dc3 | 2005-04-29 07:40:12 -0700 | [diff] [blame] | 1026 | printk(KERN_ERR "cpufreq: resume failed to assert " |
| 1027 | "current frequency is what timing core " |
| 1028 | "thinks it is.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | goto out; |
| 1030 | } |
| 1031 | |
| 1032 | if (unlikely(cur_freq != cpu_policy->cur)) { |
| 1033 | struct cpufreq_freqs freqs; |
| 1034 | |
Benjamin Herrenschmidt | ac09f69 | 2005-05-02 16:25:10 +1000 | [diff] [blame] | 1035 | if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN)) |
Dave Jones | 78ee998 | 2005-05-31 19:03:43 -0700 | [diff] [blame] | 1036 | dprintk(KERN_WARNING "Warning: CPU frequency" |
Benjamin Herrenschmidt | ac09f69 | 2005-05-02 16:25:10 +1000 | [diff] [blame] | 1037 | "is %u, cpufreq assumed %u kHz.\n", |
| 1038 | cur_freq, cpu_policy->cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | |
| 1040 | freqs.cpu = cpu; |
| 1041 | freqs.old = cpu_policy->cur; |
| 1042 | freqs.new = cur_freq; |
| 1043 | |
Benjamin Herrenschmidt | 42d4dc3 | 2005-04-29 07:40:12 -0700 | [diff] [blame] | 1044 | notifier_call_chain(&cpufreq_transition_notifier_list, |
| 1045 | CPUFREQ_RESUMECHANGE, &freqs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs); |
| 1047 | |
| 1048 | cpu_policy->cur = cur_freq; |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | out: |
| 1053 | schedule_work(&cpu_policy->update); |
| 1054 | cpufreq_cpu_put(cpu_policy); |
| 1055 | return ret; |
| 1056 | } |
| 1057 | |
| 1058 | static struct sysdev_driver cpufreq_sysdev_driver = { |
| 1059 | .add = cpufreq_add_dev, |
| 1060 | .remove = cpufreq_remove_dev, |
Benjamin Herrenschmidt | 42d4dc3 | 2005-04-29 07:40:12 -0700 | [diff] [blame] | 1061 | .suspend = cpufreq_suspend, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | .resume = cpufreq_resume, |
| 1063 | }; |
| 1064 | |
| 1065 | |
| 1066 | /********************************************************************* |
| 1067 | * NOTIFIER LISTS INTERFACE * |
| 1068 | *********************************************************************/ |
| 1069 | |
| 1070 | /** |
| 1071 | * cpufreq_register_notifier - register a driver with cpufreq |
| 1072 | * @nb: notifier function to register |
| 1073 | * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER |
| 1074 | * |
| 1075 | * Add a driver to one of two lists: either a list of drivers that |
| 1076 | * are notified about clock rate changes (once before and once after |
| 1077 | * the transition), or a list of drivers that are notified about |
| 1078 | * changes in cpufreq policy. |
| 1079 | * |
| 1080 | * This function may sleep, and has the same return conditions as |
| 1081 | * notifier_chain_register. |
| 1082 | */ |
| 1083 | int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list) |
| 1084 | { |
| 1085 | int ret; |
| 1086 | |
| 1087 | down_write(&cpufreq_notifier_rwsem); |
| 1088 | switch (list) { |
| 1089 | case CPUFREQ_TRANSITION_NOTIFIER: |
| 1090 | ret = notifier_chain_register(&cpufreq_transition_notifier_list, nb); |
| 1091 | break; |
| 1092 | case CPUFREQ_POLICY_NOTIFIER: |
| 1093 | ret = notifier_chain_register(&cpufreq_policy_notifier_list, nb); |
| 1094 | break; |
| 1095 | default: |
| 1096 | ret = -EINVAL; |
| 1097 | } |
| 1098 | up_write(&cpufreq_notifier_rwsem); |
| 1099 | |
| 1100 | return ret; |
| 1101 | } |
| 1102 | EXPORT_SYMBOL(cpufreq_register_notifier); |
| 1103 | |
| 1104 | |
| 1105 | /** |
| 1106 | * cpufreq_unregister_notifier - unregister a driver with cpufreq |
| 1107 | * @nb: notifier block to be unregistered |
| 1108 | * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER |
| 1109 | * |
| 1110 | * Remove a driver from the CPU frequency notifier list. |
| 1111 | * |
| 1112 | * This function may sleep, and has the same return conditions as |
| 1113 | * notifier_chain_unregister. |
| 1114 | */ |
| 1115 | int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list) |
| 1116 | { |
| 1117 | int ret; |
| 1118 | |
| 1119 | down_write(&cpufreq_notifier_rwsem); |
| 1120 | switch (list) { |
| 1121 | case CPUFREQ_TRANSITION_NOTIFIER: |
| 1122 | ret = notifier_chain_unregister(&cpufreq_transition_notifier_list, nb); |
| 1123 | break; |
| 1124 | case CPUFREQ_POLICY_NOTIFIER: |
| 1125 | ret = notifier_chain_unregister(&cpufreq_policy_notifier_list, nb); |
| 1126 | break; |
| 1127 | default: |
| 1128 | ret = -EINVAL; |
| 1129 | } |
| 1130 | up_write(&cpufreq_notifier_rwsem); |
| 1131 | |
| 1132 | return ret; |
| 1133 | } |
| 1134 | EXPORT_SYMBOL(cpufreq_unregister_notifier); |
| 1135 | |
| 1136 | |
| 1137 | /********************************************************************* |
| 1138 | * GOVERNORS * |
| 1139 | *********************************************************************/ |
| 1140 | |
| 1141 | |
| 1142 | int __cpufreq_driver_target(struct cpufreq_policy *policy, |
| 1143 | unsigned int target_freq, |
| 1144 | unsigned int relation) |
| 1145 | { |
| 1146 | int retval = -EINVAL; |
Ashok Raj | c32b6b8 | 2005-10-30 14:59:54 -0800 | [diff] [blame] | 1147 | |
Ashok Raj | a9d9baa | 2005-11-28 13:43:46 -0800 | [diff] [blame] | 1148 | lock_cpu_hotplug(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu, |
| 1150 | target_freq, relation); |
| 1151 | if (cpu_online(policy->cpu) && cpufreq_driver->target) |
| 1152 | retval = cpufreq_driver->target(policy, target_freq, relation); |
Ashok Raj | 90d45d1 | 2005-11-08 21:34:24 -0800 | [diff] [blame] | 1153 | |
Ashok Raj | a9d9baa | 2005-11-28 13:43:46 -0800 | [diff] [blame] | 1154 | unlock_cpu_hotplug(); |
Ashok Raj | 90d45d1 | 2005-11-08 21:34:24 -0800 | [diff] [blame] | 1155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | return retval; |
| 1157 | } |
| 1158 | EXPORT_SYMBOL_GPL(__cpufreq_driver_target); |
| 1159 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | int cpufreq_driver_target(struct cpufreq_policy *policy, |
| 1161 | unsigned int target_freq, |
| 1162 | unsigned int relation) |
| 1163 | { |
Dave Jones | cc993ca | 2005-07-28 09:43:56 -0700 | [diff] [blame] | 1164 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | |
| 1166 | policy = cpufreq_cpu_get(policy->cpu); |
| 1167 | if (!policy) |
| 1168 | return -EINVAL; |
| 1169 | |
Arjan van de Ven | 83933af | 2006-01-14 16:01:49 +0100 | [diff] [blame] | 1170 | mutex_lock(&policy->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | |
| 1172 | ret = __cpufreq_driver_target(policy, target_freq, relation); |
| 1173 | |
Arjan van de Ven | 83933af | 2006-01-14 16:01:49 +0100 | [diff] [blame] | 1174 | mutex_unlock(&policy->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | |
| 1176 | cpufreq_cpu_put(policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | return ret; |
| 1178 | } |
| 1179 | EXPORT_SYMBOL_GPL(cpufreq_driver_target); |
| 1180 | |
| 1181 | |
| 1182 | static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event) |
| 1183 | { |
Dave Jones | cc993ca | 2005-07-28 09:43:56 -0700 | [diff] [blame] | 1184 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | |
| 1186 | if (!try_module_get(policy->governor->owner)) |
| 1187 | return -EINVAL; |
| 1188 | |
| 1189 | dprintk("__cpufreq_governor for CPU %u, event %u\n", policy->cpu, event); |
| 1190 | ret = policy->governor->governor(policy, event); |
| 1191 | |
| 1192 | /* we keep one module reference alive for each CPU governed by this CPU */ |
| 1193 | if ((event != CPUFREQ_GOV_START) || ret) |
| 1194 | module_put(policy->governor->owner); |
| 1195 | if ((event == CPUFREQ_GOV_STOP) && !ret) |
| 1196 | module_put(policy->governor->owner); |
| 1197 | |
| 1198 | return ret; |
| 1199 | } |
| 1200 | |
| 1201 | |
| 1202 | int cpufreq_governor(unsigned int cpu, unsigned int event) |
| 1203 | { |
| 1204 | int ret = 0; |
| 1205 | struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); |
| 1206 | |
| 1207 | if (!policy) |
| 1208 | return -EINVAL; |
| 1209 | |
Arjan van de Ven | 83933af | 2006-01-14 16:01:49 +0100 | [diff] [blame] | 1210 | mutex_lock(&policy->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | ret = __cpufreq_governor(policy, event); |
Arjan van de Ven | 83933af | 2006-01-14 16:01:49 +0100 | [diff] [blame] | 1212 | mutex_unlock(&policy->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | |
| 1214 | cpufreq_cpu_put(policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | return ret; |
| 1216 | } |
| 1217 | EXPORT_SYMBOL_GPL(cpufreq_governor); |
| 1218 | |
| 1219 | |
| 1220 | int cpufreq_register_governor(struct cpufreq_governor *governor) |
| 1221 | { |
| 1222 | struct cpufreq_governor *t; |
| 1223 | |
| 1224 | if (!governor) |
| 1225 | return -EINVAL; |
| 1226 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 1227 | mutex_lock(&cpufreq_governor_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | |
| 1229 | list_for_each_entry(t, &cpufreq_governor_list, governor_list) { |
| 1230 | if (!strnicmp(governor->name,t->name,CPUFREQ_NAME_LEN)) { |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 1231 | mutex_unlock(&cpufreq_governor_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | return -EBUSY; |
| 1233 | } |
| 1234 | } |
| 1235 | list_add(&governor->governor_list, &cpufreq_governor_list); |
| 1236 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 1237 | mutex_unlock(&cpufreq_governor_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | return 0; |
| 1239 | } |
| 1240 | EXPORT_SYMBOL_GPL(cpufreq_register_governor); |
| 1241 | |
| 1242 | |
| 1243 | void cpufreq_unregister_governor(struct cpufreq_governor *governor) |
| 1244 | { |
| 1245 | if (!governor) |
| 1246 | return; |
| 1247 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 1248 | mutex_lock(&cpufreq_governor_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | list_del(&governor->governor_list); |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 1250 | mutex_unlock(&cpufreq_governor_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | return; |
| 1252 | } |
| 1253 | EXPORT_SYMBOL_GPL(cpufreq_unregister_governor); |
| 1254 | |
| 1255 | |
| 1256 | |
| 1257 | /********************************************************************* |
| 1258 | * POLICY INTERFACE * |
| 1259 | *********************************************************************/ |
| 1260 | |
| 1261 | /** |
| 1262 | * cpufreq_get_policy - get the current cpufreq_policy |
| 1263 | * @policy: struct cpufreq_policy into which the current cpufreq_policy is written |
| 1264 | * |
| 1265 | * Reads the current cpufreq policy. |
| 1266 | */ |
| 1267 | int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu) |
| 1268 | { |
| 1269 | struct cpufreq_policy *cpu_policy; |
| 1270 | if (!policy) |
| 1271 | return -EINVAL; |
| 1272 | |
| 1273 | cpu_policy = cpufreq_cpu_get(cpu); |
| 1274 | if (!cpu_policy) |
| 1275 | return -EINVAL; |
| 1276 | |
Arjan van de Ven | 83933af | 2006-01-14 16:01:49 +0100 | [diff] [blame] | 1277 | mutex_lock(&cpu_policy->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy)); |
Arjan van de Ven | 83933af | 2006-01-14 16:01:49 +0100 | [diff] [blame] | 1279 | mutex_unlock(&cpu_policy->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | |
| 1281 | cpufreq_cpu_put(cpu_policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | return 0; |
| 1283 | } |
| 1284 | EXPORT_SYMBOL(cpufreq_get_policy); |
| 1285 | |
| 1286 | |
| 1287 | static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy) |
| 1288 | { |
| 1289 | int ret = 0; |
| 1290 | |
| 1291 | cpufreq_debug_disable_ratelimit(); |
| 1292 | dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu, |
| 1293 | policy->min, policy->max); |
| 1294 | |
Dave Jones | 7d5e350 | 2006-02-02 17:03:42 -0500 | [diff] [blame] | 1295 | memcpy(&policy->cpuinfo, &data->cpuinfo, sizeof(struct cpufreq_cpuinfo)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | |
| 1297 | /* verify the cpu speed can be set within this limit */ |
| 1298 | ret = cpufreq_driver->verify(policy); |
| 1299 | if (ret) |
| 1300 | goto error_out; |
| 1301 | |
| 1302 | down_read(&cpufreq_notifier_rwsem); |
| 1303 | |
| 1304 | /* adjust if necessary - all reasons */ |
| 1305 | notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_ADJUST, |
| 1306 | policy); |
| 1307 | |
| 1308 | /* adjust if necessary - hardware incompatibility*/ |
| 1309 | notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_INCOMPATIBLE, |
| 1310 | policy); |
| 1311 | |
| 1312 | /* verify the cpu speed can be set within this limit, |
| 1313 | which might be different to the first one */ |
| 1314 | ret = cpufreq_driver->verify(policy); |
| 1315 | if (ret) { |
| 1316 | up_read(&cpufreq_notifier_rwsem); |
| 1317 | goto error_out; |
| 1318 | } |
| 1319 | |
| 1320 | /* notification of the new policy */ |
| 1321 | notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_NOTIFY, |
| 1322 | policy); |
| 1323 | |
| 1324 | up_read(&cpufreq_notifier_rwsem); |
| 1325 | |
Dave Jones | 7d5e350 | 2006-02-02 17:03:42 -0500 | [diff] [blame] | 1326 | data->min = policy->min; |
| 1327 | data->max = policy->max; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | |
| 1329 | dprintk("new min and max freqs are %u - %u kHz\n", data->min, data->max); |
| 1330 | |
| 1331 | if (cpufreq_driver->setpolicy) { |
| 1332 | data->policy = policy->policy; |
| 1333 | dprintk("setting range\n"); |
| 1334 | ret = cpufreq_driver->setpolicy(policy); |
| 1335 | } else { |
| 1336 | if (policy->governor != data->governor) { |
| 1337 | /* save old, working values */ |
| 1338 | struct cpufreq_governor *old_gov = data->governor; |
| 1339 | |
| 1340 | dprintk("governor switch\n"); |
| 1341 | |
| 1342 | /* end old governor */ |
| 1343 | if (data->governor) |
| 1344 | __cpufreq_governor(data, CPUFREQ_GOV_STOP); |
| 1345 | |
| 1346 | /* start new governor */ |
| 1347 | data->governor = policy->governor; |
| 1348 | if (__cpufreq_governor(data, CPUFREQ_GOV_START)) { |
| 1349 | /* new governor failed, so re-start old one */ |
| 1350 | dprintk("starting governor %s failed\n", data->governor->name); |
| 1351 | if (old_gov) { |
| 1352 | data->governor = old_gov; |
| 1353 | __cpufreq_governor(data, CPUFREQ_GOV_START); |
| 1354 | } |
| 1355 | ret = -EINVAL; |
| 1356 | goto error_out; |
| 1357 | } |
| 1358 | /* might be a policy change, too, so fall through */ |
| 1359 | } |
| 1360 | dprintk("governor: change or update limits\n"); |
| 1361 | __cpufreq_governor(data, CPUFREQ_GOV_LIMITS); |
| 1362 | } |
| 1363 | |
Dave Jones | 7d5e350 | 2006-02-02 17:03:42 -0500 | [diff] [blame] | 1364 | error_out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | cpufreq_debug_enable_ratelimit(); |
| 1366 | return ret; |
| 1367 | } |
| 1368 | |
| 1369 | /** |
| 1370 | * cpufreq_set_policy - set a new CPUFreq policy |
| 1371 | * @policy: policy to be set. |
| 1372 | * |
| 1373 | * Sets a new CPU frequency and voltage scaling policy. |
| 1374 | */ |
| 1375 | int cpufreq_set_policy(struct cpufreq_policy *policy) |
| 1376 | { |
| 1377 | int ret = 0; |
| 1378 | struct cpufreq_policy *data; |
| 1379 | |
| 1380 | if (!policy) |
| 1381 | return -EINVAL; |
| 1382 | |
| 1383 | data = cpufreq_cpu_get(policy->cpu); |
| 1384 | if (!data) |
| 1385 | return -EINVAL; |
| 1386 | |
| 1387 | /* lock this CPU */ |
Arjan van de Ven | 83933af | 2006-01-14 16:01:49 +0100 | [diff] [blame] | 1388 | mutex_lock(&data->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | |
| 1390 | ret = __cpufreq_set_policy(data, policy); |
| 1391 | data->user_policy.min = data->min; |
| 1392 | data->user_policy.max = data->max; |
| 1393 | data->user_policy.policy = data->policy; |
| 1394 | data->user_policy.governor = data->governor; |
| 1395 | |
Arjan van de Ven | 83933af | 2006-01-14 16:01:49 +0100 | [diff] [blame] | 1396 | mutex_unlock(&data->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | cpufreq_cpu_put(data); |
| 1398 | |
| 1399 | return ret; |
| 1400 | } |
| 1401 | EXPORT_SYMBOL(cpufreq_set_policy); |
| 1402 | |
| 1403 | |
| 1404 | /** |
| 1405 | * cpufreq_update_policy - re-evaluate an existing cpufreq policy |
| 1406 | * @cpu: CPU which shall be re-evaluated |
| 1407 | * |
| 1408 | * Usefull for policy notifiers which have different necessities |
| 1409 | * at different times. |
| 1410 | */ |
| 1411 | int cpufreq_update_policy(unsigned int cpu) |
| 1412 | { |
| 1413 | struct cpufreq_policy *data = cpufreq_cpu_get(cpu); |
| 1414 | struct cpufreq_policy policy; |
| 1415 | int ret = 0; |
| 1416 | |
| 1417 | if (!data) |
| 1418 | return -ENODEV; |
| 1419 | |
Arjan van de Ven | 83933af | 2006-01-14 16:01:49 +0100 | [diff] [blame] | 1420 | mutex_lock(&data->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | |
| 1422 | dprintk("updating policy for CPU %u\n", cpu); |
Dave Jones | 7d5e350 | 2006-02-02 17:03:42 -0500 | [diff] [blame] | 1423 | memcpy(&policy, data, sizeof(struct cpufreq_policy)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | policy.min = data->user_policy.min; |
| 1425 | policy.max = data->user_policy.max; |
| 1426 | policy.policy = data->user_policy.policy; |
| 1427 | policy.governor = data->user_policy.governor; |
| 1428 | |
Thomas Renninger | 0961dd0 | 2006-01-26 18:46:33 +0100 | [diff] [blame] | 1429 | /* BIOS might change freq behind our back |
| 1430 | -> ask driver for current freq and notify governors about a change */ |
| 1431 | if (cpufreq_driver->get) { |
| 1432 | policy.cur = cpufreq_driver->get(cpu); |
Thomas Renninger | a85f7bd | 2006-02-01 11:36:04 +0100 | [diff] [blame] | 1433 | if (!data->cur) { |
| 1434 | dprintk("Driver did not initialize current freq"); |
| 1435 | data->cur = policy.cur; |
| 1436 | } else { |
| 1437 | if (data->cur != policy.cur) |
| 1438 | cpufreq_out_of_sync(cpu, data->cur, policy.cur); |
| 1439 | } |
Thomas Renninger | 0961dd0 | 2006-01-26 18:46:33 +0100 | [diff] [blame] | 1440 | } |
| 1441 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | ret = __cpufreq_set_policy(data, &policy); |
| 1443 | |
Arjan van de Ven | 83933af | 2006-01-14 16:01:49 +0100 | [diff] [blame] | 1444 | mutex_unlock(&data->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | |
| 1446 | cpufreq_cpu_put(data); |
| 1447 | return ret; |
| 1448 | } |
| 1449 | EXPORT_SYMBOL(cpufreq_update_policy); |
| 1450 | |
Ashok Raj | c32b6b8 | 2005-10-30 14:59:54 -0800 | [diff] [blame] | 1451 | static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb, |
| 1452 | unsigned long action, void *hcpu) |
| 1453 | { |
| 1454 | unsigned int cpu = (unsigned long)hcpu; |
| 1455 | struct cpufreq_policy *policy; |
| 1456 | struct sys_device *sys_dev; |
| 1457 | |
| 1458 | sys_dev = get_cpu_sysdev(cpu); |
| 1459 | |
| 1460 | if (sys_dev) { |
| 1461 | switch (action) { |
| 1462 | case CPU_ONLINE: |
| 1463 | cpufreq_add_dev(sys_dev); |
| 1464 | break; |
| 1465 | case CPU_DOWN_PREPARE: |
| 1466 | /* |
| 1467 | * We attempt to put this cpu in lowest frequency |
| 1468 | * possible before going down. This will permit |
| 1469 | * hardware-managed P-State to switch other related |
| 1470 | * threads to min or higher speeds if possible. |
| 1471 | */ |
| 1472 | policy = cpufreq_cpu_data[cpu]; |
| 1473 | if (policy) { |
| 1474 | cpufreq_driver_target(policy, policy->min, |
| 1475 | CPUFREQ_RELATION_H); |
| 1476 | } |
| 1477 | break; |
| 1478 | case CPU_DEAD: |
| 1479 | cpufreq_remove_dev(sys_dev); |
| 1480 | break; |
| 1481 | } |
| 1482 | } |
| 1483 | return NOTIFY_OK; |
| 1484 | } |
| 1485 | |
| 1486 | static struct notifier_block cpufreq_cpu_notifier = |
| 1487 | { |
| 1488 | .notifier_call = cpufreq_cpu_callback, |
| 1489 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | |
| 1491 | /********************************************************************* |
| 1492 | * REGISTER / UNREGISTER CPUFREQ DRIVER * |
| 1493 | *********************************************************************/ |
| 1494 | |
| 1495 | /** |
| 1496 | * cpufreq_register_driver - register a CPU Frequency driver |
| 1497 | * @driver_data: A struct cpufreq_driver containing the values# |
| 1498 | * submitted by the CPU Frequency driver. |
| 1499 | * |
| 1500 | * Registers a CPU Frequency driver to this core code. This code |
| 1501 | * returns zero on success, -EBUSY when another driver got here first |
| 1502 | * (and isn't unregistered in the meantime). |
| 1503 | * |
| 1504 | */ |
| 1505 | int cpufreq_register_driver(struct cpufreq_driver *driver_data) |
| 1506 | { |
| 1507 | unsigned long flags; |
| 1508 | int ret; |
| 1509 | |
| 1510 | if (!driver_data || !driver_data->verify || !driver_data->init || |
| 1511 | ((!driver_data->setpolicy) && (!driver_data->target))) |
| 1512 | return -EINVAL; |
| 1513 | |
| 1514 | dprintk("trying to register driver %s\n", driver_data->name); |
| 1515 | |
| 1516 | if (driver_data->setpolicy) |
| 1517 | driver_data->flags |= CPUFREQ_CONST_LOOPS; |
| 1518 | |
| 1519 | spin_lock_irqsave(&cpufreq_driver_lock, flags); |
| 1520 | if (cpufreq_driver) { |
| 1521 | spin_unlock_irqrestore(&cpufreq_driver_lock, flags); |
| 1522 | return -EBUSY; |
| 1523 | } |
| 1524 | cpufreq_driver = driver_data; |
| 1525 | spin_unlock_irqrestore(&cpufreq_driver_lock, flags); |
| 1526 | |
| 1527 | ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver); |
| 1528 | |
| 1529 | if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) { |
| 1530 | int i; |
| 1531 | ret = -ENODEV; |
| 1532 | |
| 1533 | /* check for at least one working CPU */ |
| 1534 | for (i=0; i<NR_CPUS; i++) |
| 1535 | if (cpufreq_cpu_data[i]) |
| 1536 | ret = 0; |
| 1537 | |
| 1538 | /* if all ->init() calls failed, unregister */ |
| 1539 | if (ret) { |
| 1540 | dprintk("no CPU initialized for driver %s\n", driver_data->name); |
| 1541 | sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver); |
| 1542 | |
| 1543 | spin_lock_irqsave(&cpufreq_driver_lock, flags); |
| 1544 | cpufreq_driver = NULL; |
| 1545 | spin_unlock_irqrestore(&cpufreq_driver_lock, flags); |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | if (!ret) { |
Ashok Raj | c32b6b8 | 2005-10-30 14:59:54 -0800 | [diff] [blame] | 1550 | register_cpu_notifier(&cpufreq_cpu_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | dprintk("driver %s up and running\n", driver_data->name); |
| 1552 | cpufreq_debug_enable_ratelimit(); |
| 1553 | } |
| 1554 | |
| 1555 | return (ret); |
| 1556 | } |
| 1557 | EXPORT_SYMBOL_GPL(cpufreq_register_driver); |
| 1558 | |
| 1559 | |
| 1560 | /** |
| 1561 | * cpufreq_unregister_driver - unregister the current CPUFreq driver |
| 1562 | * |
| 1563 | * Unregister the current CPUFreq driver. Only call this if you have |
| 1564 | * the right to do so, i.e. if you have succeeded in initialising before! |
| 1565 | * Returns zero if successful, and -EINVAL if the cpufreq_driver is |
| 1566 | * currently not initialised. |
| 1567 | */ |
| 1568 | int cpufreq_unregister_driver(struct cpufreq_driver *driver) |
| 1569 | { |
| 1570 | unsigned long flags; |
| 1571 | |
| 1572 | cpufreq_debug_disable_ratelimit(); |
| 1573 | |
| 1574 | if (!cpufreq_driver || (driver != cpufreq_driver)) { |
| 1575 | cpufreq_debug_enable_ratelimit(); |
| 1576 | return -EINVAL; |
| 1577 | } |
| 1578 | |
| 1579 | dprintk("unregistering driver %s\n", driver->name); |
| 1580 | |
| 1581 | sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver); |
Ashok Raj | c32b6b8 | 2005-10-30 14:59:54 -0800 | [diff] [blame] | 1582 | unregister_cpu_notifier(&cpufreq_cpu_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | |
| 1584 | spin_lock_irqsave(&cpufreq_driver_lock, flags); |
| 1585 | cpufreq_driver = NULL; |
| 1586 | spin_unlock_irqrestore(&cpufreq_driver_lock, flags); |
| 1587 | |
| 1588 | return 0; |
| 1589 | } |
| 1590 | EXPORT_SYMBOL_GPL(cpufreq_unregister_driver); |