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