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