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