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