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