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