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