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