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