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