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