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