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