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> |
Amit Kucheria | 5c238a8 | 2019-01-30 10:52:01 +0530 | [diff] [blame] | 22 | #include <linux/cpu_cooling.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/device.h> |
Viresh Kumar | 5ff0a26 | 2013-08-06 22:53:03 +0530 | [diff] [blame] | 25 | #include <linux/init.h> |
| 26 | #include <linux/kernel_stat.h> |
| 27 | #include <linux/module.h> |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 28 | #include <linux/mutex.h> |
Viresh Kumar | 5ff0a26 | 2013-08-06 22:53:03 +0530 | [diff] [blame] | 29 | #include <linux/slab.h> |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 30 | #include <linux/suspend.h> |
Doug Anderson | 90de2a4 | 2014-12-23 22:09:48 -0800 | [diff] [blame] | 31 | #include <linux/syscore_ops.h> |
Viresh Kumar | 5ff0a26 | 2013-08-06 22:53:03 +0530 | [diff] [blame] | 32 | #include <linux/tick.h> |
Thomas Renninger | 6f4f272 | 2010-04-20 13:17:36 +0200 | [diff] [blame] | 33 | #include <trace/events/power.h> |
| 34 | |
Viresh Kumar | b4f0676 | 2015-01-27 14:06:08 +0530 | [diff] [blame] | 35 | static LIST_HEAD(cpufreq_policy_list); |
Viresh Kumar | f963735 | 2015-05-12 12:20:11 +0530 | [diff] [blame] | 36 | |
| 37 | static inline bool policy_is_inactive(struct cpufreq_policy *policy) |
| 38 | { |
| 39 | return cpumask_empty(policy->cpus); |
| 40 | } |
| 41 | |
Viresh Kumar | f963735 | 2015-05-12 12:20:11 +0530 | [diff] [blame] | 42 | /* Macros to iterate over CPU policies */ |
Eric Biggers | fd7dc7e | 2016-02-21 12:53:12 -0600 | [diff] [blame] | 43 | #define for_each_suitable_policy(__policy, __active) \ |
| 44 | list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \ |
| 45 | if ((__active) == !policy_is_inactive(__policy)) |
Viresh Kumar | f963735 | 2015-05-12 12:20:11 +0530 | [diff] [blame] | 46 | |
| 47 | #define for_each_active_policy(__policy) \ |
| 48 | for_each_suitable_policy(__policy, true) |
| 49 | #define for_each_inactive_policy(__policy) \ |
| 50 | for_each_suitable_policy(__policy, false) |
| 51 | |
| 52 | #define for_each_policy(__policy) \ |
Viresh Kumar | b4f0676 | 2015-01-27 14:06:08 +0530 | [diff] [blame] | 53 | list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) |
| 54 | |
Viresh Kumar | f7b2706 | 2015-01-27 14:06:09 +0530 | [diff] [blame] | 55 | /* Iterate over governors */ |
| 56 | static LIST_HEAD(cpufreq_governor_list); |
| 57 | #define for_each_governor(__governor) \ |
| 58 | list_for_each_entry(__governor, &cpufreq_governor_list, governor_list) |
| 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | /** |
Dave Jones | cd87847 | 2006-08-11 17:59:28 -0400 | [diff] [blame] | 61 | * The "cpufreq driver" - the arch- or hardware-dependent low |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | * level driver of CPUFreq support, and its spinlock. This lock |
| 63 | * also protects the cpufreq_cpu_data array. |
| 64 | */ |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 65 | static struct cpufreq_driver *cpufreq_driver; |
Mike Travis | 7a6aedf | 2008-03-25 15:06:53 -0700 | [diff] [blame] | 66 | static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data); |
Viresh Kumar | bb176f7 | 2013-06-19 14:19:33 +0530 | [diff] [blame] | 67 | static DEFINE_RWLOCK(cpufreq_driver_lock); |
Viresh Kumar | bb176f7 | 2013-06-19 14:19:33 +0530 | [diff] [blame] | 68 | |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 69 | /* Flag to suspend/resume CPUFreq governors */ |
| 70 | static bool cpufreq_suspended; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 72 | static inline bool has_target(void) |
| 73 | { |
| 74 | return cpufreq_driver->target_index || cpufreq_driver->target; |
| 75 | } |
| 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | /* internal prototypes */ |
Viresh Kumar | d92d50a | 2015-01-02 12:34:29 +0530 | [diff] [blame] | 78 | static unsigned int __cpufreq_get(struct cpufreq_policy *policy); |
Rafael J. Wysocki | a92604b | 2016-05-14 01:01:46 +0200 | [diff] [blame] | 79 | static int cpufreq_init_governor(struct cpufreq_policy *policy); |
| 80 | static void cpufreq_exit_governor(struct cpufreq_policy *policy); |
Rafael J. Wysocki | 0a30076 | 2016-03-21 15:45:24 +0100 | [diff] [blame] | 81 | static int cpufreq_start_governor(struct cpufreq_policy *policy); |
Rafael J. Wysocki | a92604b | 2016-05-14 01:01:46 +0200 | [diff] [blame] | 82 | static void cpufreq_stop_governor(struct cpufreq_policy *policy); |
| 83 | static void cpufreq_governor_limits(struct cpufreq_policy *policy); |
Rafael J. Wysocki | 45482c7 | 2016-05-12 15:14:12 +0200 | [diff] [blame] | 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | /** |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 86 | * Two notifier lists: the "policy" list is involved in the |
| 87 | * validation process for a new CPU frequency policy; the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | * "transition" list for kernel code that needs to handle |
| 89 | * changes to devices when the CPU clock speed changes. |
| 90 | * The mutex locks both lists. |
| 91 | */ |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 92 | static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list); |
Sebastian Andrzej Siewior | cc85de3 | 2018-05-25 12:19:58 +0200 | [diff] [blame] | 93 | SRCU_NOTIFIER_HEAD_STATIC(cpufreq_transition_notifier_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
Konrad Rzeszutek Wilk | a7b422c | 2012-03-13 19:18:39 -0400 | [diff] [blame] | 95 | static int off __read_mostly; |
Viresh Kumar | da58445 | 2012-10-26 00:51:32 +0200 | [diff] [blame] | 96 | static int cpufreq_disabled(void) |
Konrad Rzeszutek Wilk | a7b422c | 2012-03-13 19:18:39 -0400 | [diff] [blame] | 97 | { |
| 98 | return off; |
| 99 | } |
| 100 | void disable_cpufreq(void) |
| 101 | { |
| 102 | off = 1; |
| 103 | } |
Dave Jones | 29464f2 | 2009-01-18 01:37:11 -0500 | [diff] [blame] | 104 | static DEFINE_MUTEX(cpufreq_governor_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 106 | bool have_governor_per_policy(void) |
| 107 | { |
Viresh Kumar | 0b981e7 | 2013-10-02 14:13:18 +0530 | [diff] [blame] | 108 | return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY); |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 109 | } |
Viresh Kumar | 3f869d6 | 2013-05-16 05:09:56 +0000 | [diff] [blame] | 110 | EXPORT_SYMBOL_GPL(have_governor_per_policy); |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 111 | |
Viresh Kumar | 944e9a0 | 2013-05-16 05:09:57 +0000 | [diff] [blame] | 112 | struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy) |
| 113 | { |
| 114 | if (have_governor_per_policy()) |
| 115 | return &policy->kobj; |
| 116 | else |
| 117 | return cpufreq_global_kobject; |
| 118 | } |
| 119 | EXPORT_SYMBOL_GPL(get_governor_parent_kobj); |
| 120 | |
Viresh Kumar | 72a4ce3 | 2013-05-17 11:26:32 +0000 | [diff] [blame] | 121 | static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall) |
| 122 | { |
| 123 | u64 idle_time; |
| 124 | u64 cur_wall_time; |
| 125 | u64 busy_time; |
| 126 | |
Frederic Weisbecker | 7fb1327 | 2017-01-31 04:09:19 +0100 | [diff] [blame] | 127 | cur_wall_time = jiffies64_to_nsecs(get_jiffies_64()); |
Viresh Kumar | 72a4ce3 | 2013-05-17 11:26:32 +0000 | [diff] [blame] | 128 | |
| 129 | busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER]; |
| 130 | busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM]; |
| 131 | busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ]; |
| 132 | busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ]; |
| 133 | busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL]; |
| 134 | busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE]; |
| 135 | |
| 136 | idle_time = cur_wall_time - busy_time; |
| 137 | if (wall) |
Frederic Weisbecker | 7fb1327 | 2017-01-31 04:09:19 +0100 | [diff] [blame] | 138 | *wall = div_u64(cur_wall_time, NSEC_PER_USEC); |
Viresh Kumar | 72a4ce3 | 2013-05-17 11:26:32 +0000 | [diff] [blame] | 139 | |
Frederic Weisbecker | 7fb1327 | 2017-01-31 04:09:19 +0100 | [diff] [blame] | 140 | return div_u64(idle_time, NSEC_PER_USEC); |
Viresh Kumar | 72a4ce3 | 2013-05-17 11:26:32 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy) |
| 144 | { |
| 145 | u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL); |
| 146 | |
| 147 | if (idle_time == -1ULL) |
| 148 | return get_cpu_idle_time_jiffy(cpu, wall); |
| 149 | else if (!io_busy) |
| 150 | idle_time += get_cpu_iowait_time_us(cpu, wall); |
| 151 | |
| 152 | return idle_time; |
| 153 | } |
| 154 | EXPORT_SYMBOL_GPL(get_cpu_idle_time); |
| 155 | |
Dietmar Eggemann | e7d5459 | 2017-09-26 17:41:07 +0100 | [diff] [blame] | 156 | __weak void arch_set_freq_scale(struct cpumask *cpus, unsigned long cur_freq, |
| 157 | unsigned long max_freq) |
| 158 | { |
| 159 | } |
| 160 | EXPORT_SYMBOL_GPL(arch_set_freq_scale); |
| 161 | |
Viresh Kumar | 70e9e77 | 2013-10-03 20:29:07 +0530 | [diff] [blame] | 162 | /* |
| 163 | * This is a generic cpufreq init() routine which can be used by cpufreq |
| 164 | * drivers of SMP systems. It will do following: |
| 165 | * - validate & show freq table passed |
| 166 | * - set policies transition latency |
| 167 | * - policy->cpus with all possible CPUs |
| 168 | */ |
| 169 | int cpufreq_generic_init(struct cpufreq_policy *policy, |
| 170 | struct cpufreq_frequency_table *table, |
| 171 | unsigned int transition_latency) |
| 172 | { |
Viresh Kumar | 92c99d1 | 2018-02-26 10:38:45 +0530 | [diff] [blame] | 173 | policy->freq_table = table; |
Viresh Kumar | 70e9e77 | 2013-10-03 20:29:07 +0530 | [diff] [blame] | 174 | policy->cpuinfo.transition_latency = transition_latency; |
| 175 | |
| 176 | /* |
Shailendra Verma | 58405af | 2015-05-22 22:48:22 +0530 | [diff] [blame] | 177 | * The driver only supports the SMP configuration where all processors |
Viresh Kumar | 70e9e77 | 2013-10-03 20:29:07 +0530 | [diff] [blame] | 178 | * share the clock and voltage and clock. |
| 179 | */ |
| 180 | cpumask_setall(policy->cpus); |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | EXPORT_SYMBOL_GPL(cpufreq_generic_init); |
| 185 | |
Rafael J. Wysocki | 1f0bd44 | 2015-09-16 02:17:49 +0200 | [diff] [blame] | 186 | struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu) |
Viresh Kumar | 652ed95 | 2014-01-09 20:38:43 +0530 | [diff] [blame] | 187 | { |
| 188 | struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); |
| 189 | |
Viresh Kumar | 988bed0 | 2015-05-08 11:53:45 +0530 | [diff] [blame] | 190 | return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL; |
| 191 | } |
Rafael J. Wysocki | 1f0bd44 | 2015-09-16 02:17:49 +0200 | [diff] [blame] | 192 | EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw); |
Viresh Kumar | 988bed0 | 2015-05-08 11:53:45 +0530 | [diff] [blame] | 193 | |
| 194 | unsigned int cpufreq_generic_get(unsigned int cpu) |
| 195 | { |
| 196 | struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu); |
| 197 | |
Viresh Kumar | 652ed95 | 2014-01-09 20:38:43 +0530 | [diff] [blame] | 198 | if (!policy || IS_ERR(policy->clk)) { |
Joe Perches | e837f9b | 2014-03-11 10:03:00 -0700 | [diff] [blame] | 199 | pr_err("%s: No %s associated to cpu: %d\n", |
| 200 | __func__, policy ? "clk" : "policy", cpu); |
Viresh Kumar | 652ed95 | 2014-01-09 20:38:43 +0530 | [diff] [blame] | 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | return clk_get_rate(policy->clk) / 1000; |
| 205 | } |
| 206 | EXPORT_SYMBOL_GPL(cpufreq_generic_get); |
| 207 | |
Viresh Kumar | 50e9c85 | 2015-02-19 17:02:03 +0530 | [diff] [blame] | 208 | /** |
Rafael J. Wysocki | 5d094fe | 2019-03-05 11:44:04 +0100 | [diff] [blame] | 209 | * cpufreq_cpu_get - Return policy for a CPU and mark it as busy. |
| 210 | * @cpu: CPU to find the policy for. |
Viresh Kumar | 50e9c85 | 2015-02-19 17:02:03 +0530 | [diff] [blame] | 211 | * |
Rafael J. Wysocki | 5d094fe | 2019-03-05 11:44:04 +0100 | [diff] [blame] | 212 | * Call cpufreq_cpu_get_raw() to obtain a cpufreq policy for @cpu and increment |
| 213 | * the kobject reference counter of that policy. Return a valid policy on |
| 214 | * success or NULL on failure. |
Viresh Kumar | 50e9c85 | 2015-02-19 17:02:03 +0530 | [diff] [blame] | 215 | * |
Rafael J. Wysocki | 5d094fe | 2019-03-05 11:44:04 +0100 | [diff] [blame] | 216 | * The policy returned by this function has to be released with the help of |
| 217 | * cpufreq_cpu_put() to balance its kobject reference counter properly. |
Viresh Kumar | 50e9c85 | 2015-02-19 17:02:03 +0530 | [diff] [blame] | 218 | */ |
Viresh Kumar | 6eed940 | 2013-08-06 22:53:11 +0530 | [diff] [blame] | 219 | struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | { |
Viresh Kumar | 6eed940 | 2013-08-06 22:53:11 +0530 | [diff] [blame] | 221 | struct cpufreq_policy *policy = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | unsigned long flags; |
| 223 | |
Viresh Kumar | 1b947c90 | 2015-02-19 17:02:05 +0530 | [diff] [blame] | 224 | if (WARN_ON(cpu >= nr_cpu_ids)) |
Viresh Kumar | 6eed940 | 2013-08-06 22:53:11 +0530 | [diff] [blame] | 225 | return NULL; |
| 226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | /* get the cpufreq driver */ |
Nathan Zimmer | 0d1857a | 2013-02-22 16:24:34 +0000 | [diff] [blame] | 228 | read_lock_irqsave(&cpufreq_driver_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
Viresh Kumar | 6eed940 | 2013-08-06 22:53:11 +0530 | [diff] [blame] | 230 | if (cpufreq_driver) { |
| 231 | /* get the CPU */ |
Viresh Kumar | 988bed0 | 2015-05-08 11:53:45 +0530 | [diff] [blame] | 232 | policy = cpufreq_cpu_get_raw(cpu); |
Viresh Kumar | 6eed940 | 2013-08-06 22:53:11 +0530 | [diff] [blame] | 233 | if (policy) |
| 234 | kobject_get(&policy->kobj); |
| 235 | } |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 236 | |
Viresh Kumar | 6eed940 | 2013-08-06 22:53:11 +0530 | [diff] [blame] | 237 | read_unlock_irqrestore(&cpufreq_driver_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 239 | return policy; |
Stephen Boyd | a914443 | 2012-07-20 18:14:38 +0000 | [diff] [blame] | 240 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | EXPORT_SYMBOL_GPL(cpufreq_cpu_get); |
| 242 | |
Viresh Kumar | 50e9c85 | 2015-02-19 17:02:03 +0530 | [diff] [blame] | 243 | /** |
Rafael J. Wysocki | 5d094fe | 2019-03-05 11:44:04 +0100 | [diff] [blame] | 244 | * cpufreq_cpu_put - Decrement kobject usage counter for cpufreq policy. |
| 245 | * @policy: cpufreq policy returned by cpufreq_cpu_get(). |
Viresh Kumar | 50e9c85 | 2015-02-19 17:02:03 +0530 | [diff] [blame] | 246 | */ |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 247 | void cpufreq_cpu_put(struct cpufreq_policy *policy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | { |
Viresh Kumar | 6eed940 | 2013-08-06 22:53:11 +0530 | [diff] [blame] | 249 | kobject_put(&policy->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | } |
| 251 | EXPORT_SYMBOL_GPL(cpufreq_cpu_put); |
| 252 | |
Rafael J. Wysocki | 540a3758 | 2019-03-26 12:16:58 +0100 | [diff] [blame^] | 253 | /** |
| 254 | * cpufreq_cpu_release - Unlock a policy and decrement its usage counter. |
| 255 | * @policy: cpufreq policy returned by cpufreq_cpu_acquire(). |
| 256 | */ |
| 257 | static void cpufreq_cpu_release(struct cpufreq_policy *policy) |
| 258 | { |
| 259 | if (WARN_ON(!policy)) |
| 260 | return; |
| 261 | |
| 262 | lockdep_assert_held(&policy->rwsem); |
| 263 | |
| 264 | up_write(&policy->rwsem); |
| 265 | |
| 266 | cpufreq_cpu_put(policy); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * cpufreq_cpu_acquire - Find policy for a CPU, mark it as busy and lock it. |
| 271 | * @cpu: CPU to find the policy for. |
| 272 | * |
| 273 | * Call cpufreq_cpu_get() to get a reference on the cpufreq policy for @cpu and |
| 274 | * if the policy returned by it is not NULL, acquire its rwsem for writing. |
| 275 | * Return the policy if it is active or release it and return NULL otherwise. |
| 276 | * |
| 277 | * The policy returned by this function has to be released with the help of |
| 278 | * cpufreq_cpu_release() in order to release its rwsem and balance its usage |
| 279 | * counter properly. |
| 280 | */ |
| 281 | static struct cpufreq_policy *cpufreq_cpu_acquire(unsigned int cpu) |
| 282 | { |
| 283 | struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); |
| 284 | |
| 285 | if (!policy) |
| 286 | return NULL; |
| 287 | |
| 288 | down_write(&policy->rwsem); |
| 289 | |
| 290 | if (policy_is_inactive(policy)) { |
| 291 | cpufreq_cpu_release(policy); |
| 292 | return NULL; |
| 293 | } |
| 294 | |
| 295 | return policy; |
| 296 | } |
| 297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | /********************************************************************* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | * EXTERNALLY AFFECTING FREQUENCY CHANGES * |
| 300 | *********************************************************************/ |
| 301 | |
| 302 | /** |
| 303 | * adjust_jiffies - adjust the system "loops_per_jiffy" |
| 304 | * |
| 305 | * This function alters the system "loops_per_jiffy" for the clock |
| 306 | * speed change. Note that loops_per_jiffy cannot be updated on SMP |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 307 | * systems as each CPU might be scaled differently. So, use the arch |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | * per-CPU loops_per_jiffy value wherever possible. |
| 309 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 310 | static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | { |
Viresh Kumar | 39c132e | 2015-01-02 12:34:34 +0530 | [diff] [blame] | 312 | #ifndef CONFIG_SMP |
| 313 | static unsigned long l_p_j_ref; |
| 314 | static unsigned int l_p_j_ref_freq; |
| 315 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | if (ci->flags & CPUFREQ_CONST_LOOPS) |
| 317 | return; |
| 318 | |
| 319 | if (!l_p_j_ref_freq) { |
| 320 | l_p_j_ref = loops_per_jiffy; |
| 321 | l_p_j_ref_freq = ci->old; |
Joe Perches | e837f9b | 2014-03-11 10:03:00 -0700 | [diff] [blame] | 322 | pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", |
| 323 | l_p_j_ref, l_p_j_ref_freq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | } |
Viresh Kumar | 0b443ea | 2014-03-19 11:24:58 +0530 | [diff] [blame] | 325 | if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) { |
Gautham R Shenoy | e08f5f5 | 2006-10-26 16:20:58 +0530 | [diff] [blame] | 326 | loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, |
| 327 | ci->new); |
Joe Perches | e837f9b | 2014-03-11 10:03:00 -0700 | [diff] [blame] | 328 | pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n", |
| 329 | loops_per_jiffy, ci->new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | #endif |
Viresh Kumar | 39c132e | 2015-01-02 12:34:34 +0530 | [diff] [blame] | 332 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
Viresh Kumar | 20b5324 | 2018-05-10 15:00:29 +0530 | [diff] [blame] | 334 | /** |
| 335 | * cpufreq_notify_transition - Notify frequency transition and adjust_jiffies. |
| 336 | * @policy: cpufreq policy to enable fast frequency switching for. |
| 337 | * @freqs: contain details of the frequency update. |
| 338 | * @state: set to CPUFREQ_PRECHANGE or CPUFREQ_POSTCHANGE. |
| 339 | * |
| 340 | * This function calls the transition notifiers and the "adjust_jiffies" |
| 341 | * function. It is called twice on all CPU frequency changes that have |
| 342 | * external effects. |
| 343 | */ |
| 344 | static void cpufreq_notify_transition(struct cpufreq_policy *policy, |
| 345 | struct cpufreq_freqs *freqs, |
| 346 | unsigned int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | { |
| 348 | BUG_ON(irqs_disabled()); |
| 349 | |
Dirk Brandewie | d5aaffa | 2013-01-17 16:22:21 +0000 | [diff] [blame] | 350 | if (cpufreq_disabled()) |
| 351 | return; |
| 352 | |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 353 | freqs->flags = cpufreq_driver->flags; |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 354 | pr_debug("notification %u of frequency transition to %u kHz\n", |
Joe Perches | e837f9b | 2014-03-11 10:03:00 -0700 | [diff] [blame] | 355 | state, freqs->new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | switch (state) { |
| 358 | case CPUFREQ_PRECHANGE: |
Viresh Kumar | 20b5324 | 2018-05-10 15:00:29 +0530 | [diff] [blame] | 359 | /* |
| 360 | * Detect if the driver reported a value as "old frequency" |
Dave Jones | e4472cb | 2006-01-31 15:53:55 -0800 | [diff] [blame] | 361 | * which is not equal to what the cpufreq core thinks is |
| 362 | * "old frequency". |
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 | if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { |
Viresh Kumar | 20b5324 | 2018-05-10 15:00:29 +0530 | [diff] [blame] | 365 | if (policy->cur && (policy->cur != freqs->old)) { |
Joe Perches | e837f9b | 2014-03-11 10:03:00 -0700 | [diff] [blame] | 366 | pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n", |
| 367 | freqs->old, policy->cur); |
Dave Jones | e4472cb | 2006-01-31 15:53:55 -0800 | [diff] [blame] | 368 | freqs->old = policy->cur; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | } |
| 370 | } |
Viresh Kumar | 20b5324 | 2018-05-10 15:00:29 +0530 | [diff] [blame] | 371 | |
| 372 | for_each_cpu(freqs->cpu, policy->cpus) { |
| 373 | srcu_notifier_call_chain(&cpufreq_transition_notifier_list, |
| 374 | CPUFREQ_PRECHANGE, freqs); |
| 375 | } |
| 376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | adjust_jiffies(CPUFREQ_PRECHANGE, freqs); |
| 378 | break; |
Dave Jones | e4472cb | 2006-01-31 15:53:55 -0800 | [diff] [blame] | 379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | case CPUFREQ_POSTCHANGE: |
| 381 | adjust_jiffies(CPUFREQ_POSTCHANGE, freqs); |
Viresh Kumar | 20b5324 | 2018-05-10 15:00:29 +0530 | [diff] [blame] | 382 | pr_debug("FREQ: %u - CPUs: %*pbl\n", freqs->new, |
| 383 | cpumask_pr_args(policy->cpus)); |
Viresh Kumar | bb176f7 | 2013-06-19 14:19:33 +0530 | [diff] [blame] | 384 | |
Viresh Kumar | 20b5324 | 2018-05-10 15:00:29 +0530 | [diff] [blame] | 385 | for_each_cpu(freqs->cpu, policy->cpus) { |
| 386 | trace_cpu_frequency(freqs->new, freqs->cpu); |
| 387 | srcu_notifier_call_chain(&cpufreq_transition_notifier_list, |
| 388 | CPUFREQ_POSTCHANGE, freqs); |
| 389 | } |
| 390 | |
| 391 | cpufreq_stats_record_transition(policy, freqs->new); |
| 392 | policy->cur = freqs->new; |
| 393 | } |
Viresh Kumar | b43a7ff | 2013-03-24 11:56:43 +0530 | [diff] [blame] | 394 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
Viresh Kumar | f7ba3b4 | 2013-12-02 11:04:12 +0530 | [diff] [blame] | 396 | /* Do post notifications when there are chances that transition has failed */ |
Viresh Kumar | 236a980 | 2014-03-24 13:35:46 +0530 | [diff] [blame] | 397 | static void cpufreq_notify_post_transition(struct cpufreq_policy *policy, |
Viresh Kumar | f7ba3b4 | 2013-12-02 11:04:12 +0530 | [diff] [blame] | 398 | struct cpufreq_freqs *freqs, int transition_failed) |
| 399 | { |
| 400 | cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); |
| 401 | if (!transition_failed) |
| 402 | return; |
| 403 | |
| 404 | swap(freqs->old, freqs->new); |
| 405 | cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); |
| 406 | cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); |
| 407 | } |
Viresh Kumar | f7ba3b4 | 2013-12-02 11:04:12 +0530 | [diff] [blame] | 408 | |
Srivatsa S. Bhat | 12478cf | 2014-03-24 13:35:44 +0530 | [diff] [blame] | 409 | void cpufreq_freq_transition_begin(struct cpufreq_policy *policy, |
| 410 | struct cpufreq_freqs *freqs) |
| 411 | { |
Srivatsa S. Bhat | ca654dc | 2014-05-05 12:52:39 +0530 | [diff] [blame] | 412 | |
| 413 | /* |
| 414 | * Catch double invocations of _begin() which lead to self-deadlock. |
| 415 | * ASYNC_NOTIFICATION drivers are left out because the cpufreq core |
| 416 | * doesn't invoke _begin() on their behalf, and hence the chances of |
| 417 | * double invocations are very low. Moreover, there are scenarios |
| 418 | * where these checks can emit false-positive warnings in these |
| 419 | * drivers; so we avoid that by skipping them altogether. |
| 420 | */ |
| 421 | WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION) |
| 422 | && current == policy->transition_task); |
| 423 | |
Srivatsa S. Bhat | 12478cf | 2014-03-24 13:35:44 +0530 | [diff] [blame] | 424 | wait: |
| 425 | wait_event(policy->transition_wait, !policy->transition_ongoing); |
| 426 | |
| 427 | spin_lock(&policy->transition_lock); |
| 428 | |
| 429 | if (unlikely(policy->transition_ongoing)) { |
| 430 | spin_unlock(&policy->transition_lock); |
| 431 | goto wait; |
| 432 | } |
| 433 | |
| 434 | policy->transition_ongoing = true; |
Srivatsa S. Bhat | ca654dc | 2014-05-05 12:52:39 +0530 | [diff] [blame] | 435 | policy->transition_task = current; |
Srivatsa S. Bhat | 12478cf | 2014-03-24 13:35:44 +0530 | [diff] [blame] | 436 | |
| 437 | spin_unlock(&policy->transition_lock); |
| 438 | |
| 439 | cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); |
| 440 | } |
| 441 | EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin); |
| 442 | |
| 443 | void cpufreq_freq_transition_end(struct cpufreq_policy *policy, |
| 444 | struct cpufreq_freqs *freqs, int transition_failed) |
| 445 | { |
Igor Stoppa | 0e7ea2f | 2018-09-07 19:09:55 +0300 | [diff] [blame] | 446 | if (WARN_ON(!policy->transition_ongoing)) |
Srivatsa S. Bhat | 12478cf | 2014-03-24 13:35:44 +0530 | [diff] [blame] | 447 | return; |
| 448 | |
| 449 | cpufreq_notify_post_transition(policy, freqs, transition_failed); |
| 450 | |
| 451 | policy->transition_ongoing = false; |
Srivatsa S. Bhat | ca654dc | 2014-05-05 12:52:39 +0530 | [diff] [blame] | 452 | policy->transition_task = NULL; |
Srivatsa S. Bhat | 12478cf | 2014-03-24 13:35:44 +0530 | [diff] [blame] | 453 | |
| 454 | wake_up(&policy->transition_wait); |
| 455 | } |
| 456 | EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end); |
| 457 | |
Rafael J. Wysocki | b7898fd | 2016-03-30 03:47:49 +0200 | [diff] [blame] | 458 | /* |
| 459 | * Fast frequency switching status count. Positive means "enabled", negative |
| 460 | * means "disabled" and 0 means "not decided yet". |
| 461 | */ |
| 462 | static int cpufreq_fast_switch_count; |
| 463 | static DEFINE_MUTEX(cpufreq_fast_switch_lock); |
| 464 | |
| 465 | static void cpufreq_list_transition_notifiers(void) |
| 466 | { |
| 467 | struct notifier_block *nb; |
| 468 | |
| 469 | pr_info("Registered transition notifiers:\n"); |
| 470 | |
| 471 | mutex_lock(&cpufreq_transition_notifier_list.mutex); |
| 472 | |
| 473 | for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next) |
| 474 | pr_info("%pF\n", nb->notifier_call); |
| 475 | |
| 476 | mutex_unlock(&cpufreq_transition_notifier_list.mutex); |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * cpufreq_enable_fast_switch - Enable fast frequency switching for policy. |
| 481 | * @policy: cpufreq policy to enable fast frequency switching for. |
| 482 | * |
| 483 | * Try to enable fast frequency switching for @policy. |
| 484 | * |
| 485 | * The attempt will fail if there is at least one transition notifier registered |
| 486 | * at this point, as fast frequency switching is quite fundamentally at odds |
| 487 | * with transition notifiers. Thus if successful, it will make registration of |
| 488 | * transition notifiers fail going forward. |
| 489 | */ |
| 490 | void cpufreq_enable_fast_switch(struct cpufreq_policy *policy) |
| 491 | { |
| 492 | lockdep_assert_held(&policy->rwsem); |
| 493 | |
| 494 | if (!policy->fast_switch_possible) |
| 495 | return; |
| 496 | |
| 497 | mutex_lock(&cpufreq_fast_switch_lock); |
| 498 | if (cpufreq_fast_switch_count >= 0) { |
| 499 | cpufreq_fast_switch_count++; |
| 500 | policy->fast_switch_enabled = true; |
| 501 | } else { |
| 502 | pr_warn("CPU%u: Fast frequency switching not enabled\n", |
| 503 | policy->cpu); |
| 504 | cpufreq_list_transition_notifiers(); |
| 505 | } |
| 506 | mutex_unlock(&cpufreq_fast_switch_lock); |
| 507 | } |
| 508 | EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch); |
| 509 | |
Rafael J. Wysocki | 6c9d9c8 | 2016-04-07 23:38:46 +0200 | [diff] [blame] | 510 | /** |
| 511 | * cpufreq_disable_fast_switch - Disable fast frequency switching for policy. |
| 512 | * @policy: cpufreq policy to disable fast frequency switching for. |
| 513 | */ |
| 514 | void cpufreq_disable_fast_switch(struct cpufreq_policy *policy) |
Rafael J. Wysocki | b7898fd | 2016-03-30 03:47:49 +0200 | [diff] [blame] | 515 | { |
| 516 | mutex_lock(&cpufreq_fast_switch_lock); |
| 517 | if (policy->fast_switch_enabled) { |
| 518 | policy->fast_switch_enabled = false; |
| 519 | if (!WARN_ON(cpufreq_fast_switch_count <= 0)) |
| 520 | cpufreq_fast_switch_count--; |
| 521 | } |
| 522 | mutex_unlock(&cpufreq_fast_switch_lock); |
| 523 | } |
Rafael J. Wysocki | 6c9d9c8 | 2016-04-07 23:38:46 +0200 | [diff] [blame] | 524 | EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | |
Steve Muckle | e3c0623 | 2016-07-13 13:25:25 -0700 | [diff] [blame] | 526 | /** |
| 527 | * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported |
| 528 | * one. |
| 529 | * @target_freq: target frequency to resolve. |
| 530 | * |
| 531 | * The target to driver frequency mapping is cached in the policy. |
| 532 | * |
| 533 | * Return: Lowest driver-supported frequency greater than or equal to the |
| 534 | * given target_freq, subject to policy (min/max) and driver limitations. |
| 535 | */ |
| 536 | unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy, |
| 537 | unsigned int target_freq) |
| 538 | { |
| 539 | target_freq = clamp_val(target_freq, policy->min, policy->max); |
| 540 | policy->cached_target_freq = target_freq; |
Viresh Kumar | abe8bd0 | 2016-07-21 14:39:26 -0700 | [diff] [blame] | 541 | |
| 542 | if (cpufreq_driver->target_index) { |
| 543 | int idx; |
| 544 | |
| 545 | idx = cpufreq_frequency_table_target(policy, target_freq, |
| 546 | CPUFREQ_RELATION_L); |
| 547 | policy->cached_resolved_idx = idx; |
| 548 | return policy->freq_table[idx].frequency; |
| 549 | } |
| 550 | |
Steve Muckle | e3c0623 | 2016-07-13 13:25:25 -0700 | [diff] [blame] | 551 | if (cpufreq_driver->resolve_freq) |
| 552 | return cpufreq_driver->resolve_freq(policy, target_freq); |
Viresh Kumar | abe8bd0 | 2016-07-21 14:39:26 -0700 | [diff] [blame] | 553 | |
| 554 | return target_freq; |
Steve Muckle | e3c0623 | 2016-07-13 13:25:25 -0700 | [diff] [blame] | 555 | } |
Steve Muckle | ae2c1ca | 2016-07-21 19:24:38 -0700 | [diff] [blame] | 556 | EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq); |
Steve Muckle | e3c0623 | 2016-07-13 13:25:25 -0700 | [diff] [blame] | 557 | |
Viresh Kumar | aa7519a | 2017-07-19 15:42:42 +0530 | [diff] [blame] | 558 | unsigned int cpufreq_policy_transition_delay_us(struct cpufreq_policy *policy) |
| 559 | { |
| 560 | unsigned int latency; |
| 561 | |
| 562 | if (policy->transition_delay_us) |
| 563 | return policy->transition_delay_us; |
| 564 | |
| 565 | latency = policy->cpuinfo.transition_latency / NSEC_PER_USEC; |
Viresh Kumar | e948bc8 | 2017-08-17 09:12:27 +0530 | [diff] [blame] | 566 | if (latency) { |
| 567 | /* |
| 568 | * For platforms that can change the frequency very fast (< 10 |
| 569 | * us), the above formula gives a decent transition delay. But |
| 570 | * for platforms where transition_latency is in milliseconds, it |
| 571 | * ends up giving unrealistic values. |
| 572 | * |
| 573 | * Cap the default transition delay to 10 ms, which seems to be |
| 574 | * a reasonable amount of time after which we should reevaluate |
| 575 | * the frequency. |
| 576 | */ |
| 577 | return min(latency * LATENCY_MULTIPLIER, (unsigned int)10000); |
| 578 | } |
Viresh Kumar | aa7519a | 2017-07-19 15:42:42 +0530 | [diff] [blame] | 579 | |
| 580 | return LATENCY_MULTIPLIER; |
| 581 | } |
| 582 | EXPORT_SYMBOL_GPL(cpufreq_policy_transition_delay_us); |
| 583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | /********************************************************************* |
| 585 | * SYSFS INTERFACE * |
| 586 | *********************************************************************/ |
Rashika Kheria | 8a5c74a | 2014-02-26 22:12:42 +0530 | [diff] [blame] | 587 | static ssize_t show_boost(struct kobject *kobj, |
Viresh Kumar | 625c85a | 2019-01-25 12:53:07 +0530 | [diff] [blame] | 588 | struct kobj_attribute *attr, char *buf) |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 589 | { |
| 590 | return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled); |
| 591 | } |
| 592 | |
Viresh Kumar | 625c85a | 2019-01-25 12:53:07 +0530 | [diff] [blame] | 593 | static ssize_t store_boost(struct kobject *kobj, struct kobj_attribute *attr, |
| 594 | const char *buf, size_t count) |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 595 | { |
| 596 | int ret, enable; |
| 597 | |
| 598 | ret = sscanf(buf, "%d", &enable); |
| 599 | if (ret != 1 || enable < 0 || enable > 1) |
| 600 | return -EINVAL; |
| 601 | |
| 602 | if (cpufreq_boost_trigger_state(enable)) { |
Joe Perches | e837f9b | 2014-03-11 10:03:00 -0700 | [diff] [blame] | 603 | pr_err("%s: Cannot %s BOOST!\n", |
| 604 | __func__, enable ? "enable" : "disable"); |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 605 | return -EINVAL; |
| 606 | } |
| 607 | |
Joe Perches | e837f9b | 2014-03-11 10:03:00 -0700 | [diff] [blame] | 608 | pr_debug("%s: cpufreq BOOST %s\n", |
| 609 | __func__, enable ? "enabled" : "disabled"); |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 610 | |
| 611 | return count; |
| 612 | } |
| 613 | define_one_global_rw(boost); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | |
Viresh Kumar | 42f91fa | 2015-01-02 12:34:26 +0530 | [diff] [blame] | 615 | static struct cpufreq_governor *find_governor(const char *str_governor) |
Jeremy Fitzhardinge | 3bcb09a | 2006-07-06 12:30:26 -0700 | [diff] [blame] | 616 | { |
| 617 | struct cpufreq_governor *t; |
| 618 | |
Viresh Kumar | f7b2706 | 2015-01-27 14:06:09 +0530 | [diff] [blame] | 619 | for_each_governor(t) |
Rasmus Villemoes | 7c4f453 | 2014-09-29 15:50:11 +0200 | [diff] [blame] | 620 | if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN)) |
Jeremy Fitzhardinge | 3bcb09a | 2006-07-06 12:30:26 -0700 | [diff] [blame] | 621 | return t; |
| 622 | |
| 623 | return NULL; |
| 624 | } |
| 625 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | /** |
| 627 | * cpufreq_parse_governor - parse a governor string |
| 628 | */ |
Rafael J. Wysocki | ae0ff89 | 2017-11-23 01:24:05 +0100 | [diff] [blame] | 629 | static int cpufreq_parse_governor(char *str_governor, |
| 630 | struct cpufreq_policy *policy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | { |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 632 | if (cpufreq_driver->setpolicy) { |
Rasmus Villemoes | 7c4f453 | 2014-09-29 15:50:11 +0200 | [diff] [blame] | 633 | if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) { |
Rafael J. Wysocki | ae0ff89 | 2017-11-23 01:24:05 +0100 | [diff] [blame] | 634 | policy->policy = CPUFREQ_POLICY_PERFORMANCE; |
Rafael J. Wysocki | 045149e | 2017-11-23 01:23:16 +0100 | [diff] [blame] | 635 | return 0; |
| 636 | } |
| 637 | |
| 638 | if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) { |
Rafael J. Wysocki | ae0ff89 | 2017-11-23 01:24:05 +0100 | [diff] [blame] | 639 | policy->policy = CPUFREQ_POLICY_POWERSAVE; |
Rafael J. Wysocki | 045149e | 2017-11-23 01:23:16 +0100 | [diff] [blame] | 640 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | } |
Viresh Kumar | 2e1cc3a | 2015-01-02 12:34:27 +0530 | [diff] [blame] | 642 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | struct cpufreq_governor *t; |
Jeremy Fitzhardinge | 3bcb09a | 2006-07-06 12:30:26 -0700 | [diff] [blame] | 644 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 645 | mutex_lock(&cpufreq_governor_mutex); |
Jeremy Fitzhardinge | 3bcb09a | 2006-07-06 12:30:26 -0700 | [diff] [blame] | 646 | |
Viresh Kumar | 42f91fa | 2015-01-02 12:34:26 +0530 | [diff] [blame] | 647 | t = find_governor(str_governor); |
Rafael J. Wysocki | 045149e | 2017-11-23 01:23:16 +0100 | [diff] [blame] | 648 | if (!t) { |
Kees Cook | 1a8e146 | 2011-05-04 08:38:56 -0700 | [diff] [blame] | 649 | int ret; |
Jeremy Fitzhardinge | ea71497 | 2006-07-06 12:32:01 -0700 | [diff] [blame] | 650 | |
Kees Cook | 1a8e146 | 2011-05-04 08:38:56 -0700 | [diff] [blame] | 651 | mutex_unlock(&cpufreq_governor_mutex); |
Rafael J. Wysocki | 045149e | 2017-11-23 01:23:16 +0100 | [diff] [blame] | 652 | |
Kees Cook | 1a8e146 | 2011-05-04 08:38:56 -0700 | [diff] [blame] | 653 | ret = request_module("cpufreq_%s", str_governor); |
Rafael J. Wysocki | 045149e | 2017-11-23 01:23:16 +0100 | [diff] [blame] | 654 | if (ret) |
| 655 | return -EINVAL; |
| 656 | |
Kees Cook | 1a8e146 | 2011-05-04 08:38:56 -0700 | [diff] [blame] | 657 | mutex_lock(&cpufreq_governor_mutex); |
Jeremy Fitzhardinge | ea71497 | 2006-07-06 12:32:01 -0700 | [diff] [blame] | 658 | |
Rafael J. Wysocki | 045149e | 2017-11-23 01:23:16 +0100 | [diff] [blame] | 659 | t = find_governor(str_governor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | } |
Rafael J. Wysocki | a8b149d | 2017-11-23 14:27:07 +0100 | [diff] [blame] | 661 | if (t && !try_module_get(t->owner)) |
| 662 | t = NULL; |
Jeremy Fitzhardinge | 3bcb09a | 2006-07-06 12:30:26 -0700 | [diff] [blame] | 663 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 664 | mutex_unlock(&cpufreq_governor_mutex); |
Rafael J. Wysocki | 045149e | 2017-11-23 01:23:16 +0100 | [diff] [blame] | 665 | |
| 666 | if (t) { |
Rafael J. Wysocki | ae0ff89 | 2017-11-23 01:24:05 +0100 | [diff] [blame] | 667 | policy->governor = t; |
Rafael J. Wysocki | 045149e | 2017-11-23 01:23:16 +0100 | [diff] [blame] | 668 | return 0; |
| 669 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | } |
Rafael J. Wysocki | 045149e | 2017-11-23 01:23:16 +0100 | [diff] [blame] | 671 | |
| 672 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | /** |
Gautham R Shenoy | e08f5f5 | 2006-10-26 16:20:58 +0530 | [diff] [blame] | 676 | * cpufreq_per_cpu_attr_read() / show_##file_name() - |
| 677 | * print out cpufreq information |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | * |
| 679 | * Write out information from cpufreq_driver->policy[cpu]; object must be |
| 680 | * "unsigned int". |
| 681 | */ |
| 682 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 683 | #define show_one(file_name, object) \ |
| 684 | static ssize_t show_##file_name \ |
Dave Jones | 905d77c | 2008-03-05 14:28:32 -0500 | [diff] [blame] | 685 | (struct cpufreq_policy *policy, char *buf) \ |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 686 | { \ |
Dave Jones | 29464f2 | 2009-01-18 01:37:11 -0500 | [diff] [blame] | 687 | return sprintf(buf, "%u\n", policy->object); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | show_one(cpuinfo_min_freq, cpuinfo.min_freq); |
| 691 | show_one(cpuinfo_max_freq, cpuinfo.max_freq); |
Thomas Renninger | ed12978 | 2009-02-04 01:17:41 +0100 | [diff] [blame] | 692 | show_one(cpuinfo_transition_latency, cpuinfo.transition_latency); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | show_one(scaling_min_freq, min); |
| 694 | show_one(scaling_max_freq, max); |
Dirk Brandewie | c034b02 | 2014-10-13 08:37:40 -0700 | [diff] [blame] | 695 | |
Len Brown | f8475ce | 2017-06-23 22:11:52 -0700 | [diff] [blame] | 696 | __weak unsigned int arch_freq_get_on_cpu(int cpu) |
| 697 | { |
| 698 | return 0; |
| 699 | } |
| 700 | |
Viresh Kumar | 09347b2 | 2015-01-02 12:34:24 +0530 | [diff] [blame] | 701 | static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf) |
Dirk Brandewie | c034b02 | 2014-10-13 08:37:40 -0700 | [diff] [blame] | 702 | { |
| 703 | ssize_t ret; |
Len Brown | f8475ce | 2017-06-23 22:11:52 -0700 | [diff] [blame] | 704 | unsigned int freq; |
Dirk Brandewie | c034b02 | 2014-10-13 08:37:40 -0700 | [diff] [blame] | 705 | |
Len Brown | f8475ce | 2017-06-23 22:11:52 -0700 | [diff] [blame] | 706 | freq = arch_freq_get_on_cpu(policy->cpu); |
| 707 | if (freq) |
| 708 | ret = sprintf(buf, "%u\n", freq); |
| 709 | else if (cpufreq_driver && cpufreq_driver->setpolicy && |
| 710 | cpufreq_driver->get) |
Dirk Brandewie | c034b02 | 2014-10-13 08:37:40 -0700 | [diff] [blame] | 711 | ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu)); |
| 712 | else |
| 713 | ret = sprintf(buf, "%u\n", policy->cur); |
| 714 | return ret; |
| 715 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | |
Viresh Kumar | 037ce83 | 2013-10-02 14:13:16 +0530 | [diff] [blame] | 717 | static int cpufreq_set_policy(struct cpufreq_policy *policy, |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 718 | struct cpufreq_policy *new_policy); |
Thomas Renninger | 7970e08 | 2006-04-13 15:14:04 +0200 | [diff] [blame] | 719 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | /** |
| 721 | * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access |
| 722 | */ |
| 723 | #define store_one(file_name, object) \ |
| 724 | static ssize_t store_##file_name \ |
Dave Jones | 905d77c | 2008-03-05 14:28:32 -0500 | [diff] [blame] | 725 | (struct cpufreq_policy *policy, const char *buf, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | { \ |
Vince Hsu | 619c144c | 2014-11-10 14:14:50 +0800 | [diff] [blame] | 727 | int ret, temp; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | struct cpufreq_policy new_policy; \ |
| 729 | \ |
Viresh Kumar | 8fa5b63 | 2015-08-03 08:36:15 +0530 | [diff] [blame] | 730 | memcpy(&new_policy, policy, sizeof(*policy)); \ |
Tao Wang | c7d1f11 | 2018-05-26 15:16:48 +0800 | [diff] [blame] | 731 | new_policy.min = policy->user_policy.min; \ |
| 732 | new_policy.max = policy->user_policy.max; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | \ |
Dave Jones | 29464f2 | 2009-01-18 01:37:11 -0500 | [diff] [blame] | 734 | ret = sscanf(buf, "%u", &new_policy.object); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | if (ret != 1) \ |
| 736 | return -EINVAL; \ |
| 737 | \ |
Vince Hsu | 619c144c | 2014-11-10 14:14:50 +0800 | [diff] [blame] | 738 | temp = new_policy.object; \ |
Viresh Kumar | 037ce83 | 2013-10-02 14:13:16 +0530 | [diff] [blame] | 739 | ret = cpufreq_set_policy(policy, &new_policy); \ |
Vince Hsu | 619c144c | 2014-11-10 14:14:50 +0800 | [diff] [blame] | 740 | if (!ret) \ |
| 741 | policy->user_policy.object = temp; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | \ |
| 743 | return ret ? ret : count; \ |
| 744 | } |
| 745 | |
Dave Jones | 29464f2 | 2009-01-18 01:37:11 -0500 | [diff] [blame] | 746 | store_one(scaling_min_freq, min); |
| 747 | store_one(scaling_max_freq, max); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | |
| 749 | /** |
| 750 | * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware |
| 751 | */ |
Dave Jones | 905d77c | 2008-03-05 14:28:32 -0500 | [diff] [blame] | 752 | static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy, |
| 753 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | { |
Viresh Kumar | d92d50a | 2015-01-02 12:34:29 +0530 | [diff] [blame] | 755 | unsigned int cur_freq = __cpufreq_get(policy); |
Rafael J. Wysocki | 9b4f603 | 2017-03-15 00:12:16 +0100 | [diff] [blame] | 756 | |
| 757 | if (cur_freq) |
| 758 | return sprintf(buf, "%u\n", cur_freq); |
| 759 | |
| 760 | return sprintf(buf, "<unknown>\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | } |
| 762 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | /** |
| 764 | * show_scaling_governor - show the current policy for the specified CPU |
| 765 | */ |
Dave Jones | 905d77c | 2008-03-05 14:28:32 -0500 | [diff] [blame] | 766 | static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | { |
Dave Jones | 29464f2 | 2009-01-18 01:37:11 -0500 | [diff] [blame] | 768 | if (policy->policy == CPUFREQ_POLICY_POWERSAVE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | return sprintf(buf, "powersave\n"); |
| 770 | else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) |
| 771 | return sprintf(buf, "performance\n"); |
| 772 | else if (policy->governor) |
viresh kumar | 4b972f0 | 2012-10-23 01:23:43 +0200 | [diff] [blame] | 773 | return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", |
Dave Jones | 29464f2 | 2009-01-18 01:37:11 -0500 | [diff] [blame] | 774 | policy->governor->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | return -EINVAL; |
| 776 | } |
| 777 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | /** |
| 779 | * store_scaling_governor - store policy for the specified CPU |
| 780 | */ |
Dave Jones | 905d77c | 2008-03-05 14:28:32 -0500 | [diff] [blame] | 781 | static ssize_t store_scaling_governor(struct cpufreq_policy *policy, |
| 782 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | { |
Srivatsa S. Bhat | 5136fa5 | 2013-09-07 01:24:06 +0530 | [diff] [blame] | 784 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | char str_governor[16]; |
| 786 | struct cpufreq_policy new_policy; |
| 787 | |
Viresh Kumar | 8fa5b63 | 2015-08-03 08:36:15 +0530 | [diff] [blame] | 788 | memcpy(&new_policy, policy, sizeof(*policy)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | |
Dave Jones | 29464f2 | 2009-01-18 01:37:11 -0500 | [diff] [blame] | 790 | ret = sscanf(buf, "%15s", str_governor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | if (ret != 1) |
| 792 | return -EINVAL; |
| 793 | |
Rafael J. Wysocki | ae0ff89 | 2017-11-23 01:24:05 +0100 | [diff] [blame] | 794 | if (cpufreq_parse_governor(str_governor, &new_policy)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | return -EINVAL; |
| 796 | |
Viresh Kumar | 037ce83 | 2013-10-02 14:13:16 +0530 | [diff] [blame] | 797 | ret = cpufreq_set_policy(policy, &new_policy); |
Rafael J. Wysocki | a8b149d | 2017-11-23 14:27:07 +0100 | [diff] [blame] | 798 | |
| 799 | if (new_policy.governor) |
| 800 | module_put(new_policy.governor->owner); |
| 801 | |
Viresh Kumar | 88dc438 | 2015-08-03 08:36:18 +0530 | [diff] [blame] | 802 | return ret ? ret : count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | /** |
| 806 | * show_scaling_driver - show the cpufreq driver currently loaded |
| 807 | */ |
Dave Jones | 905d77c | 2008-03-05 14:28:32 -0500 | [diff] [blame] | 808 | static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | { |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 810 | return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | /** |
| 814 | * show_scaling_available_governors - show the available CPUfreq governors |
| 815 | */ |
Dave Jones | 905d77c | 2008-03-05 14:28:32 -0500 | [diff] [blame] | 816 | static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy, |
| 817 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | { |
| 819 | ssize_t i = 0; |
| 820 | struct cpufreq_governor *t; |
| 821 | |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 822 | if (!has_target()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | i += sprintf(buf, "performance powersave"); |
| 824 | goto out; |
| 825 | } |
| 826 | |
Viresh Kumar | f7b2706 | 2015-01-27 14:06:09 +0530 | [diff] [blame] | 827 | for_each_governor(t) { |
Dave Jones | 29464f2 | 2009-01-18 01:37:11 -0500 | [diff] [blame] | 828 | if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) |
| 829 | - (CPUFREQ_NAME_LEN + 2))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | goto out; |
viresh kumar | 4b972f0 | 2012-10-23 01:23:43 +0200 | [diff] [blame] | 831 | i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | } |
Dave Jones | 7d5e350 | 2006-02-02 17:03:42 -0500 | [diff] [blame] | 833 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | i += sprintf(&buf[i], "\n"); |
| 835 | return i; |
| 836 | } |
Darrick J. Wong | e8628dd | 2008-04-18 13:31:12 -0700 | [diff] [blame] | 837 | |
Lan Tianyu | f4fd379 | 2013-06-27 15:08:54 +0800 | [diff] [blame] | 838 | ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | { |
| 840 | ssize_t i = 0; |
| 841 | unsigned int cpu; |
| 842 | |
Rusty Russell | 835481d | 2009-01-04 05:18:06 -0800 | [diff] [blame] | 843 | for_each_cpu(cpu, mask) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | if (i) |
| 845 | i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " "); |
| 846 | i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu); |
| 847 | if (i >= (PAGE_SIZE - 5)) |
Dave Jones | 29464f2 | 2009-01-18 01:37:11 -0500 | [diff] [blame] | 848 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | } |
| 850 | i += sprintf(&buf[i], "\n"); |
| 851 | return i; |
| 852 | } |
Lan Tianyu | f4fd379 | 2013-06-27 15:08:54 +0800 | [diff] [blame] | 853 | EXPORT_SYMBOL_GPL(cpufreq_show_cpus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | |
Darrick J. Wong | e8628dd | 2008-04-18 13:31:12 -0700 | [diff] [blame] | 855 | /** |
| 856 | * show_related_cpus - show the CPUs affected by each transition even if |
| 857 | * hw coordination is in use |
| 858 | */ |
| 859 | static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf) |
| 860 | { |
Lan Tianyu | f4fd379 | 2013-06-27 15:08:54 +0800 | [diff] [blame] | 861 | return cpufreq_show_cpus(policy->related_cpus, buf); |
Darrick J. Wong | e8628dd | 2008-04-18 13:31:12 -0700 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | /** |
| 865 | * show_affected_cpus - show the CPUs affected by each transition |
| 866 | */ |
| 867 | static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf) |
| 868 | { |
Lan Tianyu | f4fd379 | 2013-06-27 15:08:54 +0800 | [diff] [blame] | 869 | return cpufreq_show_cpus(policy->cpus, buf); |
Darrick J. Wong | e8628dd | 2008-04-18 13:31:12 -0700 | [diff] [blame] | 870 | } |
| 871 | |
Venki Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 872 | static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy, |
Dave Jones | 905d77c | 2008-03-05 14:28:32 -0500 | [diff] [blame] | 873 | const char *buf, size_t count) |
Venki Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 874 | { |
| 875 | unsigned int freq = 0; |
| 876 | unsigned int ret; |
| 877 | |
CHIKAMA masaki | 879000f | 2008-06-05 22:46:33 -0700 | [diff] [blame] | 878 | if (!policy->governor || !policy->governor->store_setspeed) |
Venki Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 879 | return -EINVAL; |
| 880 | |
| 881 | ret = sscanf(buf, "%u", &freq); |
| 882 | if (ret != 1) |
| 883 | return -EINVAL; |
| 884 | |
| 885 | policy->governor->store_setspeed(policy, freq); |
| 886 | |
| 887 | return count; |
| 888 | } |
| 889 | |
| 890 | static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf) |
| 891 | { |
CHIKAMA masaki | 879000f | 2008-06-05 22:46:33 -0700 | [diff] [blame] | 892 | if (!policy->governor || !policy->governor->show_setspeed) |
Venki Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 893 | return sprintf(buf, "<unsupported>\n"); |
| 894 | |
| 895 | return policy->governor->show_setspeed(policy, buf); |
| 896 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | |
Thomas Renninger | e2f74f3 | 2009-11-19 12:31:01 +0100 | [diff] [blame] | 898 | /** |
viresh kumar | 8bf1ac72 | 2012-10-23 01:23:33 +0200 | [diff] [blame] | 899 | * show_bios_limit - show the current cpufreq HW/BIOS limitation |
Thomas Renninger | e2f74f3 | 2009-11-19 12:31:01 +0100 | [diff] [blame] | 900 | */ |
| 901 | static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf) |
| 902 | { |
| 903 | unsigned int limit; |
| 904 | int ret; |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 905 | if (cpufreq_driver->bios_limit) { |
| 906 | ret = cpufreq_driver->bios_limit(policy->cpu, &limit); |
Thomas Renninger | e2f74f3 | 2009-11-19 12:31:01 +0100 | [diff] [blame] | 907 | if (!ret) |
| 908 | return sprintf(buf, "%u\n", limit); |
| 909 | } |
| 910 | return sprintf(buf, "%u\n", policy->cpuinfo.max_freq); |
| 911 | } |
| 912 | |
Borislav Petkov | 6dad2a2 | 2010-03-31 21:56:46 +0200 | [diff] [blame] | 913 | cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400); |
| 914 | cpufreq_freq_attr_ro(cpuinfo_min_freq); |
| 915 | cpufreq_freq_attr_ro(cpuinfo_max_freq); |
| 916 | cpufreq_freq_attr_ro(cpuinfo_transition_latency); |
| 917 | cpufreq_freq_attr_ro(scaling_available_governors); |
| 918 | cpufreq_freq_attr_ro(scaling_driver); |
| 919 | cpufreq_freq_attr_ro(scaling_cur_freq); |
| 920 | cpufreq_freq_attr_ro(bios_limit); |
| 921 | cpufreq_freq_attr_ro(related_cpus); |
| 922 | cpufreq_freq_attr_ro(affected_cpus); |
| 923 | cpufreq_freq_attr_rw(scaling_min_freq); |
| 924 | cpufreq_freq_attr_rw(scaling_max_freq); |
| 925 | cpufreq_freq_attr_rw(scaling_governor); |
| 926 | cpufreq_freq_attr_rw(scaling_setspeed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | |
Dave Jones | 905d77c | 2008-03-05 14:28:32 -0500 | [diff] [blame] | 928 | static struct attribute *default_attrs[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | &cpuinfo_min_freq.attr, |
| 930 | &cpuinfo_max_freq.attr, |
Thomas Renninger | ed12978 | 2009-02-04 01:17:41 +0100 | [diff] [blame] | 931 | &cpuinfo_transition_latency.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | &scaling_min_freq.attr, |
| 933 | &scaling_max_freq.attr, |
| 934 | &affected_cpus.attr, |
Darrick J. Wong | e8628dd | 2008-04-18 13:31:12 -0700 | [diff] [blame] | 935 | &related_cpus.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | &scaling_governor.attr, |
| 937 | &scaling_driver.attr, |
| 938 | &scaling_available_governors.attr, |
Venki Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 939 | &scaling_setspeed.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | NULL |
| 941 | }; |
| 942 | |
Dave Jones | 29464f2 | 2009-01-18 01:37:11 -0500 | [diff] [blame] | 943 | #define to_policy(k) container_of(k, struct cpufreq_policy, kobj) |
| 944 | #define to_attr(a) container_of(a, struct freq_attr, attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | |
Dave Jones | 29464f2 | 2009-01-18 01:37:11 -0500 | [diff] [blame] | 946 | static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | { |
Dave Jones | 905d77c | 2008-03-05 14:28:32 -0500 | [diff] [blame] | 948 | struct cpufreq_policy *policy = to_policy(kobj); |
| 949 | struct freq_attr *fattr = to_attr(attr); |
Viresh Kumar | 1b750e3 | 2013-10-02 14:13:09 +0530 | [diff] [blame] | 950 | ssize_t ret; |
Viresh Kumar | 6eed940 | 2013-08-06 22:53:11 +0530 | [diff] [blame] | 951 | |
viresh kumar | ad7722d | 2013-10-18 19:10:15 +0530 | [diff] [blame] | 952 | down_read(&policy->rwsem); |
Rafael J. Wysocki | 6541aef | 2016-02-12 23:56:21 +0100 | [diff] [blame] | 953 | ret = fattr->show(policy, buf); |
viresh kumar | ad7722d | 2013-10-18 19:10:15 +0530 | [diff] [blame] | 954 | up_read(&policy->rwsem); |
Viresh Kumar | 1b750e3 | 2013-10-02 14:13:09 +0530 | [diff] [blame] | 955 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | return ret; |
| 957 | } |
| 958 | |
Dave Jones | 905d77c | 2008-03-05 14:28:32 -0500 | [diff] [blame] | 959 | static ssize_t store(struct kobject *kobj, struct attribute *attr, |
| 960 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | { |
Dave Jones | 905d77c | 2008-03-05 14:28:32 -0500 | [diff] [blame] | 962 | struct cpufreq_policy *policy = to_policy(kobj); |
| 963 | struct freq_attr *fattr = to_attr(attr); |
Dave Jones | a07530b | 2008-03-05 14:22:25 -0500 | [diff] [blame] | 964 | ssize_t ret = -EINVAL; |
Viresh Kumar | 6eed940 | 2013-08-06 22:53:11 +0530 | [diff] [blame] | 965 | |
Waiman Long | 9b3d9bb | 2018-07-24 14:26:05 -0400 | [diff] [blame] | 966 | /* |
| 967 | * cpus_read_trylock() is used here to work around a circular lock |
| 968 | * dependency problem with respect to the cpufreq_register_driver(). |
| 969 | */ |
| 970 | if (!cpus_read_trylock()) |
| 971 | return -EBUSY; |
Srivatsa S. Bhat | 4f750c9 | 2013-09-07 01:23:43 +0530 | [diff] [blame] | 972 | |
Rafael J. Wysocki | 6541aef | 2016-02-12 23:56:21 +0100 | [diff] [blame] | 973 | if (cpu_online(policy->cpu)) { |
| 974 | down_write(&policy->rwsem); |
Gautham R Shenoy | e08f5f5 | 2006-10-26 16:20:58 +0530 | [diff] [blame] | 975 | ret = fattr->store(policy, buf, count); |
Rafael J. Wysocki | 6541aef | 2016-02-12 23:56:21 +0100 | [diff] [blame] | 976 | up_write(&policy->rwsem); |
| 977 | } |
Gautham R Shenoy | e08f5f5 | 2006-10-26 16:20:58 +0530 | [diff] [blame] | 978 | |
Sebastian Andrzej Siewior | a92551e | 2017-05-24 10:15:20 +0200 | [diff] [blame] | 979 | cpus_read_unlock(); |
Srivatsa S. Bhat | 4f750c9 | 2013-09-07 01:23:43 +0530 | [diff] [blame] | 980 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | return ret; |
| 982 | } |
| 983 | |
Dave Jones | 905d77c | 2008-03-05 14:28:32 -0500 | [diff] [blame] | 984 | static void cpufreq_sysfs_release(struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | { |
Dave Jones | 905d77c | 2008-03-05 14:28:32 -0500 | [diff] [blame] | 986 | struct cpufreq_policy *policy = to_policy(kobj); |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 987 | pr_debug("last reference is dropped\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | complete(&policy->kobj_unregister); |
| 989 | } |
| 990 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 991 | static const struct sysfs_ops sysfs_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | .show = show, |
| 993 | .store = store, |
| 994 | }; |
| 995 | |
| 996 | static struct kobj_type ktype_cpufreq = { |
| 997 | .sysfs_ops = &sysfs_ops, |
| 998 | .default_attrs = default_attrs, |
| 999 | .release = cpufreq_sysfs_release, |
| 1000 | }; |
| 1001 | |
Rafael J. Wysocki | 2f0ba79 | 2017-03-27 19:33:09 +0200 | [diff] [blame] | 1002 | static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu) |
Viresh Kumar | 8754914 | 2015-06-10 02:13:21 +0200 | [diff] [blame] | 1003 | { |
Rafael J. Wysocki | 2f0ba79 | 2017-03-27 19:33:09 +0200 | [diff] [blame] | 1004 | struct device *dev = get_cpu_device(cpu); |
| 1005 | |
| 1006 | if (!dev) |
| 1007 | return; |
| 1008 | |
| 1009 | if (cpumask_test_and_set_cpu(cpu, policy->real_cpus)) |
| 1010 | return; |
| 1011 | |
Viresh Kumar | 26619804 | 2016-09-12 12:07:05 +0530 | [diff] [blame] | 1012 | dev_dbg(dev, "%s: Adding symlink\n", __func__); |
Rafael J. Wysocki | 2f0ba79 | 2017-03-27 19:33:09 +0200 | [diff] [blame] | 1013 | if (sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq")) |
| 1014 | dev_err(dev, "cpufreq symlink creation failed\n"); |
Viresh Kumar | 8754914 | 2015-06-10 02:13:21 +0200 | [diff] [blame] | 1015 | } |
| 1016 | |
Viresh Kumar | 26619804 | 2016-09-12 12:07:05 +0530 | [diff] [blame] | 1017 | static void remove_cpu_dev_symlink(struct cpufreq_policy *policy, |
| 1018 | struct device *dev) |
Viresh Kumar | 8754914 | 2015-06-10 02:13:21 +0200 | [diff] [blame] | 1019 | { |
Viresh Kumar | 26619804 | 2016-09-12 12:07:05 +0530 | [diff] [blame] | 1020 | dev_dbg(dev, "%s: Removing symlink\n", __func__); |
| 1021 | sysfs_remove_link(&dev->kobj, "cpufreq"); |
Viresh Kumar | 8754914 | 2015-06-10 02:13:21 +0200 | [diff] [blame] | 1022 | } |
| 1023 | |
Rafael J. Wysocki | d9612a4 | 2015-07-27 23:11:37 +0200 | [diff] [blame] | 1024 | static int cpufreq_add_dev_interface(struct cpufreq_policy *policy) |
Dave Jones | 909a694 | 2009-07-08 18:05:42 -0400 | [diff] [blame] | 1025 | { |
| 1026 | struct freq_attr **drv_attr; |
Dave Jones | 909a694 | 2009-07-08 18:05:42 -0400 | [diff] [blame] | 1027 | int ret = 0; |
Dave Jones | 909a694 | 2009-07-08 18:05:42 -0400 | [diff] [blame] | 1028 | |
Dave Jones | 909a694 | 2009-07-08 18:05:42 -0400 | [diff] [blame] | 1029 | /* set up files for this cpu device */ |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 1030 | drv_attr = cpufreq_driver->attr; |
Viresh Kumar | f13f118 | 2015-01-02 12:34:23 +0530 | [diff] [blame] | 1031 | while (drv_attr && *drv_attr) { |
Dave Jones | 909a694 | 2009-07-08 18:05:42 -0400 | [diff] [blame] | 1032 | ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr)); |
| 1033 | if (ret) |
Tomeu Vizoso | 6d4e81e | 2014-11-24 10:08:03 +0100 | [diff] [blame] | 1034 | return ret; |
Dave Jones | 909a694 | 2009-07-08 18:05:42 -0400 | [diff] [blame] | 1035 | drv_attr++; |
| 1036 | } |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 1037 | if (cpufreq_driver->get) { |
Dave Jones | 909a694 | 2009-07-08 18:05:42 -0400 | [diff] [blame] | 1038 | ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr); |
| 1039 | if (ret) |
Tomeu Vizoso | 6d4e81e | 2014-11-24 10:08:03 +0100 | [diff] [blame] | 1040 | return ret; |
Dave Jones | 909a694 | 2009-07-08 18:05:42 -0400 | [diff] [blame] | 1041 | } |
Dirk Brandewie | c034b02 | 2014-10-13 08:37:40 -0700 | [diff] [blame] | 1042 | |
| 1043 | ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr); |
| 1044 | if (ret) |
Tomeu Vizoso | 6d4e81e | 2014-11-24 10:08:03 +0100 | [diff] [blame] | 1045 | return ret; |
Dirk Brandewie | c034b02 | 2014-10-13 08:37:40 -0700 | [diff] [blame] | 1046 | |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 1047 | if (cpufreq_driver->bios_limit) { |
Thomas Renninger | e2f74f3 | 2009-11-19 12:31:01 +0100 | [diff] [blame] | 1048 | ret = sysfs_create_file(&policy->kobj, &bios_limit.attr); |
| 1049 | if (ret) |
Tomeu Vizoso | 6d4e81e | 2014-11-24 10:08:03 +0100 | [diff] [blame] | 1050 | return ret; |
Thomas Renninger | e2f74f3 | 2009-11-19 12:31:01 +0100 | [diff] [blame] | 1051 | } |
Dave Jones | 909a694 | 2009-07-08 18:05:42 -0400 | [diff] [blame] | 1052 | |
Viresh Kumar | 26619804 | 2016-09-12 12:07:05 +0530 | [diff] [blame] | 1053 | return 0; |
Srivatsa S. Bhat | e18f168 | 2013-07-30 04:24:23 +0530 | [diff] [blame] | 1054 | } |
| 1055 | |
Rafael J. Wysocki | de1df26 | 2016-02-05 02:37:42 +0100 | [diff] [blame] | 1056 | __weak struct cpufreq_governor *cpufreq_default_governor(void) |
| 1057 | { |
| 1058 | return NULL; |
| 1059 | } |
| 1060 | |
Viresh Kumar | 7f0fa40 | 2015-07-08 15:12:16 +0530 | [diff] [blame] | 1061 | static int cpufreq_init_policy(struct cpufreq_policy *policy) |
Srivatsa S. Bhat | e18f168 | 2013-07-30 04:24:23 +0530 | [diff] [blame] | 1062 | { |
viresh kumar | 6e2c89d | 2014-03-04 11:43:59 +0800 | [diff] [blame] | 1063 | struct cpufreq_governor *gov = NULL; |
Srivatsa S. Bhat | e18f168 | 2013-07-30 04:24:23 +0530 | [diff] [blame] | 1064 | struct cpufreq_policy new_policy; |
Srivatsa S. Bhat | e18f168 | 2013-07-30 04:24:23 +0530 | [diff] [blame] | 1065 | |
Viresh Kumar | d5b73cd | 2013-08-06 22:53:06 +0530 | [diff] [blame] | 1066 | memcpy(&new_policy, policy, sizeof(*policy)); |
Jason Baron | a27a9ab | 2013-12-19 22:50:50 +0000 | [diff] [blame] | 1067 | |
viresh kumar | 6e2c89d | 2014-03-04 11:43:59 +0800 | [diff] [blame] | 1068 | /* Update governor of new_policy to the governor used before hotplug */ |
Viresh Kumar | 4573237 | 2015-05-12 12:22:34 +0530 | [diff] [blame] | 1069 | gov = find_governor(policy->last_governor); |
Rafael J. Wysocki | de1df26 | 2016-02-05 02:37:42 +0100 | [diff] [blame] | 1070 | if (gov) { |
viresh kumar | 6e2c89d | 2014-03-04 11:43:59 +0800 | [diff] [blame] | 1071 | pr_debug("Restoring governor %s for cpu %d\n", |
| 1072 | policy->governor->name, policy->cpu); |
Rafael J. Wysocki | de1df26 | 2016-02-05 02:37:42 +0100 | [diff] [blame] | 1073 | } else { |
| 1074 | gov = cpufreq_default_governor(); |
| 1075 | if (!gov) |
| 1076 | return -ENODATA; |
| 1077 | } |
viresh kumar | 6e2c89d | 2014-03-04 11:43:59 +0800 | [diff] [blame] | 1078 | |
| 1079 | new_policy.governor = gov; |
| 1080 | |
Srinivas Pandruvada | 69030dd1 | 2015-12-01 16:52:14 -0800 | [diff] [blame] | 1081 | /* Use the default policy if there is no last_policy. */ |
| 1082 | if (cpufreq_driver->setpolicy) { |
| 1083 | if (policy->last_policy) |
| 1084 | new_policy.policy = policy->last_policy; |
| 1085 | else |
Rafael J. Wysocki | ae0ff89 | 2017-11-23 01:24:05 +0100 | [diff] [blame] | 1086 | cpufreq_parse_governor(gov->name, &new_policy); |
Srinivas Pandruvada | 69030dd1 | 2015-12-01 16:52:14 -0800 | [diff] [blame] | 1087 | } |
Dave Jones | ecf7e46 | 2009-07-08 18:48:47 -0400 | [diff] [blame] | 1088 | /* set default policy */ |
Viresh Kumar | 7f0fa40 | 2015-07-08 15:12:16 +0530 | [diff] [blame] | 1089 | return cpufreq_set_policy(policy, &new_policy); |
Dave Jones | 909a694 | 2009-07-08 18:05:42 -0400 | [diff] [blame] | 1090 | } |
| 1091 | |
Rafael J. Wysocki | d9612a4 | 2015-07-27 23:11:37 +0200 | [diff] [blame] | 1092 | static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu) |
Viresh Kumar | fcf8058 | 2013-01-29 14:39:08 +0000 | [diff] [blame] | 1093 | { |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 1094 | int ret = 0; |
Viresh Kumar | fcf8058 | 2013-01-29 14:39:08 +0000 | [diff] [blame] | 1095 | |
Viresh Kumar | bb29ae1 | 2015-02-19 17:02:06 +0530 | [diff] [blame] | 1096 | /* Has this CPU been taken care of already? */ |
| 1097 | if (cpumask_test_cpu(cpu, policy->cpus)) |
| 1098 | return 0; |
| 1099 | |
Viresh Kumar | 49f1856 | 2016-02-11 17:31:12 +0530 | [diff] [blame] | 1100 | down_write(&policy->rwsem); |
Rafael J. Wysocki | 45482c7 | 2016-05-12 15:14:12 +0200 | [diff] [blame] | 1101 | if (has_target()) |
| 1102 | cpufreq_stop_governor(policy); |
Viresh Kumar | fcf8058 | 2013-01-29 14:39:08 +0000 | [diff] [blame] | 1103 | |
Viresh Kumar | fcf8058 | 2013-01-29 14:39:08 +0000 | [diff] [blame] | 1104 | cpumask_set_cpu(cpu, policy->cpus); |
Viresh Kumar | 2eaa3e2 | 2013-02-07 10:55:00 +0530 | [diff] [blame] | 1105 | |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 1106 | if (has_target()) { |
Rafael J. Wysocki | 0a30076 | 2016-03-21 15:45:24 +0100 | [diff] [blame] | 1107 | ret = cpufreq_start_governor(policy); |
Viresh Kumar | 49f1856 | 2016-02-11 17:31:12 +0530 | [diff] [blame] | 1108 | if (ret) |
Viresh Kumar | 3de9bde | 2013-08-06 22:53:13 +0530 | [diff] [blame] | 1109 | pr_err("%s: Failed to start governor\n", __func__); |
Viresh Kumar | 820c6ca | 2013-04-22 00:48:03 +0200 | [diff] [blame] | 1110 | } |
Viresh Kumar | 49f1856 | 2016-02-11 17:31:12 +0530 | [diff] [blame] | 1111 | up_write(&policy->rwsem); |
| 1112 | return ret; |
Viresh Kumar | fcf8058 | 2013-01-29 14:39:08 +0000 | [diff] [blame] | 1113 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | |
Viresh Kumar | 11eb69b | 2016-02-22 16:36:42 +0530 | [diff] [blame] | 1115 | static void handle_update(struct work_struct *work) |
| 1116 | { |
| 1117 | struct cpufreq_policy *policy = |
| 1118 | container_of(work, struct cpufreq_policy, update); |
| 1119 | unsigned int cpu = policy->cpu; |
| 1120 | pr_debug("handle_update for cpu %u called\n", cpu); |
| 1121 | cpufreq_update_policy(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | } |
| 1123 | |
Rafael J. Wysocki | a34e63b | 2015-07-27 23:11:50 +0200 | [diff] [blame] | 1124 | static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu) |
Srivatsa S. Bhat | 8414809 | 2013-07-30 04:25:10 +0530 | [diff] [blame] | 1125 | { |
Srivatsa S. Bhat | e9698cc | 2013-07-30 04:24:11 +0530 | [diff] [blame] | 1126 | struct cpufreq_policy *policy; |
Viresh Kumar | edd4a89 | 2016-03-03 14:51:33 +0530 | [diff] [blame] | 1127 | int ret; |
Srivatsa S. Bhat | e9698cc | 2013-07-30 04:24:11 +0530 | [diff] [blame] | 1128 | |
| 1129 | policy = kzalloc(sizeof(*policy), GFP_KERNEL); |
| 1130 | if (!policy) |
| 1131 | return NULL; |
| 1132 | |
| 1133 | if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL)) |
| 1134 | goto err_free_policy; |
| 1135 | |
| 1136 | if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL)) |
| 1137 | goto err_free_cpumask; |
| 1138 | |
Rafael J. Wysocki | 559ed40 | 2015-07-26 02:07:47 +0200 | [diff] [blame] | 1139 | if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL)) |
| 1140 | goto err_free_rcpumask; |
| 1141 | |
Viresh Kumar | edd4a89 | 2016-03-03 14:51:33 +0530 | [diff] [blame] | 1142 | ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq, |
| 1143 | cpufreq_global_kobject, "policy%u", cpu); |
| 1144 | if (ret) { |
| 1145 | pr_err("%s: failed to init policy->kobj: %d\n", __func__, ret); |
| 1146 | goto err_free_real_cpus; |
| 1147 | } |
| 1148 | |
Lukasz Majewski | c88a1f8 | 2013-08-06 22:53:08 +0530 | [diff] [blame] | 1149 | INIT_LIST_HEAD(&policy->policy_list); |
viresh kumar | ad7722d | 2013-10-18 19:10:15 +0530 | [diff] [blame] | 1150 | init_rwsem(&policy->rwsem); |
Srivatsa S. Bhat | 12478cf | 2014-03-24 13:35:44 +0530 | [diff] [blame] | 1151 | spin_lock_init(&policy->transition_lock); |
| 1152 | init_waitqueue_head(&policy->transition_wait); |
Viresh Kumar | 818c571 | 2015-01-02 12:34:38 +0530 | [diff] [blame] | 1153 | init_completion(&policy->kobj_unregister); |
| 1154 | INIT_WORK(&policy->update, handle_update); |
viresh kumar | ad7722d | 2013-10-18 19:10:15 +0530 | [diff] [blame] | 1155 | |
Rafael J. Wysocki | a34e63b | 2015-07-27 23:11:50 +0200 | [diff] [blame] | 1156 | policy->cpu = cpu; |
Srivatsa S. Bhat | e9698cc | 2013-07-30 04:24:11 +0530 | [diff] [blame] | 1157 | return policy; |
| 1158 | |
Viresh Kumar | edd4a89 | 2016-03-03 14:51:33 +0530 | [diff] [blame] | 1159 | err_free_real_cpus: |
| 1160 | free_cpumask_var(policy->real_cpus); |
Viresh Kumar | 2fc3384 | 2015-06-08 18:25:29 +0530 | [diff] [blame] | 1161 | err_free_rcpumask: |
| 1162 | free_cpumask_var(policy->related_cpus); |
Srivatsa S. Bhat | e9698cc | 2013-07-30 04:24:11 +0530 | [diff] [blame] | 1163 | err_free_cpumask: |
| 1164 | free_cpumask_var(policy->cpus); |
| 1165 | err_free_policy: |
| 1166 | kfree(policy); |
| 1167 | |
| 1168 | return NULL; |
| 1169 | } |
| 1170 | |
Viresh Kumar | f9f41e3e | 2017-01-05 10:17:27 +0530 | [diff] [blame] | 1171 | static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy) |
Viresh Kumar | 42f921a | 2013-12-20 21:26:02 +0530 | [diff] [blame] | 1172 | { |
| 1173 | struct kobject *kobj; |
| 1174 | struct completion *cmp; |
| 1175 | |
Viresh Kumar | 8754914 | 2015-06-10 02:13:21 +0200 | [diff] [blame] | 1176 | down_write(&policy->rwsem); |
Rafael J. Wysocki | 1aefc75 | 2016-05-31 22:14:44 +0200 | [diff] [blame] | 1177 | cpufreq_stats_free_table(policy); |
Viresh Kumar | 42f921a | 2013-12-20 21:26:02 +0530 | [diff] [blame] | 1178 | kobj = &policy->kobj; |
| 1179 | cmp = &policy->kobj_unregister; |
Viresh Kumar | 8754914 | 2015-06-10 02:13:21 +0200 | [diff] [blame] | 1180 | up_write(&policy->rwsem); |
Viresh Kumar | 42f921a | 2013-12-20 21:26:02 +0530 | [diff] [blame] | 1181 | kobject_put(kobj); |
| 1182 | |
| 1183 | /* |
| 1184 | * We need to make sure that the underlying kobj is |
| 1185 | * actually not referenced anymore by anybody before we |
| 1186 | * proceed with unloading. |
| 1187 | */ |
| 1188 | pr_debug("waiting for dropping of refcount\n"); |
| 1189 | wait_for_completion(cmp); |
| 1190 | pr_debug("wait complete\n"); |
| 1191 | } |
| 1192 | |
Viresh Kumar | f9f41e3e | 2017-01-05 10:17:27 +0530 | [diff] [blame] | 1193 | static void cpufreq_policy_free(struct cpufreq_policy *policy) |
Srivatsa S. Bhat | e9698cc | 2013-07-30 04:24:11 +0530 | [diff] [blame] | 1194 | { |
Viresh Kumar | 988bed0 | 2015-05-08 11:53:45 +0530 | [diff] [blame] | 1195 | unsigned long flags; |
| 1196 | int cpu; |
| 1197 | |
| 1198 | /* Remove policy from list */ |
| 1199 | write_lock_irqsave(&cpufreq_driver_lock, flags); |
| 1200 | list_del(&policy->policy_list); |
| 1201 | |
| 1202 | for_each_cpu(cpu, policy->related_cpus) |
| 1203 | per_cpu(cpufreq_cpu_data, cpu) = NULL; |
| 1204 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
| 1205 | |
Viresh Kumar | f9f41e3e | 2017-01-05 10:17:27 +0530 | [diff] [blame] | 1206 | cpufreq_policy_put_kobj(policy); |
Rafael J. Wysocki | 559ed40 | 2015-07-26 02:07:47 +0200 | [diff] [blame] | 1207 | free_cpumask_var(policy->real_cpus); |
Srivatsa S. Bhat | e9698cc | 2013-07-30 04:24:11 +0530 | [diff] [blame] | 1208 | free_cpumask_var(policy->related_cpus); |
| 1209 | free_cpumask_var(policy->cpus); |
| 1210 | kfree(policy); |
| 1211 | } |
| 1212 | |
Rafael J. Wysocki | 0b27535 | 2015-07-29 03:03:44 +0200 | [diff] [blame] | 1213 | static int cpufreq_online(unsigned int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | { |
Viresh Kumar | 7f0c020 | 2015-01-02 12:34:32 +0530 | [diff] [blame] | 1215 | struct cpufreq_policy *policy; |
Rafael J. Wysocki | 194d99c | 2015-07-29 03:08:57 +0200 | [diff] [blame] | 1216 | bool new_policy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | unsigned long flags; |
Rafael J. Wysocki | 0b27535 | 2015-07-29 03:03:44 +0200 | [diff] [blame] | 1218 | unsigned int j; |
| 1219 | int ret; |
Ashok Raj | c32b6b8 | 2005-10-30 14:59:54 -0800 | [diff] [blame] | 1220 | |
Rafael J. Wysocki | 0b27535 | 2015-07-29 03:03:44 +0200 | [diff] [blame] | 1221 | pr_debug("%s: bringing CPU%u online\n", __func__, cpu); |
Viresh Kumar | 6eed940 | 2013-08-06 22:53:11 +0530 | [diff] [blame] | 1222 | |
Viresh Kumar | bb29ae1 | 2015-02-19 17:02:06 +0530 | [diff] [blame] | 1223 | /* Check if this CPU already has a policy to manage it */ |
Viresh Kumar | 9104bb2 | 2015-05-12 12:22:12 +0530 | [diff] [blame] | 1224 | policy = per_cpu(cpufreq_cpu_data, cpu); |
Rafael J. Wysocki | 11ce707 | 2015-07-27 23:11:21 +0200 | [diff] [blame] | 1225 | if (policy) { |
Viresh Kumar | 9104bb2 | 2015-05-12 12:22:12 +0530 | [diff] [blame] | 1226 | WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus)); |
Rafael J. Wysocki | 11ce707 | 2015-07-27 23:11:21 +0200 | [diff] [blame] | 1227 | if (!policy_is_inactive(policy)) |
Rafael J. Wysocki | d9612a4 | 2015-07-27 23:11:37 +0200 | [diff] [blame] | 1228 | return cpufreq_add_policy_cpu(policy, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | |
Rafael J. Wysocki | 11ce707 | 2015-07-27 23:11:21 +0200 | [diff] [blame] | 1230 | /* This is the only online CPU for the policy. Start over. */ |
Rafael J. Wysocki | 194d99c | 2015-07-29 03:08:57 +0200 | [diff] [blame] | 1231 | new_policy = false; |
Rafael J. Wysocki | 11ce707 | 2015-07-27 23:11:21 +0200 | [diff] [blame] | 1232 | down_write(&policy->rwsem); |
| 1233 | policy->cpu = cpu; |
| 1234 | policy->governor = NULL; |
| 1235 | up_write(&policy->rwsem); |
| 1236 | } else { |
Rafael J. Wysocki | 194d99c | 2015-07-29 03:08:57 +0200 | [diff] [blame] | 1237 | new_policy = true; |
Rafael J. Wysocki | a34e63b | 2015-07-27 23:11:50 +0200 | [diff] [blame] | 1238 | policy = cpufreq_policy_alloc(cpu); |
Rafael J. Wysocki | 72368d1 | 2013-12-27 01:07:11 +0100 | [diff] [blame] | 1239 | if (!policy) |
Rafael J. Wysocki | d4d854d | 2015-07-27 23:11:30 +0200 | [diff] [blame] | 1240 | return -ENOMEM; |
Rafael J. Wysocki | 72368d1 | 2013-12-27 01:07:11 +0100 | [diff] [blame] | 1241 | } |
Srivatsa S. Bhat | 0d66b91 | 2013-09-12 01:42:59 +0530 | [diff] [blame] | 1242 | |
Viresh Kumar | 91a12e9 | 2019-02-12 16:36:04 +0530 | [diff] [blame] | 1243 | if (!new_policy && cpufreq_driver->online) { |
| 1244 | ret = cpufreq_driver->online(policy); |
| 1245 | if (ret) { |
| 1246 | pr_debug("%s: %d: initialization failed\n", __func__, |
| 1247 | __LINE__); |
| 1248 | goto out_exit_policy; |
| 1249 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | |
Viresh Kumar | 91a12e9 | 2019-02-12 16:36:04 +0530 | [diff] [blame] | 1251 | /* Recover policy->cpus using related_cpus */ |
| 1252 | cpumask_copy(policy->cpus, policy->related_cpus); |
| 1253 | } else { |
| 1254 | cpumask_copy(policy->cpus, cpumask_of(cpu)); |
Viresh Kumar | 643ae6e | 2013-01-12 05:14:38 +0000 | [diff] [blame] | 1255 | |
Viresh Kumar | 91a12e9 | 2019-02-12 16:36:04 +0530 | [diff] [blame] | 1256 | /* |
| 1257 | * Call driver. From then on the cpufreq must be able |
| 1258 | * to accept all calls to ->verify and ->setpolicy for this CPU. |
| 1259 | */ |
| 1260 | ret = cpufreq_driver->init(policy); |
| 1261 | if (ret) { |
| 1262 | pr_debug("%s: %d: initialization failed\n", __func__, |
| 1263 | __LINE__); |
| 1264 | goto out_free_policy; |
| 1265 | } |
Viresh Kumar | d417e06 | 2018-02-22 11:29:44 +0530 | [diff] [blame] | 1266 | |
Viresh Kumar | 91a12e9 | 2019-02-12 16:36:04 +0530 | [diff] [blame] | 1267 | ret = cpufreq_table_validate_and_sort(policy); |
| 1268 | if (ret) |
| 1269 | goto out_exit_policy; |
Tomeu Vizoso | 6d4e81e | 2014-11-24 10:08:03 +0100 | [diff] [blame] | 1270 | |
Rafael J. Wysocki | 4d1f3a5 | 2015-07-27 23:11:44 +0200 | [diff] [blame] | 1271 | /* related_cpus should at least include policy->cpus. */ |
Viresh Kumar | 0998a03 | 2015-10-15 21:35:21 +0530 | [diff] [blame] | 1272 | cpumask_copy(policy->related_cpus, policy->cpus); |
Rafael J. Wysocki | 4d1f3a5 | 2015-07-27 23:11:44 +0200 | [diff] [blame] | 1273 | } |
Rafael J. Wysocki | 559ed40 | 2015-07-26 02:07:47 +0200 | [diff] [blame] | 1274 | |
Viresh Kumar | 91a12e9 | 2019-02-12 16:36:04 +0530 | [diff] [blame] | 1275 | down_write(&policy->rwsem); |
Viresh Kumar | 5a7e56a | 2014-03-04 11:44:00 +0800 | [diff] [blame] | 1276 | /* |
| 1277 | * affected cpus must always be the one, which are online. We aren't |
| 1278 | * managing offline cpus here. |
| 1279 | */ |
| 1280 | cpumask_and(policy->cpus, policy->cpus, cpu_online_mask); |
| 1281 | |
Rafael J. Wysocki | 194d99c | 2015-07-29 03:08:57 +0200 | [diff] [blame] | 1282 | if (new_policy) { |
Viresh Kumar | 5a7e56a | 2014-03-04 11:44:00 +0800 | [diff] [blame] | 1283 | policy->user_policy.min = policy->min; |
| 1284 | policy->user_policy.max = policy->max; |
Tomeu Vizoso | 6d4e81e | 2014-11-24 10:08:03 +0100 | [diff] [blame] | 1285 | |
Rafael J. Wysocki | 2f0ba79 | 2017-03-27 19:33:09 +0200 | [diff] [blame] | 1286 | for_each_cpu(j, policy->related_cpus) { |
Viresh Kumar | 988bed0 | 2015-05-08 11:53:45 +0530 | [diff] [blame] | 1287 | per_cpu(cpufreq_cpu_data, j) = policy; |
Rafael J. Wysocki | 2f0ba79 | 2017-03-27 19:33:09 +0200 | [diff] [blame] | 1288 | add_cpu_dev_symlink(policy, j); |
| 1289 | } |
Viresh Kumar | ff01047 | 2017-03-21 11:36:06 +0530 | [diff] [blame] | 1290 | } else { |
| 1291 | policy->min = policy->user_policy.min; |
| 1292 | policy->max = policy->user_policy.max; |
Viresh Kumar | 988bed0 | 2015-05-08 11:53:45 +0530 | [diff] [blame] | 1293 | } |
Viresh Kumar | 652ed95 | 2014-01-09 20:38:43 +0530 | [diff] [blame] | 1294 | |
Rafael J. Wysocki | 2ed99e3 | 2014-03-12 21:49:33 +0100 | [diff] [blame] | 1295 | if (cpufreq_driver->get && !cpufreq_driver->setpolicy) { |
Viresh Kumar | da60ce9 | 2013-10-03 20:28:30 +0530 | [diff] [blame] | 1296 | policy->cur = cpufreq_driver->get(policy->cpu); |
| 1297 | if (!policy->cur) { |
| 1298 | pr_err("%s: ->get() failed\n", __func__); |
Viresh Kumar | d417e06 | 2018-02-22 11:29:44 +0530 | [diff] [blame] | 1299 | goto out_destroy_policy; |
Viresh Kumar | da60ce9 | 2013-10-03 20:28:30 +0530 | [diff] [blame] | 1300 | } |
| 1301 | } |
| 1302 | |
Viresh Kumar | d391669 | 2013-12-03 11:20:46 +0530 | [diff] [blame] | 1303 | /* |
| 1304 | * Sometimes boot loaders set CPU frequency to a value outside of |
| 1305 | * frequency table present with cpufreq core. In such cases CPU might be |
| 1306 | * unstable if it has to run on that frequency for long duration of time |
| 1307 | * and so its better to set it to a frequency which is specified in |
| 1308 | * freq-table. This also makes cpufreq stats inconsistent as |
| 1309 | * cpufreq-stats would fail to register because current frequency of CPU |
| 1310 | * isn't found in freq-table. |
| 1311 | * |
| 1312 | * Because we don't want this change to effect boot process badly, we go |
| 1313 | * for the next freq which is >= policy->cur ('cur' must be set by now, |
| 1314 | * otherwise we will end up setting freq to lowest of the table as 'cur' |
| 1315 | * is initialized to zero). |
| 1316 | * |
| 1317 | * We are passing target-freq as "policy->cur - 1" otherwise |
| 1318 | * __cpufreq_driver_target() would simply fail, as policy->cur will be |
| 1319 | * equal to target-freq. |
| 1320 | */ |
| 1321 | if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK) |
| 1322 | && has_target()) { |
| 1323 | /* Are we running at unknown frequency ? */ |
| 1324 | ret = cpufreq_frequency_table_get_index(policy, policy->cur); |
| 1325 | if (ret == -EINVAL) { |
| 1326 | /* Warn user and fix it */ |
| 1327 | pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n", |
| 1328 | __func__, policy->cpu, policy->cur); |
| 1329 | ret = __cpufreq_driver_target(policy, policy->cur - 1, |
| 1330 | CPUFREQ_RELATION_L); |
| 1331 | |
| 1332 | /* |
| 1333 | * Reaching here after boot in a few seconds may not |
| 1334 | * mean that system will remain stable at "unknown" |
| 1335 | * frequency for longer duration. Hence, a BUG_ON(). |
| 1336 | */ |
| 1337 | BUG_ON(ret); |
| 1338 | pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n", |
| 1339 | __func__, policy->cpu, policy->cur); |
| 1340 | } |
| 1341 | } |
| 1342 | |
Rafael J. Wysocki | 194d99c | 2015-07-29 03:08:57 +0200 | [diff] [blame] | 1343 | if (new_policy) { |
Rafael J. Wysocki | d9612a4 | 2015-07-27 23:11:37 +0200 | [diff] [blame] | 1344 | ret = cpufreq_add_dev_interface(policy); |
Srivatsa S. Bhat | a82fab2 | 2013-07-30 04:24:49 +0530 | [diff] [blame] | 1345 | if (ret) |
Viresh Kumar | d417e06 | 2018-02-22 11:29:44 +0530 | [diff] [blame] | 1346 | goto out_destroy_policy; |
Rafael J. Wysocki | 1aefc75 | 2016-05-31 22:14:44 +0200 | [diff] [blame] | 1347 | |
| 1348 | cpufreq_stats_create_table(policy); |
Dave Jones | 8ff6973 | 2006-03-05 03:37:23 -0500 | [diff] [blame] | 1349 | |
Viresh Kumar | 988bed0 | 2015-05-08 11:53:45 +0530 | [diff] [blame] | 1350 | write_lock_irqsave(&cpufreq_driver_lock, flags); |
| 1351 | list_add(&policy->policy_list, &cpufreq_policy_list); |
| 1352 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
| 1353 | } |
Viresh Kumar | 9515f4d | 2013-08-20 12:08:23 +0530 | [diff] [blame] | 1354 | |
Viresh Kumar | 7f0fa40 | 2015-07-08 15:12:16 +0530 | [diff] [blame] | 1355 | ret = cpufreq_init_policy(policy); |
| 1356 | if (ret) { |
| 1357 | pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n", |
| 1358 | __func__, cpu, ret); |
Viresh Kumar | d417e06 | 2018-02-22 11:29:44 +0530 | [diff] [blame] | 1359 | goto out_destroy_policy; |
Viresh Kumar | 08fd8c1c | 2013-12-24 07:11:01 +0530 | [diff] [blame] | 1360 | } |
Srivatsa S. Bhat | e18f168 | 2013-07-30 04:24:23 +0530 | [diff] [blame] | 1361 | |
Viresh Kumar | 4e97b63 | 2014-03-04 11:44:01 +0800 | [diff] [blame] | 1362 | up_write(&policy->rwsem); |
Viresh Kumar | 08fd8c1c | 2013-12-24 07:11:01 +0530 | [diff] [blame] | 1363 | |
Greg Kroah-Hartman | 038c5b3 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 1364 | kobject_uevent(&policy->kobj, KOBJ_ADD); |
Viresh Kumar | 7c45cf3 | 2014-11-27 06:07:51 +0530 | [diff] [blame] | 1365 | |
Viresh Kumar | 7c45cf3 | 2014-11-27 06:07:51 +0530 | [diff] [blame] | 1366 | /* Callback for handling stuff after policy is ready */ |
| 1367 | if (cpufreq_driver->ready) |
| 1368 | cpufreq_driver->ready(policy); |
| 1369 | |
Amit Kucheria | 5c238a8 | 2019-01-30 10:52:01 +0530 | [diff] [blame] | 1370 | if (IS_ENABLED(CONFIG_CPU_THERMAL) && |
| 1371 | cpufreq_driver->flags & CPUFREQ_IS_COOLING_DEV) |
| 1372 | policy->cdev = of_cpufreq_cooling_register(policy); |
| 1373 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 1374 | pr_debug("initialization complete\n"); |
Dave Jones | 87c3227 | 2006-03-29 01:48:37 -0500 | [diff] [blame] | 1375 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | return 0; |
| 1377 | |
Viresh Kumar | d417e06 | 2018-02-22 11:29:44 +0530 | [diff] [blame] | 1378 | out_destroy_policy: |
Viresh Kumar | b24b647 | 2018-02-22 11:29:43 +0530 | [diff] [blame] | 1379 | for_each_cpu(j, policy->real_cpus) |
| 1380 | remove_cpu_dev_symlink(policy, get_cpu_device(j)); |
| 1381 | |
Prarit Bhargava | 7106e02 | 2014-09-10 10:12:08 -0400 | [diff] [blame] | 1382 | up_write(&policy->rwsem); |
| 1383 | |
Viresh Kumar | d417e06 | 2018-02-22 11:29:44 +0530 | [diff] [blame] | 1384 | out_exit_policy: |
Viresh Kumar | da60ce9 | 2013-10-03 20:28:30 +0530 | [diff] [blame] | 1385 | if (cpufreq_driver->exit) |
| 1386 | cpufreq_driver->exit(policy); |
Rafael J. Wysocki | 2f0ba79 | 2017-03-27 19:33:09 +0200 | [diff] [blame] | 1387 | |
Viresh Kumar | 8101f99 | 2015-07-08 15:12:15 +0530 | [diff] [blame] | 1388 | out_free_policy: |
Viresh Kumar | f9f41e3e | 2017-01-05 10:17:27 +0530 | [diff] [blame] | 1389 | cpufreq_policy_free(policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | return ret; |
| 1391 | } |
| 1392 | |
Rafael J. Wysocki | 0b27535 | 2015-07-29 03:03:44 +0200 | [diff] [blame] | 1393 | /** |
| 1394 | * cpufreq_add_dev - the cpufreq interface for a CPU device. |
| 1395 | * @dev: CPU device. |
| 1396 | * @sif: Subsystem interface structure pointer (not used) |
| 1397 | */ |
| 1398 | static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif) |
| 1399 | { |
Rafael J. Wysocki | a794d61 | 2016-04-07 03:31:57 +0200 | [diff] [blame] | 1400 | struct cpufreq_policy *policy; |
Rafael J. Wysocki | 0b27535 | 2015-07-29 03:03:44 +0200 | [diff] [blame] | 1401 | unsigned cpu = dev->id; |
Viresh Kumar | 26619804 | 2016-09-12 12:07:05 +0530 | [diff] [blame] | 1402 | int ret; |
Rafael J. Wysocki | 0b27535 | 2015-07-29 03:03:44 +0200 | [diff] [blame] | 1403 | |
| 1404 | dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu); |
| 1405 | |
Viresh Kumar | 26619804 | 2016-09-12 12:07:05 +0530 | [diff] [blame] | 1406 | if (cpu_online(cpu)) { |
| 1407 | ret = cpufreq_online(cpu); |
| 1408 | if (ret) |
| 1409 | return ret; |
| 1410 | } |
Rafael J. Wysocki | 0b27535 | 2015-07-29 03:03:44 +0200 | [diff] [blame] | 1411 | |
Viresh Kumar | 26619804 | 2016-09-12 12:07:05 +0530 | [diff] [blame] | 1412 | /* Create sysfs link on CPU registration */ |
Rafael J. Wysocki | a794d61 | 2016-04-07 03:31:57 +0200 | [diff] [blame] | 1413 | policy = per_cpu(cpufreq_cpu_data, cpu); |
Rafael J. Wysocki | 2f0ba79 | 2017-03-27 19:33:09 +0200 | [diff] [blame] | 1414 | if (policy) |
| 1415 | add_cpu_dev_symlink(policy, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | |
Rafael J. Wysocki | 2f0ba79 | 2017-03-27 19:33:09 +0200 | [diff] [blame] | 1417 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | } |
| 1419 | |
Sebastian Andrzej Siewior | 27622b0 | 2016-09-06 19:04:48 +0200 | [diff] [blame] | 1420 | static int cpufreq_offline(unsigned int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | { |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 1422 | struct cpufreq_policy *policy; |
Viresh Kumar | 69cee71 | 2016-02-11 17:31:11 +0530 | [diff] [blame] | 1423 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | |
Viresh Kumar | b8eed8a | 2013-01-14 13:23:03 +0000 | [diff] [blame] | 1425 | pr_debug("%s: unregistering CPU %u\n", __func__, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | |
Viresh Kumar | 988bed0 | 2015-05-08 11:53:45 +0530 | [diff] [blame] | 1427 | policy = cpufreq_cpu_get_raw(cpu); |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 1428 | if (!policy) { |
Viresh Kumar | b8eed8a | 2013-01-14 13:23:03 +0000 | [diff] [blame] | 1429 | pr_debug("%s: No cpu_data found\n", __func__); |
Sebastian Andrzej Siewior | 27622b0 | 2016-09-06 19:04:48 +0200 | [diff] [blame] | 1430 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | |
Viresh Kumar | 49f1856 | 2016-02-11 17:31:12 +0530 | [diff] [blame] | 1433 | down_write(&policy->rwsem); |
Rafael J. Wysocki | 45482c7 | 2016-05-12 15:14:12 +0200 | [diff] [blame] | 1434 | if (has_target()) |
| 1435 | cpufreq_stop_governor(policy); |
Jacob Shin | 27ecddc | 2011-04-27 13:32:11 -0500 | [diff] [blame] | 1436 | |
Viresh Kumar | 9591bec | 2015-06-10 02:20:23 +0200 | [diff] [blame] | 1437 | cpumask_clear_cpu(cpu, policy->cpus); |
Viresh Kumar | 4573237 | 2015-05-12 12:22:34 +0530 | [diff] [blame] | 1438 | |
Viresh Kumar | 9591bec | 2015-06-10 02:20:23 +0200 | [diff] [blame] | 1439 | if (policy_is_inactive(policy)) { |
| 1440 | if (has_target()) |
| 1441 | strncpy(policy->last_governor, policy->governor->name, |
| 1442 | CPUFREQ_NAME_LEN); |
Srinivas Pandruvada | 69030dd1 | 2015-12-01 16:52:14 -0800 | [diff] [blame] | 1443 | else |
| 1444 | policy->last_policy = policy->policy; |
Viresh Kumar | 9591bec | 2015-06-10 02:20:23 +0200 | [diff] [blame] | 1445 | } else if (cpu == policy->cpu) { |
| 1446 | /* Nominate new CPU */ |
| 1447 | policy->cpu = cpumask_any(policy->cpus); |
| 1448 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | |
Viresh Kumar | 9591bec | 2015-06-10 02:20:23 +0200 | [diff] [blame] | 1450 | /* Start governor again for active policy */ |
| 1451 | if (!policy_is_inactive(policy)) { |
| 1452 | if (has_target()) { |
Rafael J. Wysocki | 0a30076 | 2016-03-21 15:45:24 +0100 | [diff] [blame] | 1453 | ret = cpufreq_start_governor(policy); |
Viresh Kumar | 9591bec | 2015-06-10 02:20:23 +0200 | [diff] [blame] | 1454 | if (ret) |
| 1455 | pr_err("%s: Failed to start governor\n", __func__); |
| 1456 | } |
Viresh Kumar | 69cee71 | 2016-02-11 17:31:11 +0530 | [diff] [blame] | 1457 | |
Viresh Kumar | 49f1856 | 2016-02-11 17:31:12 +0530 | [diff] [blame] | 1458 | goto unlock; |
Viresh Kumar | 69cee71 | 2016-02-11 17:31:11 +0530 | [diff] [blame] | 1459 | } |
| 1460 | |
Amit Kucheria | 5c238a8 | 2019-01-30 10:52:01 +0530 | [diff] [blame] | 1461 | if (IS_ENABLED(CONFIG_CPU_THERMAL) && |
| 1462 | cpufreq_driver->flags & CPUFREQ_IS_COOLING_DEV) { |
| 1463 | cpufreq_cooling_unregister(policy->cdev); |
| 1464 | policy->cdev = NULL; |
| 1465 | } |
| 1466 | |
Viresh Kumar | 69cee71 | 2016-02-11 17:31:11 +0530 | [diff] [blame] | 1467 | if (cpufreq_driver->stop_cpu) |
Dirk Brandewie | 367dc4a | 2014-03-19 08:45:53 -0700 | [diff] [blame] | 1468 | cpufreq_driver->stop_cpu(policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | |
Rafael J. Wysocki | 36be341 | 2016-05-12 15:13:35 +0200 | [diff] [blame] | 1470 | if (has_target()) |
| 1471 | cpufreq_exit_governor(policy); |
Viresh Kumar | 8754914 | 2015-06-10 02:13:21 +0200 | [diff] [blame] | 1472 | |
Viresh Kumar | 8754914 | 2015-06-10 02:13:21 +0200 | [diff] [blame] | 1473 | /* |
Viresh Kumar | 91a12e9 | 2019-02-12 16:36:04 +0530 | [diff] [blame] | 1474 | * Perform the ->offline() during light-weight tear-down, as |
| 1475 | * that allows fast recovery when the CPU comes back. |
Viresh Kumar | 8754914 | 2015-06-10 02:13:21 +0200 | [diff] [blame] | 1476 | */ |
Viresh Kumar | 91a12e9 | 2019-02-12 16:36:04 +0530 | [diff] [blame] | 1477 | if (cpufreq_driver->offline) { |
| 1478 | cpufreq_driver->offline(policy); |
| 1479 | } else if (cpufreq_driver->exit) { |
Viresh Kumar | 8754914 | 2015-06-10 02:13:21 +0200 | [diff] [blame] | 1480 | cpufreq_driver->exit(policy); |
Srinivas Pandruvada | 55582bc | 2015-10-07 13:50:44 -0700 | [diff] [blame] | 1481 | policy->freq_table = NULL; |
| 1482 | } |
Viresh Kumar | 49f1856 | 2016-02-11 17:31:12 +0530 | [diff] [blame] | 1483 | |
| 1484 | unlock: |
| 1485 | up_write(&policy->rwsem); |
Sebastian Andrzej Siewior | 27622b0 | 2016-09-06 19:04:48 +0200 | [diff] [blame] | 1486 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | } |
| 1488 | |
Srivatsa S. Bhat | cedb70a | 2013-09-07 01:23:09 +0530 | [diff] [blame] | 1489 | /** |
Viresh Kumar | 27a862e | 2013-10-02 14:13:14 +0530 | [diff] [blame] | 1490 | * cpufreq_remove_dev - remove a CPU device |
Srivatsa S. Bhat | cedb70a | 2013-09-07 01:23:09 +0530 | [diff] [blame] | 1491 | * |
| 1492 | * Removes the cpufreq interface for a CPU device. |
Srivatsa S. Bhat | cedb70a | 2013-09-07 01:23:09 +0530 | [diff] [blame] | 1493 | */ |
Viresh Kumar | 71db87b | 2015-07-30 15:04:01 +0530 | [diff] [blame] | 1494 | static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif) |
Venkatesh Pallipadi | 5a01f2e | 2007-02-05 16:12:44 -0800 | [diff] [blame] | 1495 | { |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 1496 | unsigned int cpu = dev->id; |
Rafael J. Wysocki | 559ed40 | 2015-07-26 02:07:47 +0200 | [diff] [blame] | 1497 | struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); |
Venki Pallipadi | ec28297 | 2007-03-26 12:03:19 -0700 | [diff] [blame] | 1498 | |
Rafael J. Wysocki | 559ed40 | 2015-07-26 02:07:47 +0200 | [diff] [blame] | 1499 | if (!policy) |
Linus Torvalds | 1af115d | 2015-08-31 08:47:40 -0700 | [diff] [blame] | 1500 | return; |
Viresh Kumar | 8754914 | 2015-06-10 02:13:21 +0200 | [diff] [blame] | 1501 | |
Viresh Kumar | 69cee71 | 2016-02-11 17:31:11 +0530 | [diff] [blame] | 1502 | if (cpu_online(cpu)) |
| 1503 | cpufreq_offline(cpu); |
Viresh Kumar | 8754914 | 2015-06-10 02:13:21 +0200 | [diff] [blame] | 1504 | |
Rafael J. Wysocki | 559ed40 | 2015-07-26 02:07:47 +0200 | [diff] [blame] | 1505 | cpumask_clear_cpu(cpu, policy->real_cpus); |
Viresh Kumar | 26619804 | 2016-09-12 12:07:05 +0530 | [diff] [blame] | 1506 | remove_cpu_dev_symlink(policy, dev); |
Viresh Kumar | f344dae | 2015-11-21 09:06:49 +0530 | [diff] [blame] | 1507 | |
Viresh Kumar | 91a12e9 | 2019-02-12 16:36:04 +0530 | [diff] [blame] | 1508 | if (cpumask_empty(policy->real_cpus)) { |
| 1509 | /* We did light-weight exit earlier, do full tear down now */ |
| 1510 | if (cpufreq_driver->offline) |
| 1511 | cpufreq_driver->exit(policy); |
| 1512 | |
Viresh Kumar | f9f41e3e | 2017-01-05 10:17:27 +0530 | [diff] [blame] | 1513 | cpufreq_policy_free(policy); |
Viresh Kumar | 91a12e9 | 2019-02-12 16:36:04 +0530 | [diff] [blame] | 1514 | } |
Venkatesh Pallipadi | 5a01f2e | 2007-02-05 16:12:44 -0800 | [diff] [blame] | 1515 | } |
| 1516 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | /** |
Viresh Kumar | bb176f7 | 2013-06-19 14:19:33 +0530 | [diff] [blame] | 1518 | * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're |
| 1519 | * in deep trouble. |
Viresh Kumar | a1e1dc4 | 2015-01-02 12:34:28 +0530 | [diff] [blame] | 1520 | * @policy: policy managing CPUs |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | * @new_freq: CPU frequency the CPU actually runs at |
| 1522 | * |
Dave Jones | 29464f2 | 2009-01-18 01:37:11 -0500 | [diff] [blame] | 1523 | * We adjust to current frequency first, and need to clean up later. |
| 1524 | * So either call to cpufreq_update_policy() or schedule handle_update()). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | */ |
Viresh Kumar | a1e1dc4 | 2015-01-02 12:34:28 +0530 | [diff] [blame] | 1526 | static void cpufreq_out_of_sync(struct cpufreq_policy *policy, |
Gautham R Shenoy | e08f5f5 | 2006-10-26 16:20:58 +0530 | [diff] [blame] | 1527 | unsigned int new_freq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | { |
| 1529 | struct cpufreq_freqs freqs; |
Viresh Kumar | b43a7ff | 2013-03-24 11:56:43 +0530 | [diff] [blame] | 1530 | |
Joe Perches | e837f9b | 2014-03-11 10:03:00 -0700 | [diff] [blame] | 1531 | pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n", |
Viresh Kumar | a1e1dc4 | 2015-01-02 12:34:28 +0530 | [diff] [blame] | 1532 | policy->cur, new_freq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | |
Viresh Kumar | a1e1dc4 | 2015-01-02 12:34:28 +0530 | [diff] [blame] | 1534 | freqs.old = policy->cur; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | freqs.new = new_freq; |
Viresh Kumar | b43a7ff | 2013-03-24 11:56:43 +0530 | [diff] [blame] | 1536 | |
Viresh Kumar | 8fec051 | 2014-03-24 13:35:45 +0530 | [diff] [blame] | 1537 | cpufreq_freq_transition_begin(policy, &freqs); |
| 1538 | cpufreq_freq_transition_end(policy, &freqs, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | } |
| 1540 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 1541 | /** |
Dhaval Giani | 4ab70df | 2006-12-13 14:49:15 +0530 | [diff] [blame] | 1542 | * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur |
Venkatesh Pallipadi | 95235ca | 2005-12-02 10:43:20 -0800 | [diff] [blame] | 1543 | * @cpu: CPU number |
| 1544 | * |
| 1545 | * This is the last known freq, without actually getting it from the driver. |
| 1546 | * Return value will be same as what is shown in scaling_cur_freq in sysfs. |
| 1547 | */ |
| 1548 | unsigned int cpufreq_quick_get(unsigned int cpu) |
| 1549 | { |
Dirk Brandewie | 9e21ba8 | 2013-02-06 09:02:08 -0800 | [diff] [blame] | 1550 | struct cpufreq_policy *policy; |
Gautham R Shenoy | e08f5f5 | 2006-10-26 16:20:58 +0530 | [diff] [blame] | 1551 | unsigned int ret_freq = 0; |
Richard Cochran | c75361c | 2016-03-11 09:43:07 +0100 | [diff] [blame] | 1552 | unsigned long flags; |
Venkatesh Pallipadi | 95235ca | 2005-12-02 10:43:20 -0800 | [diff] [blame] | 1553 | |
Richard Cochran | c75361c | 2016-03-11 09:43:07 +0100 | [diff] [blame] | 1554 | read_lock_irqsave(&cpufreq_driver_lock, flags); |
| 1555 | |
| 1556 | if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) { |
| 1557 | ret_freq = cpufreq_driver->get(cpu); |
| 1558 | read_unlock_irqrestore(&cpufreq_driver_lock, flags); |
| 1559 | return ret_freq; |
| 1560 | } |
| 1561 | |
| 1562 | read_unlock_irqrestore(&cpufreq_driver_lock, flags); |
Dirk Brandewie | 9e21ba8 | 2013-02-06 09:02:08 -0800 | [diff] [blame] | 1563 | |
| 1564 | policy = cpufreq_cpu_get(cpu); |
Venkatesh Pallipadi | 95235ca | 2005-12-02 10:43:20 -0800 | [diff] [blame] | 1565 | if (policy) { |
Gautham R Shenoy | e08f5f5 | 2006-10-26 16:20:58 +0530 | [diff] [blame] | 1566 | ret_freq = policy->cur; |
Venkatesh Pallipadi | 95235ca | 2005-12-02 10:43:20 -0800 | [diff] [blame] | 1567 | cpufreq_cpu_put(policy); |
| 1568 | } |
| 1569 | |
Dave Jones | 4d34a67 | 2008-02-07 16:33:49 -0500 | [diff] [blame] | 1570 | return ret_freq; |
Venkatesh Pallipadi | 95235ca | 2005-12-02 10:43:20 -0800 | [diff] [blame] | 1571 | } |
| 1572 | EXPORT_SYMBOL(cpufreq_quick_get); |
| 1573 | |
Jesse Barnes | 3d73710 | 2011-06-28 10:59:12 -0700 | [diff] [blame] | 1574 | /** |
| 1575 | * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU |
| 1576 | * @cpu: CPU number |
| 1577 | * |
| 1578 | * Just return the max possible frequency for a given CPU. |
| 1579 | */ |
| 1580 | unsigned int cpufreq_quick_get_max(unsigned int cpu) |
| 1581 | { |
| 1582 | struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); |
| 1583 | unsigned int ret_freq = 0; |
| 1584 | |
| 1585 | if (policy) { |
| 1586 | ret_freq = policy->max; |
| 1587 | cpufreq_cpu_put(policy); |
| 1588 | } |
| 1589 | |
| 1590 | return ret_freq; |
| 1591 | } |
| 1592 | EXPORT_SYMBOL(cpufreq_quick_get_max); |
| 1593 | |
Viresh Kumar | d92d50a | 2015-01-02 12:34:29 +0530 | [diff] [blame] | 1594 | static unsigned int __cpufreq_get(struct cpufreq_policy *policy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | { |
Gautham R Shenoy | e08f5f5 | 2006-10-26 16:20:58 +0530 | [diff] [blame] | 1596 | unsigned int ret_freq = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | |
Sudeep Holla | 2f66196 | 2019-01-07 18:51:53 +0000 | [diff] [blame] | 1598 | if (unlikely(policy_is_inactive(policy)) || !cpufreq_driver->get) |
Dave Jones | 4d34a67 | 2008-02-07 16:33:49 -0500 | [diff] [blame] | 1599 | return ret_freq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | |
Viresh Kumar | d92d50a | 2015-01-02 12:34:29 +0530 | [diff] [blame] | 1601 | ret_freq = cpufreq_driver->get(policy->cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | |
Rafael J. Wysocki | b7898fd | 2016-03-30 03:47:49 +0200 | [diff] [blame] | 1603 | /* |
Sudeep Holla | 2f66196 | 2019-01-07 18:51:53 +0000 | [diff] [blame] | 1604 | * If fast frequency switching is used with the given policy, the check |
Rafael J. Wysocki | b7898fd | 2016-03-30 03:47:49 +0200 | [diff] [blame] | 1605 | * against policy->cur is pointless, so skip it in that case too. |
| 1606 | */ |
Sudeep Holla | 2f66196 | 2019-01-07 18:51:53 +0000 | [diff] [blame] | 1607 | if (policy->fast_switch_enabled) |
Viresh Kumar | 11e584c | 2015-06-10 02:11:45 +0200 | [diff] [blame] | 1608 | return ret_freq; |
| 1609 | |
Gautham R Shenoy | e08f5f5 | 2006-10-26 16:20:58 +0530 | [diff] [blame] | 1610 | if (ret_freq && policy->cur && |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 1611 | !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { |
Gautham R Shenoy | e08f5f5 | 2006-10-26 16:20:58 +0530 | [diff] [blame] | 1612 | /* verify no discrepancy between actual and |
| 1613 | saved value exists */ |
| 1614 | if (unlikely(ret_freq != policy->cur)) { |
Viresh Kumar | a1e1dc4 | 2015-01-02 12:34:28 +0530 | [diff] [blame] | 1615 | cpufreq_out_of_sync(policy, ret_freq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | schedule_work(&policy->update); |
| 1617 | } |
| 1618 | } |
| 1619 | |
Dave Jones | 4d34a67 | 2008-02-07 16:33:49 -0500 | [diff] [blame] | 1620 | return ret_freq; |
Venkatesh Pallipadi | 5a01f2e | 2007-02-05 16:12:44 -0800 | [diff] [blame] | 1621 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | |
Venkatesh Pallipadi | 5a01f2e | 2007-02-05 16:12:44 -0800 | [diff] [blame] | 1623 | /** |
| 1624 | * cpufreq_get - get the current CPU frequency (in kHz) |
| 1625 | * @cpu: CPU number |
| 1626 | * |
| 1627 | * Get the CPU current (static) CPU frequency |
| 1628 | */ |
| 1629 | unsigned int cpufreq_get(unsigned int cpu) |
| 1630 | { |
Aaron Plattner | 999976e | 2014-03-04 12:42:15 -0800 | [diff] [blame] | 1631 | struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); |
Venkatesh Pallipadi | 5a01f2e | 2007-02-05 16:12:44 -0800 | [diff] [blame] | 1632 | unsigned int ret_freq = 0; |
Venkatesh Pallipadi | 5a01f2e | 2007-02-05 16:12:44 -0800 | [diff] [blame] | 1633 | |
Aaron Plattner | 999976e | 2014-03-04 12:42:15 -0800 | [diff] [blame] | 1634 | if (policy) { |
| 1635 | down_read(&policy->rwsem); |
Sudeep Holla | 2f66196 | 2019-01-07 18:51:53 +0000 | [diff] [blame] | 1636 | ret_freq = __cpufreq_get(policy); |
Aaron Plattner | 999976e | 2014-03-04 12:42:15 -0800 | [diff] [blame] | 1637 | up_read(&policy->rwsem); |
Viresh Kumar | 26ca869 | 2013-09-20 22:37:31 +0530 | [diff] [blame] | 1638 | |
Aaron Plattner | 999976e | 2014-03-04 12:42:15 -0800 | [diff] [blame] | 1639 | cpufreq_cpu_put(policy); |
| 1640 | } |
Viresh Kumar | 6eed940 | 2013-08-06 22:53:11 +0530 | [diff] [blame] | 1641 | |
Dave Jones | 4d34a67 | 2008-02-07 16:33:49 -0500 | [diff] [blame] | 1642 | return ret_freq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | } |
| 1644 | EXPORT_SYMBOL(cpufreq_get); |
| 1645 | |
Rafael J. Wysocki | 999f572 | 2016-03-21 15:46:25 +0100 | [diff] [blame] | 1646 | static unsigned int cpufreq_update_current_freq(struct cpufreq_policy *policy) |
| 1647 | { |
| 1648 | unsigned int new_freq; |
| 1649 | |
| 1650 | new_freq = cpufreq_driver->get(policy->cpu); |
| 1651 | if (!new_freq) |
| 1652 | return 0; |
| 1653 | |
| 1654 | if (!policy->cur) { |
| 1655 | pr_debug("cpufreq: Driver did not initialize current freq\n"); |
| 1656 | policy->cur = new_freq; |
| 1657 | } else if (policy->cur != new_freq && has_target()) { |
| 1658 | cpufreq_out_of_sync(policy, new_freq); |
| 1659 | } |
| 1660 | |
| 1661 | return new_freq; |
| 1662 | } |
| 1663 | |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 1664 | static struct subsys_interface cpufreq_interface = { |
| 1665 | .name = "cpufreq", |
| 1666 | .subsys = &cpu_subsys, |
| 1667 | .add_dev = cpufreq_add_dev, |
| 1668 | .remove_dev = cpufreq_remove_dev, |
Rafael J. Wysocki | e00e56d | 2011-03-23 22:16:32 +0100 | [diff] [blame] | 1669 | }; |
| 1670 | |
Viresh Kumar | e28867e | 2014-03-04 11:00:27 +0800 | [diff] [blame] | 1671 | /* |
| 1672 | * In case platform wants some specific frequency to be configured |
| 1673 | * during suspend.. |
Benjamin Herrenschmidt | 42d4dc3 | 2005-04-29 07:40:12 -0700 | [diff] [blame] | 1674 | */ |
Viresh Kumar | e28867e | 2014-03-04 11:00:27 +0800 | [diff] [blame] | 1675 | int cpufreq_generic_suspend(struct cpufreq_policy *policy) |
Benjamin Herrenschmidt | 42d4dc3 | 2005-04-29 07:40:12 -0700 | [diff] [blame] | 1676 | { |
Viresh Kumar | e28867e | 2014-03-04 11:00:27 +0800 | [diff] [blame] | 1677 | int ret; |
Dave Jones | 4bc5d34 | 2009-08-04 14:03:25 -0400 | [diff] [blame] | 1678 | |
Viresh Kumar | e28867e | 2014-03-04 11:00:27 +0800 | [diff] [blame] | 1679 | if (!policy->suspend_freq) { |
Bartlomiej Zolnierkiewicz | 201f371 | 2015-09-08 18:41:02 +0200 | [diff] [blame] | 1680 | pr_debug("%s: suspend_freq not defined\n", __func__); |
| 1681 | return 0; |
Benjamin Herrenschmidt | 42d4dc3 | 2005-04-29 07:40:12 -0700 | [diff] [blame] | 1682 | } |
| 1683 | |
Viresh Kumar | e28867e | 2014-03-04 11:00:27 +0800 | [diff] [blame] | 1684 | pr_debug("%s: Setting suspend-freq: %u\n", __func__, |
| 1685 | policy->suspend_freq); |
| 1686 | |
| 1687 | ret = __cpufreq_driver_target(policy, policy->suspend_freq, |
| 1688 | CPUFREQ_RELATION_H); |
| 1689 | if (ret) |
| 1690 | pr_err("%s: unable to set suspend-freq: %u. err: %d\n", |
| 1691 | __func__, policy->suspend_freq, ret); |
| 1692 | |
Dave Jones | c906049 | 2008-02-07 16:32:18 -0500 | [diff] [blame] | 1693 | return ret; |
Benjamin Herrenschmidt | 42d4dc3 | 2005-04-29 07:40:12 -0700 | [diff] [blame] | 1694 | } |
Viresh Kumar | e28867e | 2014-03-04 11:00:27 +0800 | [diff] [blame] | 1695 | EXPORT_SYMBOL(cpufreq_generic_suspend); |
Benjamin Herrenschmidt | 42d4dc3 | 2005-04-29 07:40:12 -0700 | [diff] [blame] | 1696 | |
| 1697 | /** |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 1698 | * cpufreq_suspend() - Suspend CPUFreq governors |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | * |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 1700 | * Called during system wide Suspend/Hibernate cycles for suspending governors |
| 1701 | * as some platforms can't change frequency after this point in suspend cycle. |
| 1702 | * Because some of the devices (like: i2c, regulators, etc) they use for |
| 1703 | * changing frequency are suspended quickly after this point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | */ |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 1705 | void cpufreq_suspend(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | { |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 1707 | struct cpufreq_policy *policy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 1709 | if (!cpufreq_driver) |
Rafael J. Wysocki | e00e56d | 2011-03-23 22:16:32 +0100 | [diff] [blame] | 1710 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | |
Rafael J. Wysocki | ba41e1b | 2016-05-02 02:27:19 +0200 | [diff] [blame] | 1712 | if (!has_target() && !cpufreq_driver->suspend) |
Viresh Kumar | b1b12bab | 2014-09-30 09:33:17 +0530 | [diff] [blame] | 1713 | goto suspend; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 1715 | pr_debug("%s: Suspending Governors\n", __func__); |
| 1716 | |
Viresh Kumar | f963735 | 2015-05-12 12:20:11 +0530 | [diff] [blame] | 1717 | for_each_active_policy(policy) { |
Rafael J. Wysocki | ba41e1b | 2016-05-02 02:27:19 +0200 | [diff] [blame] | 1718 | if (has_target()) { |
| 1719 | down_write(&policy->rwsem); |
Rafael J. Wysocki | 45482c7 | 2016-05-12 15:14:12 +0200 | [diff] [blame] | 1720 | cpufreq_stop_governor(policy); |
Rafael J. Wysocki | ba41e1b | 2016-05-02 02:27:19 +0200 | [diff] [blame] | 1721 | up_write(&policy->rwsem); |
Rafael J. Wysocki | ba41e1b | 2016-05-02 02:27:19 +0200 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy)) |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 1725 | pr_err("%s: Failed to suspend driver: %p\n", __func__, |
| 1726 | policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | } |
Viresh Kumar | b1b12bab | 2014-09-30 09:33:17 +0530 | [diff] [blame] | 1728 | |
| 1729 | suspend: |
| 1730 | cpufreq_suspended = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | } |
| 1732 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | /** |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 1734 | * cpufreq_resume() - Resume CPUFreq governors |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | * |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 1736 | * Called during system wide Suspend/Hibernate cycle for resuming governors that |
| 1737 | * are suspended with cpufreq_suspend(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | */ |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 1739 | void cpufreq_resume(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | struct cpufreq_policy *policy; |
Viresh Kumar | 49f1856 | 2016-02-11 17:31:12 +0530 | [diff] [blame] | 1742 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 1744 | if (!cpufreq_driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | return; |
| 1746 | |
Bo Yan | 703cbaa | 2018-01-23 13:57:55 -0800 | [diff] [blame] | 1747 | if (unlikely(!cpufreq_suspended)) |
| 1748 | return; |
| 1749 | |
Lan Tianyu | 8e30444 | 2014-09-18 15:03:07 +0800 | [diff] [blame] | 1750 | cpufreq_suspended = false; |
| 1751 | |
Rafael J. Wysocki | ba41e1b | 2016-05-02 02:27:19 +0200 | [diff] [blame] | 1752 | if (!has_target() && !cpufreq_driver->resume) |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 1753 | return; |
| 1754 | |
| 1755 | pr_debug("%s: Resuming Governors\n", __func__); |
| 1756 | |
Viresh Kumar | f963735 | 2015-05-12 12:20:11 +0530 | [diff] [blame] | 1757 | for_each_active_policy(policy) { |
Viresh Kumar | 49f1856 | 2016-02-11 17:31:12 +0530 | [diff] [blame] | 1758 | if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) { |
Viresh Kumar | 0c5aa40 | 2014-03-24 12:30:29 +0530 | [diff] [blame] | 1759 | pr_err("%s: Failed to resume driver: %p\n", __func__, |
| 1760 | policy); |
Rafael J. Wysocki | ba41e1b | 2016-05-02 02:27:19 +0200 | [diff] [blame] | 1761 | } else if (has_target()) { |
Viresh Kumar | 49f1856 | 2016-02-11 17:31:12 +0530 | [diff] [blame] | 1762 | down_write(&policy->rwsem); |
Rafael J. Wysocki | 0a30076 | 2016-03-21 15:45:24 +0100 | [diff] [blame] | 1763 | ret = cpufreq_start_governor(policy); |
Viresh Kumar | 49f1856 | 2016-02-11 17:31:12 +0530 | [diff] [blame] | 1764 | up_write(&policy->rwsem); |
| 1765 | |
| 1766 | if (ret) |
| 1767 | pr_err("%s: Failed to start governor for policy: %p\n", |
| 1768 | __func__, policy); |
| 1769 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | |
Borislav Petkov | 9d95046 | 2013-01-20 10:24:28 +0000 | [diff] [blame] | 1773 | /** |
| 1774 | * cpufreq_get_current_driver - return current driver's name |
| 1775 | * |
| 1776 | * Return the name string of the currently loaded cpufreq driver |
| 1777 | * or NULL, if none. |
| 1778 | */ |
| 1779 | const char *cpufreq_get_current_driver(void) |
| 1780 | { |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 1781 | if (cpufreq_driver) |
| 1782 | return cpufreq_driver->name; |
| 1783 | |
| 1784 | return NULL; |
Borislav Petkov | 9d95046 | 2013-01-20 10:24:28 +0000 | [diff] [blame] | 1785 | } |
| 1786 | EXPORT_SYMBOL_GPL(cpufreq_get_current_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | |
Thomas Petazzoni | 51315cd | 2014-10-19 11:30:27 +0200 | [diff] [blame] | 1788 | /** |
| 1789 | * cpufreq_get_driver_data - return current driver data |
| 1790 | * |
| 1791 | * Return the private data of the currently loaded cpufreq |
| 1792 | * driver, or NULL if no cpufreq driver is loaded. |
| 1793 | */ |
| 1794 | void *cpufreq_get_driver_data(void) |
| 1795 | { |
| 1796 | if (cpufreq_driver) |
| 1797 | return cpufreq_driver->driver_data; |
| 1798 | |
| 1799 | return NULL; |
| 1800 | } |
| 1801 | EXPORT_SYMBOL_GPL(cpufreq_get_driver_data); |
| 1802 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | /********************************************************************* |
| 1804 | * NOTIFIER LISTS INTERFACE * |
| 1805 | *********************************************************************/ |
| 1806 | |
| 1807 | /** |
| 1808 | * cpufreq_register_notifier - register a driver with cpufreq |
| 1809 | * @nb: notifier function to register |
| 1810 | * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER |
| 1811 | * |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 1812 | * 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] | 1813 | * are notified about clock rate changes (once before and once after |
| 1814 | * the transition), or a list of drivers that are notified about |
| 1815 | * changes in cpufreq policy. |
| 1816 | * |
| 1817 | * This function may sleep, and has the same return conditions as |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 1818 | * blocking_notifier_chain_register. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | */ |
| 1820 | int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list) |
| 1821 | { |
| 1822 | int ret; |
| 1823 | |
Dirk Brandewie | d5aaffa | 2013-01-17 16:22:21 +0000 | [diff] [blame] | 1824 | if (cpufreq_disabled()) |
| 1825 | return -EINVAL; |
| 1826 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | switch (list) { |
| 1828 | case CPUFREQ_TRANSITION_NOTIFIER: |
Rafael J. Wysocki | b7898fd | 2016-03-30 03:47:49 +0200 | [diff] [blame] | 1829 | mutex_lock(&cpufreq_fast_switch_lock); |
| 1830 | |
| 1831 | if (cpufreq_fast_switch_count > 0) { |
| 1832 | mutex_unlock(&cpufreq_fast_switch_lock); |
| 1833 | return -EBUSY; |
| 1834 | } |
Alan Stern | b4dfdbb | 2006-10-04 02:17:06 -0700 | [diff] [blame] | 1835 | ret = srcu_notifier_chain_register( |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 1836 | &cpufreq_transition_notifier_list, nb); |
Rafael J. Wysocki | b7898fd | 2016-03-30 03:47:49 +0200 | [diff] [blame] | 1837 | if (!ret) |
| 1838 | cpufreq_fast_switch_count--; |
| 1839 | |
| 1840 | mutex_unlock(&cpufreq_fast_switch_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | break; |
| 1842 | case CPUFREQ_POLICY_NOTIFIER: |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 1843 | ret = blocking_notifier_chain_register( |
| 1844 | &cpufreq_policy_notifier_list, nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1845 | break; |
| 1846 | default: |
| 1847 | ret = -EINVAL; |
| 1848 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1849 | |
| 1850 | return ret; |
| 1851 | } |
| 1852 | EXPORT_SYMBOL(cpufreq_register_notifier); |
| 1853 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1854 | /** |
| 1855 | * cpufreq_unregister_notifier - unregister a driver with cpufreq |
| 1856 | * @nb: notifier block to be unregistered |
Viresh Kumar | bb176f7 | 2013-06-19 14:19:33 +0530 | [diff] [blame] | 1857 | * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1858 | * |
| 1859 | * Remove a driver from the CPU frequency notifier list. |
| 1860 | * |
| 1861 | * This function may sleep, and has the same return conditions as |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 1862 | * blocking_notifier_chain_unregister. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1863 | */ |
| 1864 | int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list) |
| 1865 | { |
| 1866 | int ret; |
| 1867 | |
Dirk Brandewie | d5aaffa | 2013-01-17 16:22:21 +0000 | [diff] [blame] | 1868 | if (cpufreq_disabled()) |
| 1869 | return -EINVAL; |
| 1870 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | switch (list) { |
| 1872 | case CPUFREQ_TRANSITION_NOTIFIER: |
Rafael J. Wysocki | b7898fd | 2016-03-30 03:47:49 +0200 | [diff] [blame] | 1873 | mutex_lock(&cpufreq_fast_switch_lock); |
| 1874 | |
Alan Stern | b4dfdbb | 2006-10-04 02:17:06 -0700 | [diff] [blame] | 1875 | ret = srcu_notifier_chain_unregister( |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 1876 | &cpufreq_transition_notifier_list, nb); |
Rafael J. Wysocki | b7898fd | 2016-03-30 03:47:49 +0200 | [diff] [blame] | 1877 | if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0)) |
| 1878 | cpufreq_fast_switch_count++; |
| 1879 | |
| 1880 | mutex_unlock(&cpufreq_fast_switch_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1881 | break; |
| 1882 | case CPUFREQ_POLICY_NOTIFIER: |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 1883 | ret = blocking_notifier_chain_unregister( |
| 1884 | &cpufreq_policy_notifier_list, nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1885 | break; |
| 1886 | default: |
| 1887 | ret = -EINVAL; |
| 1888 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | |
| 1890 | return ret; |
| 1891 | } |
| 1892 | EXPORT_SYMBOL(cpufreq_unregister_notifier); |
| 1893 | |
| 1894 | |
| 1895 | /********************************************************************* |
| 1896 | * GOVERNORS * |
| 1897 | *********************************************************************/ |
| 1898 | |
Rafael J. Wysocki | b7898fd | 2016-03-30 03:47:49 +0200 | [diff] [blame] | 1899 | /** |
| 1900 | * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch. |
| 1901 | * @policy: cpufreq policy to switch the frequency for. |
| 1902 | * @target_freq: New frequency to set (may be approximate). |
| 1903 | * |
| 1904 | * Carry out a fast frequency switch without sleeping. |
| 1905 | * |
| 1906 | * The driver's ->fast_switch() callback invoked by this function must be |
| 1907 | * suitable for being called from within RCU-sched read-side critical sections |
| 1908 | * and it is expected to select the minimum available frequency greater than or |
| 1909 | * equal to @target_freq (CPUFREQ_RELATION_L). |
| 1910 | * |
| 1911 | * This function must not be called if policy->fast_switch_enabled is unset. |
| 1912 | * |
| 1913 | * Governors calling this function must guarantee that it will never be invoked |
| 1914 | * twice in parallel for the same policy and that it will never be called in |
| 1915 | * parallel with either ->target() or ->target_index() for the same policy. |
| 1916 | * |
Viresh Kumar | 209887e | 2017-08-09 10:21:46 +0530 | [diff] [blame] | 1917 | * Returns the actual frequency set for the CPU. |
| 1918 | * |
| 1919 | * If 0 is returned by the driver's ->fast_switch() callback to indicate an |
| 1920 | * error condition, the hardware configuration must be preserved. |
Rafael J. Wysocki | b7898fd | 2016-03-30 03:47:49 +0200 | [diff] [blame] | 1921 | */ |
| 1922 | unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy, |
| 1923 | unsigned int target_freq) |
| 1924 | { |
Rafael J. Wysocki | b9af694 | 2016-06-01 22:36:26 +0200 | [diff] [blame] | 1925 | target_freq = clamp_val(target_freq, policy->min, policy->max); |
Rafael J. Wysocki | b7898fd | 2016-03-30 03:47:49 +0200 | [diff] [blame] | 1926 | |
| 1927 | return cpufreq_driver->fast_switch(policy, target_freq); |
| 1928 | } |
| 1929 | EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch); |
| 1930 | |
Viresh Kumar | 1c03a2d | 2014-06-02 22:49:28 +0530 | [diff] [blame] | 1931 | /* Must set freqs->new to intermediate frequency */ |
| 1932 | static int __target_intermediate(struct cpufreq_policy *policy, |
| 1933 | struct cpufreq_freqs *freqs, int index) |
| 1934 | { |
| 1935 | int ret; |
| 1936 | |
| 1937 | freqs->new = cpufreq_driver->get_intermediate(policy, index); |
| 1938 | |
| 1939 | /* We don't need to switch to intermediate freq */ |
| 1940 | if (!freqs->new) |
| 1941 | return 0; |
| 1942 | |
| 1943 | pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n", |
| 1944 | __func__, policy->cpu, freqs->old, freqs->new); |
| 1945 | |
| 1946 | cpufreq_freq_transition_begin(policy, freqs); |
| 1947 | ret = cpufreq_driver->target_intermediate(policy, index); |
| 1948 | cpufreq_freq_transition_end(policy, freqs, ret); |
| 1949 | |
| 1950 | if (ret) |
| 1951 | pr_err("%s: Failed to change to intermediate frequency: %d\n", |
| 1952 | __func__, ret); |
| 1953 | |
| 1954 | return ret; |
| 1955 | } |
| 1956 | |
Viresh Kumar | 2372784 | 2016-06-03 10:58:50 +0530 | [diff] [blame] | 1957 | static int __target_index(struct cpufreq_policy *policy, int index) |
Viresh Kumar | 8d65775 | 2014-05-21 14:29:29 +0530 | [diff] [blame] | 1958 | { |
Viresh Kumar | 1c03a2d | 2014-06-02 22:49:28 +0530 | [diff] [blame] | 1959 | struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0}; |
| 1960 | unsigned int intermediate_freq = 0; |
Viresh Kumar | 2372784 | 2016-06-03 10:58:50 +0530 | [diff] [blame] | 1961 | unsigned int newfreq = policy->freq_table[index].frequency; |
Viresh Kumar | 8d65775 | 2014-05-21 14:29:29 +0530 | [diff] [blame] | 1962 | int retval = -EINVAL; |
| 1963 | bool notify; |
| 1964 | |
Viresh Kumar | 2372784 | 2016-06-03 10:58:50 +0530 | [diff] [blame] | 1965 | if (newfreq == policy->cur) |
| 1966 | return 0; |
| 1967 | |
Viresh Kumar | 8d65775 | 2014-05-21 14:29:29 +0530 | [diff] [blame] | 1968 | notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION); |
Viresh Kumar | 8d65775 | 2014-05-21 14:29:29 +0530 | [diff] [blame] | 1969 | if (notify) { |
Viresh Kumar | 1c03a2d | 2014-06-02 22:49:28 +0530 | [diff] [blame] | 1970 | /* Handle switching to intermediate frequency */ |
| 1971 | if (cpufreq_driver->get_intermediate) { |
| 1972 | retval = __target_intermediate(policy, &freqs, index); |
| 1973 | if (retval) |
| 1974 | return retval; |
Viresh Kumar | 8d65775 | 2014-05-21 14:29:29 +0530 | [diff] [blame] | 1975 | |
Viresh Kumar | 1c03a2d | 2014-06-02 22:49:28 +0530 | [diff] [blame] | 1976 | intermediate_freq = freqs.new; |
| 1977 | /* Set old freq to intermediate */ |
| 1978 | if (intermediate_freq) |
| 1979 | freqs.old = freqs.new; |
| 1980 | } |
| 1981 | |
Viresh Kumar | 2372784 | 2016-06-03 10:58:50 +0530 | [diff] [blame] | 1982 | freqs.new = newfreq; |
Viresh Kumar | 8d65775 | 2014-05-21 14:29:29 +0530 | [diff] [blame] | 1983 | pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n", |
| 1984 | __func__, policy->cpu, freqs.old, freqs.new); |
| 1985 | |
| 1986 | cpufreq_freq_transition_begin(policy, &freqs); |
| 1987 | } |
| 1988 | |
| 1989 | retval = cpufreq_driver->target_index(policy, index); |
| 1990 | if (retval) |
| 1991 | pr_err("%s: Failed to change cpu frequency: %d\n", __func__, |
| 1992 | retval); |
| 1993 | |
Viresh Kumar | 1c03a2d | 2014-06-02 22:49:28 +0530 | [diff] [blame] | 1994 | if (notify) { |
Viresh Kumar | 8d65775 | 2014-05-21 14:29:29 +0530 | [diff] [blame] | 1995 | cpufreq_freq_transition_end(policy, &freqs, retval); |
| 1996 | |
Viresh Kumar | 1c03a2d | 2014-06-02 22:49:28 +0530 | [diff] [blame] | 1997 | /* |
| 1998 | * Failed after setting to intermediate freq? Driver should have |
| 1999 | * reverted back to initial frequency and so should we. Check |
| 2000 | * here for intermediate_freq instead of get_intermediate, in |
Shailendra Verma | 58405af | 2015-05-22 22:48:22 +0530 | [diff] [blame] | 2001 | * case we haven't switched to intermediate freq at all. |
Viresh Kumar | 1c03a2d | 2014-06-02 22:49:28 +0530 | [diff] [blame] | 2002 | */ |
| 2003 | if (unlikely(retval && intermediate_freq)) { |
| 2004 | freqs.old = intermediate_freq; |
| 2005 | freqs.new = policy->restore_freq; |
| 2006 | cpufreq_freq_transition_begin(policy, &freqs); |
| 2007 | cpufreq_freq_transition_end(policy, &freqs, 0); |
| 2008 | } |
| 2009 | } |
| 2010 | |
Viresh Kumar | 8d65775 | 2014-05-21 14:29:29 +0530 | [diff] [blame] | 2011 | return retval; |
| 2012 | } |
| 2013 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | int __cpufreq_driver_target(struct cpufreq_policy *policy, |
| 2015 | unsigned int target_freq, |
| 2016 | unsigned int relation) |
| 2017 | { |
Viresh Kumar | 7249924 | 2012-10-31 01:28:21 +0100 | [diff] [blame] | 2018 | unsigned int old_target_freq = target_freq; |
Viresh Kumar | d218ed7 | 2016-06-03 10:58:51 +0530 | [diff] [blame] | 2019 | int index; |
Ashok Raj | c32b6b8 | 2005-10-30 14:59:54 -0800 | [diff] [blame] | 2020 | |
Konrad Rzeszutek Wilk | a7b422c | 2012-03-13 19:18:39 -0400 | [diff] [blame] | 2021 | if (cpufreq_disabled()) |
| 2022 | return -ENODEV; |
| 2023 | |
Viresh Kumar | 7249924 | 2012-10-31 01:28:21 +0100 | [diff] [blame] | 2024 | /* Make sure that target_freq is within supported range */ |
Viresh Kumar | 910c6e8 | 2016-05-26 11:27:24 +0530 | [diff] [blame] | 2025 | target_freq = clamp_val(target_freq, policy->min, policy->max); |
Viresh Kumar | 7249924 | 2012-10-31 01:28:21 +0100 | [diff] [blame] | 2026 | |
| 2027 | pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n", |
Joe Perches | e837f9b | 2014-03-11 10:03:00 -0700 | [diff] [blame] | 2028 | policy->cpu, target_freq, relation, old_target_freq); |
Viresh Kumar | 5a1c022 | 2012-10-31 01:28:15 +0100 | [diff] [blame] | 2029 | |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 2030 | /* |
| 2031 | * This might look like a redundant call as we are checking it again |
| 2032 | * after finding index. But it is left intentionally for cases where |
| 2033 | * exactly same freq is called again and so we can save on few function |
| 2034 | * calls. |
| 2035 | */ |
Viresh Kumar | 5a1c022 | 2012-10-31 01:28:15 +0100 | [diff] [blame] | 2036 | if (target_freq == policy->cur) |
| 2037 | return 0; |
| 2038 | |
Viresh Kumar | 1c03a2d | 2014-06-02 22:49:28 +0530 | [diff] [blame] | 2039 | /* Save last value to restore later on errors */ |
| 2040 | policy->restore_freq = policy->cur; |
| 2041 | |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 2042 | if (cpufreq_driver->target) |
Rafael J. Wysocki | 6019d23 | 2016-02-26 00:03:01 +0100 | [diff] [blame] | 2043 | return cpufreq_driver->target(policy, target_freq, relation); |
Ashok Raj | 90d45d1 | 2005-11-08 21:34:24 -0800 | [diff] [blame] | 2044 | |
Rafael J. Wysocki | 6019d23 | 2016-02-26 00:03:01 +0100 | [diff] [blame] | 2045 | if (!cpufreq_driver->target_index) |
| 2046 | return -EINVAL; |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 2047 | |
Viresh Kumar | d218ed7 | 2016-06-03 10:58:51 +0530 | [diff] [blame] | 2048 | index = cpufreq_frequency_table_target(policy, target_freq, relation); |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 2049 | |
Viresh Kumar | 2372784 | 2016-06-03 10:58:50 +0530 | [diff] [blame] | 2050 | return __target_index(policy, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | } |
| 2052 | EXPORT_SYMBOL_GPL(__cpufreq_driver_target); |
| 2053 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | int cpufreq_driver_target(struct cpufreq_policy *policy, |
| 2055 | unsigned int target_freq, |
| 2056 | unsigned int relation) |
| 2057 | { |
Julia Lawall | f1829e4 | 2008-07-25 22:44:53 +0200 | [diff] [blame] | 2058 | int ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2059 | |
viresh kumar | ad7722d | 2013-10-18 19:10:15 +0530 | [diff] [blame] | 2060 | down_write(&policy->rwsem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | |
| 2062 | ret = __cpufreq_driver_target(policy, target_freq, relation); |
| 2063 | |
viresh kumar | ad7722d | 2013-10-18 19:10:15 +0530 | [diff] [blame] | 2064 | up_write(&policy->rwsem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | return ret; |
| 2067 | } |
| 2068 | EXPORT_SYMBOL_GPL(cpufreq_driver_target); |
| 2069 | |
Rafael J. Wysocki | de1df26 | 2016-02-05 02:37:42 +0100 | [diff] [blame] | 2070 | __weak struct cpufreq_governor *cpufreq_fallback_governor(void) |
| 2071 | { |
| 2072 | return NULL; |
| 2073 | } |
| 2074 | |
Rafael J. Wysocki | a92604b | 2016-05-14 01:01:46 +0200 | [diff] [blame] | 2075 | static int cpufreq_init_governor(struct cpufreq_policy *policy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | { |
Dave Jones | cc993ca | 2005-07-28 09:43:56 -0700 | [diff] [blame] | 2077 | int ret; |
Thomas Renninger | 6afde10 | 2007-10-02 13:28:13 -0700 | [diff] [blame] | 2078 | |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 2079 | /* Don't start any governor operations if we are entering suspend */ |
| 2080 | if (cpufreq_suspended) |
| 2081 | return 0; |
Ethan Zhao | cb57720b | 2014-12-18 15:28:19 +0900 | [diff] [blame] | 2082 | /* |
| 2083 | * Governor might not be initiated here if ACPI _PPC changed |
| 2084 | * notification happened, so check it. |
| 2085 | */ |
| 2086 | if (!policy->governor) |
| 2087 | return -EINVAL; |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 2088 | |
Viresh Kumar | ed4676e | 2017-07-19 15:42:46 +0530 | [diff] [blame] | 2089 | /* Platform doesn't want dynamic frequency switching ? */ |
| 2090 | if (policy->governor->dynamic_switching && |
Viresh Kumar | fc4c709 | 2017-07-19 15:42:49 +0530 | [diff] [blame] | 2091 | cpufreq_driver->flags & CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING) { |
Rafael J. Wysocki | de1df26 | 2016-02-05 02:37:42 +0100 | [diff] [blame] | 2092 | struct cpufreq_governor *gov = cpufreq_fallback_governor(); |
| 2093 | |
| 2094 | if (gov) { |
Viresh Kumar | fe829ed | 2017-07-19 15:42:48 +0530 | [diff] [blame] | 2095 | pr_warn("Can't use %s governor as dynamic switching is disallowed. Fallback to %s governor\n", |
Joe Perches | e837f9b | 2014-03-11 10:03:00 -0700 | [diff] [blame] | 2096 | policy->governor->name, gov->name); |
Thomas Renninger | 6afde10 | 2007-10-02 13:28:13 -0700 | [diff] [blame] | 2097 | policy->governor = gov; |
Rafael J. Wysocki | de1df26 | 2016-02-05 02:37:42 +0100 | [diff] [blame] | 2098 | } else { |
| 2099 | return -EINVAL; |
Thomas Renninger | 6afde10 | 2007-10-02 13:28:13 -0700 | [diff] [blame] | 2100 | } |
Thomas Renninger | 1c25624 | 2007-10-02 13:28:12 -0700 | [diff] [blame] | 2101 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2102 | |
Rafael J. Wysocki | a92604b | 2016-05-14 01:01:46 +0200 | [diff] [blame] | 2103 | if (!try_module_get(policy->governor->owner)) |
| 2104 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | |
Rafael J. Wysocki | a92604b | 2016-05-14 01:01:46 +0200 | [diff] [blame] | 2106 | pr_debug("%s: for CPU %u\n", __func__, policy->cpu); |
Xiaoguang Chen | 95731eb | 2013-06-19 15:00:07 +0800 | [diff] [blame] | 2107 | |
Rafael J. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 2108 | if (policy->governor->init) { |
| 2109 | ret = policy->governor->init(policy); |
| 2110 | if (ret) { |
Rafael J. Wysocki | 36be341 | 2016-05-12 15:13:35 +0200 | [diff] [blame] | 2111 | module_put(policy->governor->owner); |
Rafael J. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 2112 | return ret; |
| 2113 | } |
Rafael J. Wysocki | 36be341 | 2016-05-12 15:13:35 +0200 | [diff] [blame] | 2114 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2115 | |
Rafael J. Wysocki | a92604b | 2016-05-14 01:01:46 +0200 | [diff] [blame] | 2116 | return 0; |
| 2117 | } |
| 2118 | |
| 2119 | static void cpufreq_exit_governor(struct cpufreq_policy *policy) |
| 2120 | { |
| 2121 | if (cpufreq_suspended || !policy->governor) |
| 2122 | return; |
| 2123 | |
| 2124 | pr_debug("%s: for CPU %u\n", __func__, policy->cpu); |
| 2125 | |
Rafael J. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 2126 | if (policy->governor->exit) |
| 2127 | policy->governor->exit(policy); |
Rafael J. Wysocki | a92604b | 2016-05-14 01:01:46 +0200 | [diff] [blame] | 2128 | |
Rafael J. Wysocki | a92604b | 2016-05-14 01:01:46 +0200 | [diff] [blame] | 2129 | module_put(policy->governor->owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2130 | } |
| 2131 | |
Rafael J. Wysocki | 0a30076 | 2016-03-21 15:45:24 +0100 | [diff] [blame] | 2132 | static int cpufreq_start_governor(struct cpufreq_policy *policy) |
| 2133 | { |
| 2134 | int ret; |
| 2135 | |
Rafael J. Wysocki | a92604b | 2016-05-14 01:01:46 +0200 | [diff] [blame] | 2136 | if (cpufreq_suspended) |
| 2137 | return 0; |
| 2138 | |
| 2139 | if (!policy->governor) |
| 2140 | return -EINVAL; |
| 2141 | |
| 2142 | pr_debug("%s: for CPU %u\n", __func__, policy->cpu); |
| 2143 | |
Rafael J. Wysocki | 3bbf8fe | 2016-03-21 15:47:48 +0100 | [diff] [blame] | 2144 | if (cpufreq_driver->get && !cpufreq_driver->setpolicy) |
| 2145 | cpufreq_update_current_freq(policy); |
| 2146 | |
Rafael J. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 2147 | if (policy->governor->start) { |
| 2148 | ret = policy->governor->start(policy); |
| 2149 | if (ret) |
| 2150 | return ret; |
| 2151 | } |
Rafael J. Wysocki | d6ff44d | 2016-05-14 00:59:27 +0200 | [diff] [blame] | 2152 | |
Rafael J. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 2153 | if (policy->governor->limits) |
| 2154 | policy->governor->limits(policy); |
| 2155 | |
Rafael J. Wysocki | d6ff44d | 2016-05-14 00:59:27 +0200 | [diff] [blame] | 2156 | return 0; |
Rafael J. Wysocki | 0a30076 | 2016-03-21 15:45:24 +0100 | [diff] [blame] | 2157 | } |
| 2158 | |
Rafael J. Wysocki | a92604b | 2016-05-14 01:01:46 +0200 | [diff] [blame] | 2159 | static void cpufreq_stop_governor(struct cpufreq_policy *policy) |
| 2160 | { |
| 2161 | if (cpufreq_suspended || !policy->governor) |
| 2162 | return; |
| 2163 | |
| 2164 | pr_debug("%s: for CPU %u\n", __func__, policy->cpu); |
| 2165 | |
Rafael J. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 2166 | if (policy->governor->stop) |
| 2167 | policy->governor->stop(policy); |
Rafael J. Wysocki | a92604b | 2016-05-14 01:01:46 +0200 | [diff] [blame] | 2168 | } |
| 2169 | |
| 2170 | static void cpufreq_governor_limits(struct cpufreq_policy *policy) |
| 2171 | { |
| 2172 | if (cpufreq_suspended || !policy->governor) |
| 2173 | return; |
| 2174 | |
| 2175 | pr_debug("%s: for CPU %u\n", __func__, policy->cpu); |
| 2176 | |
Rafael J. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 2177 | if (policy->governor->limits) |
| 2178 | policy->governor->limits(policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | } |
| 2180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2181 | int cpufreq_register_governor(struct cpufreq_governor *governor) |
| 2182 | { |
Jeremy Fitzhardinge | 3bcb09a | 2006-07-06 12:30:26 -0700 | [diff] [blame] | 2183 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | |
| 2185 | if (!governor) |
| 2186 | return -EINVAL; |
| 2187 | |
Konrad Rzeszutek Wilk | a7b422c | 2012-03-13 19:18:39 -0400 | [diff] [blame] | 2188 | if (cpufreq_disabled()) |
| 2189 | return -ENODEV; |
| 2190 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 2191 | mutex_lock(&cpufreq_governor_mutex); |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 2192 | |
Jeremy Fitzhardinge | 3bcb09a | 2006-07-06 12:30:26 -0700 | [diff] [blame] | 2193 | err = -EBUSY; |
Viresh Kumar | 42f91fa | 2015-01-02 12:34:26 +0530 | [diff] [blame] | 2194 | if (!find_governor(governor->name)) { |
Jeremy Fitzhardinge | 3bcb09a | 2006-07-06 12:30:26 -0700 | [diff] [blame] | 2195 | err = 0; |
| 2196 | list_add(&governor->governor_list, &cpufreq_governor_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2198 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 2199 | mutex_unlock(&cpufreq_governor_mutex); |
Jeremy Fitzhardinge | 3bcb09a | 2006-07-06 12:30:26 -0700 | [diff] [blame] | 2200 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | } |
| 2202 | EXPORT_SYMBOL_GPL(cpufreq_register_governor); |
| 2203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | void cpufreq_unregister_governor(struct cpufreq_governor *governor) |
| 2205 | { |
Viresh Kumar | 4573237 | 2015-05-12 12:22:34 +0530 | [diff] [blame] | 2206 | struct cpufreq_policy *policy; |
| 2207 | unsigned long flags; |
Prarit Bhargava | 90e41ba | 2009-11-12 09:18:46 -0500 | [diff] [blame] | 2208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2209 | if (!governor) |
| 2210 | return; |
| 2211 | |
Konrad Rzeszutek Wilk | a7b422c | 2012-03-13 19:18:39 -0400 | [diff] [blame] | 2212 | if (cpufreq_disabled()) |
| 2213 | return; |
| 2214 | |
Viresh Kumar | 4573237 | 2015-05-12 12:22:34 +0530 | [diff] [blame] | 2215 | /* clear last_governor for all inactive policies */ |
| 2216 | read_lock_irqsave(&cpufreq_driver_lock, flags); |
| 2217 | for_each_inactive_policy(policy) { |
Viresh Kumar | 18bf3a1 | 2015-05-12 12:22:51 +0530 | [diff] [blame] | 2218 | if (!strcmp(policy->last_governor, governor->name)) { |
| 2219 | policy->governor = NULL; |
Viresh Kumar | 4573237 | 2015-05-12 12:22:34 +0530 | [diff] [blame] | 2220 | strcpy(policy->last_governor, "\0"); |
Viresh Kumar | 18bf3a1 | 2015-05-12 12:22:51 +0530 | [diff] [blame] | 2221 | } |
Prarit Bhargava | 90e41ba | 2009-11-12 09:18:46 -0500 | [diff] [blame] | 2222 | } |
Viresh Kumar | 4573237 | 2015-05-12 12:22:34 +0530 | [diff] [blame] | 2223 | read_unlock_irqrestore(&cpufreq_driver_lock, flags); |
Prarit Bhargava | 90e41ba | 2009-11-12 09:18:46 -0500 | [diff] [blame] | 2224 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 2225 | mutex_lock(&cpufreq_governor_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2226 | list_del(&governor->governor_list); |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 2227 | mutex_unlock(&cpufreq_governor_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | } |
| 2229 | EXPORT_SYMBOL_GPL(cpufreq_unregister_governor); |
| 2230 | |
| 2231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | /********************************************************************* |
| 2233 | * POLICY INTERFACE * |
| 2234 | *********************************************************************/ |
| 2235 | |
| 2236 | /** |
| 2237 | * cpufreq_get_policy - get the current cpufreq_policy |
Dave Jones | 29464f2 | 2009-01-18 01:37:11 -0500 | [diff] [blame] | 2238 | * @policy: struct cpufreq_policy into which the current cpufreq_policy |
| 2239 | * is written |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2240 | * |
| 2241 | * Reads the current cpufreq policy. |
| 2242 | */ |
| 2243 | int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu) |
| 2244 | { |
| 2245 | struct cpufreq_policy *cpu_policy; |
| 2246 | if (!policy) |
| 2247 | return -EINVAL; |
| 2248 | |
| 2249 | cpu_policy = cpufreq_cpu_get(cpu); |
| 2250 | if (!cpu_policy) |
| 2251 | return -EINVAL; |
| 2252 | |
Viresh Kumar | d5b73cd | 2013-08-06 22:53:06 +0530 | [diff] [blame] | 2253 | memcpy(policy, cpu_policy, sizeof(*policy)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2254 | |
| 2255 | cpufreq_cpu_put(cpu_policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | return 0; |
| 2257 | } |
| 2258 | EXPORT_SYMBOL(cpufreq_get_policy); |
| 2259 | |
Rafael J. Wysocki | a0dbb81 | 2019-02-20 00:22:51 +0100 | [diff] [blame] | 2260 | /** |
| 2261 | * cpufreq_set_policy - Modify cpufreq policy parameters. |
| 2262 | * @policy: Policy object to modify. |
| 2263 | * @new_policy: New policy data. |
| 2264 | * |
| 2265 | * Pass @new_policy to the cpufreq driver's ->verify() callback, run the |
| 2266 | * installed policy notifiers for it with the CPUFREQ_ADJUST value, pass it to |
| 2267 | * the driver's ->verify() callback again and run the notifiers for it again |
| 2268 | * with the CPUFREQ_NOTIFY value. Next, copy the min and max parameters |
| 2269 | * of @new_policy to @policy and either invoke the driver's ->setpolicy() |
| 2270 | * callback (if present) or carry out a governor update for @policy. That is, |
| 2271 | * run the current governor's ->limits() callback (if the governor field in |
| 2272 | * @new_policy points to the same object as the one in @policy) or replace the |
| 2273 | * governor for @policy with the new one stored in @new_policy. |
| 2274 | * |
| 2275 | * The cpuinfo part of @policy is not updated by this function. |
Arjan van de Ven | 153d7f3 | 2006-07-26 15:40:07 +0200 | [diff] [blame] | 2276 | */ |
Viresh Kumar | 037ce83 | 2013-10-02 14:13:16 +0530 | [diff] [blame] | 2277 | static int cpufreq_set_policy(struct cpufreq_policy *policy, |
Rafael J. Wysocki | a0dbb81 | 2019-02-20 00:22:51 +0100 | [diff] [blame] | 2278 | struct cpufreq_policy *new_policy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2279 | { |
Rafael J. Wysocki | d9a789c | 2014-02-17 22:56:35 +0100 | [diff] [blame] | 2280 | struct cpufreq_governor *old_gov; |
| 2281 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 | |
Joe Perches | e837f9b | 2014-03-11 10:03:00 -0700 | [diff] [blame] | 2283 | pr_debug("setting new policy for CPU %u: %u - %u kHz\n", |
| 2284 | new_policy->cpu, new_policy->min, new_policy->max); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2285 | |
Viresh Kumar | d5b73cd | 2013-08-06 22:53:06 +0530 | [diff] [blame] | 2286 | memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2287 | |
Pan Xinhui | fba9573 | 2015-07-30 18:10:40 +0800 | [diff] [blame] | 2288 | /* |
| 2289 | * This check works well when we store new min/max freq attributes, |
| 2290 | * because new_policy is a copy of policy with one field updated. |
| 2291 | */ |
| 2292 | if (new_policy->min > new_policy->max) |
Rafael J. Wysocki | d9a789c | 2014-02-17 22:56:35 +0100 | [diff] [blame] | 2293 | return -EINVAL; |
Mattia Dongili | 9c9a43e | 2006-07-05 23:12:20 +0200 | [diff] [blame] | 2294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | /* verify the cpu speed can be set within this limit */ |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 2296 | ret = cpufreq_driver->verify(new_policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2297 | if (ret) |
Rafael J. Wysocki | d9a789c | 2014-02-17 22:56:35 +0100 | [diff] [blame] | 2298 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2299 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2300 | /* adjust if necessary - all reasons */ |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 2301 | blocking_notifier_call_chain(&cpufreq_policy_notifier_list, |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 2302 | CPUFREQ_ADJUST, new_policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2303 | |
Viresh Kumar | bb176f7 | 2013-06-19 14:19:33 +0530 | [diff] [blame] | 2304 | /* |
| 2305 | * verify the cpu speed can be set within this limit, which might be |
| 2306 | * different to the first one |
| 2307 | */ |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 2308 | ret = cpufreq_driver->verify(new_policy); |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 2309 | if (ret) |
Rafael J. Wysocki | d9a789c | 2014-02-17 22:56:35 +0100 | [diff] [blame] | 2310 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2311 | |
| 2312 | /* notification of the new policy */ |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 2313 | blocking_notifier_call_chain(&cpufreq_policy_notifier_list, |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 2314 | CPUFREQ_NOTIFY, new_policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2315 | |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 2316 | policy->min = new_policy->min; |
| 2317 | policy->max = new_policy->max; |
Ruchi Kandoi | 601b218 | 2018-07-24 10:35:44 -0700 | [diff] [blame] | 2318 | trace_cpu_frequency_limits(policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2319 | |
Steve Muckle | e3c0623 | 2016-07-13 13:25:25 -0700 | [diff] [blame] | 2320 | policy->cached_target_freq = UINT_MAX; |
| 2321 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 2322 | pr_debug("new min and max freqs are %u - %u kHz\n", |
Joe Perches | e837f9b | 2014-03-11 10:03:00 -0700 | [diff] [blame] | 2323 | policy->min, policy->max); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2324 | |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 2325 | if (cpufreq_driver->setpolicy) { |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 2326 | policy->policy = new_policy->policy; |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 2327 | pr_debug("setting range\n"); |
Rafael J. Wysocki | 167a38d | 2019-02-20 00:26:30 +0100 | [diff] [blame] | 2328 | return cpufreq_driver->setpolicy(policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2329 | } |
| 2330 | |
Rafael J. Wysocki | 0a30076 | 2016-03-21 15:45:24 +0100 | [diff] [blame] | 2331 | if (new_policy->governor == policy->governor) { |
Rafael J. Wysocki | 2bb4059 | 2019-02-20 00:25:18 +0100 | [diff] [blame] | 2332 | pr_debug("governor limits update\n"); |
Rafael J. Wysocki | a92604b | 2016-05-14 01:01:46 +0200 | [diff] [blame] | 2333 | cpufreq_governor_limits(policy); |
Rafael J. Wysocki | d6ff44d | 2016-05-14 00:59:27 +0200 | [diff] [blame] | 2334 | return 0; |
Rafael J. Wysocki | 0a30076 | 2016-03-21 15:45:24 +0100 | [diff] [blame] | 2335 | } |
Rafael J. Wysocki | d9a789c | 2014-02-17 22:56:35 +0100 | [diff] [blame] | 2336 | |
| 2337 | pr_debug("governor switch\n"); |
| 2338 | |
| 2339 | /* save old, working values */ |
| 2340 | old_gov = policy->governor; |
| 2341 | /* end old governor */ |
| 2342 | if (old_gov) { |
Rafael J. Wysocki | 45482c7 | 2016-05-12 15:14:12 +0200 | [diff] [blame] | 2343 | cpufreq_stop_governor(policy); |
Rafael J. Wysocki | 36be341 | 2016-05-12 15:13:35 +0200 | [diff] [blame] | 2344 | cpufreq_exit_governor(policy); |
Rafael J. Wysocki | d9a789c | 2014-02-17 22:56:35 +0100 | [diff] [blame] | 2345 | } |
| 2346 | |
| 2347 | /* start new governor */ |
| 2348 | policy->governor = new_policy->governor; |
Rafael J. Wysocki | a92604b | 2016-05-14 01:01:46 +0200 | [diff] [blame] | 2349 | ret = cpufreq_init_governor(policy); |
Viresh Kumar | 4bc384a | 2015-07-18 11:31:03 +0530 | [diff] [blame] | 2350 | if (!ret) { |
Rafael J. Wysocki | 0a30076 | 2016-03-21 15:45:24 +0100 | [diff] [blame] | 2351 | ret = cpufreq_start_governor(policy); |
| 2352 | if (!ret) { |
Rafael J. Wysocki | 2bb4059 | 2019-02-20 00:25:18 +0100 | [diff] [blame] | 2353 | pr_debug("governor change\n"); |
Quentin Perret | 531b5c9 | 2018-12-03 09:56:21 +0000 | [diff] [blame] | 2354 | sched_cpufreq_governor_change(policy, old_gov); |
Rafael J. Wysocki | 0a30076 | 2016-03-21 15:45:24 +0100 | [diff] [blame] | 2355 | return 0; |
| 2356 | } |
Rafael J. Wysocki | b7898fd | 2016-03-30 03:47:49 +0200 | [diff] [blame] | 2357 | cpufreq_exit_governor(policy); |
Rafael J. Wysocki | d9a789c | 2014-02-17 22:56:35 +0100 | [diff] [blame] | 2358 | } |
| 2359 | |
| 2360 | /* new governor failed, so re-start old one */ |
| 2361 | pr_debug("starting governor %s failed\n", policy->governor->name); |
| 2362 | if (old_gov) { |
| 2363 | policy->governor = old_gov; |
Rafael J. Wysocki | a92604b | 2016-05-14 01:01:46 +0200 | [diff] [blame] | 2364 | if (cpufreq_init_governor(policy)) |
Viresh Kumar | 4bc384a | 2015-07-18 11:31:03 +0530 | [diff] [blame] | 2365 | policy->governor = NULL; |
| 2366 | else |
Rafael J. Wysocki | 0a30076 | 2016-03-21 15:45:24 +0100 | [diff] [blame] | 2367 | cpufreq_start_governor(policy); |
Rafael J. Wysocki | d9a789c | 2014-02-17 22:56:35 +0100 | [diff] [blame] | 2368 | } |
| 2369 | |
Viresh Kumar | 4bc384a | 2015-07-18 11:31:03 +0530 | [diff] [blame] | 2370 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | } |
| 2372 | |
| 2373 | /** |
Rafael J. Wysocki | a0dbb81 | 2019-02-20 00:22:51 +0100 | [diff] [blame] | 2374 | * cpufreq_update_policy - Re-evaluate an existing cpufreq policy. |
| 2375 | * @cpu: CPU to re-evaluate the policy for. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2376 | * |
Rafael J. Wysocki | a0dbb81 | 2019-02-20 00:22:51 +0100 | [diff] [blame] | 2377 | * Update the current frequency for the cpufreq policy of @cpu and use |
| 2378 | * cpufreq_set_policy() to re-apply the min and max limits saved in the |
| 2379 | * user_policy sub-structure of that policy, which triggers the evaluation |
| 2380 | * of policy notifiers and the cpufreq driver's ->verify() callback for the |
| 2381 | * policy in question, among other things. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2382 | */ |
Rafael J. Wysocki | 30248fe | 2016-11-18 13:59:21 +0100 | [diff] [blame] | 2383 | void cpufreq_update_policy(unsigned int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2384 | { |
Rafael J. Wysocki | 540a3758 | 2019-03-26 12:16:58 +0100 | [diff] [blame^] | 2385 | struct cpufreq_policy *policy = cpufreq_cpu_acquire(cpu); |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 2386 | struct cpufreq_policy new_policy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2387 | |
Aaron Plattner | fefa8ff | 2014-06-18 11:27:32 -0700 | [diff] [blame] | 2388 | if (!policy) |
Rafael J. Wysocki | 30248fe | 2016-11-18 13:59:21 +0100 | [diff] [blame] | 2389 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 | |
Viresh Kumar | bb176f7 | 2013-06-19 14:19:33 +0530 | [diff] [blame] | 2391 | /* |
| 2392 | * BIOS might change freq behind our back |
| 2393 | * -> ask driver for current freq and notify governors about a change |
| 2394 | */ |
Rafael J. Wysocki | 348a2ec | 2019-02-20 00:24:25 +0100 | [diff] [blame] | 2395 | if (cpufreq_driver->get && !cpufreq_driver->setpolicy && |
| 2396 | (cpufreq_suspended || WARN_ON(!cpufreq_update_current_freq(policy)))) |
| 2397 | goto unlock; |
Rafael J. Wysocki | 30248fe | 2016-11-18 13:59:21 +0100 | [diff] [blame] | 2398 | |
Rafael J. Wysocki | 348a2ec | 2019-02-20 00:24:25 +0100 | [diff] [blame] | 2399 | pr_debug("updating policy for CPU %u\n", cpu); |
| 2400 | memcpy(&new_policy, policy, sizeof(*policy)); |
| 2401 | new_policy.min = policy->user_policy.min; |
| 2402 | new_policy.max = policy->user_policy.max; |
Thomas Renninger | 0961dd0 | 2006-01-26 18:46:33 +0100 | [diff] [blame] | 2403 | |
Rafael J. Wysocki | 30248fe | 2016-11-18 13:59:21 +0100 | [diff] [blame] | 2404 | cpufreq_set_policy(policy, &new_policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2405 | |
Aaron Plattner | fefa8ff | 2014-06-18 11:27:32 -0700 | [diff] [blame] | 2406 | unlock: |
Rafael J. Wysocki | 540a3758 | 2019-03-26 12:16:58 +0100 | [diff] [blame^] | 2407 | cpufreq_cpu_release(policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2408 | } |
| 2409 | EXPORT_SYMBOL(cpufreq_update_policy); |
| 2410 | |
Rafael J. Wysocki | 5a25e3f | 2019-03-26 12:15:13 +0100 | [diff] [blame] | 2411 | /** |
| 2412 | * cpufreq_update_limits - Update policy limits for a given CPU. |
| 2413 | * @cpu: CPU to update the policy limits for. |
| 2414 | * |
| 2415 | * Invoke the driver's ->update_limits callback if present or call |
| 2416 | * cpufreq_update_policy() for @cpu. |
| 2417 | */ |
| 2418 | void cpufreq_update_limits(unsigned int cpu) |
| 2419 | { |
| 2420 | if (cpufreq_driver->update_limits) |
| 2421 | cpufreq_driver->update_limits(cpu); |
| 2422 | else |
| 2423 | cpufreq_update_policy(cpu); |
| 2424 | } |
| 2425 | EXPORT_SYMBOL_GPL(cpufreq_update_limits); |
| 2426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2427 | /********************************************************************* |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 2428 | * BOOST * |
| 2429 | *********************************************************************/ |
| 2430 | static int cpufreq_boost_set_sw(int state) |
| 2431 | { |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 2432 | struct cpufreq_policy *policy; |
| 2433 | int ret = -EINVAL; |
| 2434 | |
Viresh Kumar | f963735 | 2015-05-12 12:20:11 +0530 | [diff] [blame] | 2435 | for_each_active_policy(policy) { |
Viresh Kumar | f8bfc11 | 2016-06-03 10:58:47 +0530 | [diff] [blame] | 2436 | if (!policy->freq_table) |
| 2437 | continue; |
Viresh Kumar | 49f1856 | 2016-02-11 17:31:12 +0530 | [diff] [blame] | 2438 | |
Viresh Kumar | f8bfc11 | 2016-06-03 10:58:47 +0530 | [diff] [blame] | 2439 | ret = cpufreq_frequency_table_cpuinfo(policy, |
| 2440 | policy->freq_table); |
| 2441 | if (ret) { |
| 2442 | pr_err("%s: Policy frequency update failed\n", |
| 2443 | __func__); |
| 2444 | break; |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 2445 | } |
Viresh Kumar | f8bfc11 | 2016-06-03 10:58:47 +0530 | [diff] [blame] | 2446 | |
| 2447 | down_write(&policy->rwsem); |
| 2448 | policy->user_policy.max = policy->max; |
| 2449 | cpufreq_governor_limits(policy); |
| 2450 | up_write(&policy->rwsem); |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 2451 | } |
| 2452 | |
| 2453 | return ret; |
| 2454 | } |
| 2455 | |
| 2456 | int cpufreq_boost_trigger_state(int state) |
| 2457 | { |
| 2458 | unsigned long flags; |
| 2459 | int ret = 0; |
| 2460 | |
| 2461 | if (cpufreq_driver->boost_enabled == state) |
| 2462 | return 0; |
| 2463 | |
| 2464 | write_lock_irqsave(&cpufreq_driver_lock, flags); |
| 2465 | cpufreq_driver->boost_enabled = state; |
| 2466 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
| 2467 | |
| 2468 | ret = cpufreq_driver->set_boost(state); |
| 2469 | if (ret) { |
| 2470 | write_lock_irqsave(&cpufreq_driver_lock, flags); |
| 2471 | cpufreq_driver->boost_enabled = !state; |
| 2472 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
| 2473 | |
Joe Perches | e837f9b | 2014-03-11 10:03:00 -0700 | [diff] [blame] | 2474 | pr_err("%s: Cannot %s BOOST\n", |
| 2475 | __func__, state ? "enable" : "disable"); |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 2476 | } |
| 2477 | |
| 2478 | return ret; |
| 2479 | } |
| 2480 | |
Rafael J. Wysocki | 41669da | 2015-12-27 00:23:48 +0100 | [diff] [blame] | 2481 | static bool cpufreq_boost_supported(void) |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 2482 | { |
Rafael J. Wysocki | 7a6c79f | 2015-12-27 00:27:38 +0100 | [diff] [blame] | 2483 | return likely(cpufreq_driver) && cpufreq_driver->set_boost; |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 2484 | } |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 2485 | |
Viresh Kumar | 44139ed | 2015-07-29 16:23:09 +0530 | [diff] [blame] | 2486 | static int create_boost_sysfs_file(void) |
| 2487 | { |
| 2488 | int ret; |
| 2489 | |
Viresh Kumar | c82bd44 | 2015-10-15 21:35:23 +0530 | [diff] [blame] | 2490 | ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr); |
Viresh Kumar | 44139ed | 2015-07-29 16:23:09 +0530 | [diff] [blame] | 2491 | if (ret) |
| 2492 | pr_err("%s: cannot register global BOOST sysfs file\n", |
| 2493 | __func__); |
| 2494 | |
| 2495 | return ret; |
| 2496 | } |
| 2497 | |
| 2498 | static void remove_boost_sysfs_file(void) |
| 2499 | { |
| 2500 | if (cpufreq_boost_supported()) |
Viresh Kumar | c82bd44 | 2015-10-15 21:35:23 +0530 | [diff] [blame] | 2501 | sysfs_remove_file(cpufreq_global_kobject, &boost.attr); |
Viresh Kumar | 44139ed | 2015-07-29 16:23:09 +0530 | [diff] [blame] | 2502 | } |
| 2503 | |
| 2504 | int cpufreq_enable_boost_support(void) |
| 2505 | { |
| 2506 | if (!cpufreq_driver) |
| 2507 | return -EINVAL; |
| 2508 | |
| 2509 | if (cpufreq_boost_supported()) |
| 2510 | return 0; |
| 2511 | |
Rafael J. Wysocki | 7a6c79f | 2015-12-27 00:27:38 +0100 | [diff] [blame] | 2512 | cpufreq_driver->set_boost = cpufreq_boost_set_sw; |
Viresh Kumar | 44139ed | 2015-07-29 16:23:09 +0530 | [diff] [blame] | 2513 | |
| 2514 | /* This will get removed on driver unregister */ |
| 2515 | return create_boost_sysfs_file(); |
| 2516 | } |
| 2517 | EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support); |
| 2518 | |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 2519 | int cpufreq_boost_enabled(void) |
| 2520 | { |
| 2521 | return cpufreq_driver->boost_enabled; |
| 2522 | } |
| 2523 | EXPORT_SYMBOL_GPL(cpufreq_boost_enabled); |
| 2524 | |
| 2525 | /********************************************************************* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2526 | * REGISTER / UNREGISTER CPUFREQ DRIVER * |
| 2527 | *********************************************************************/ |
Sebastian Andrzej Siewior | 27622b0 | 2016-09-06 19:04:48 +0200 | [diff] [blame] | 2528 | static enum cpuhp_state hp_online; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2529 | |
Chen Yu | c4a3fa2 | 2017-04-09 13:45:16 +0800 | [diff] [blame] | 2530 | static int cpuhp_cpufreq_online(unsigned int cpu) |
| 2531 | { |
| 2532 | cpufreq_online(cpu); |
| 2533 | |
| 2534 | return 0; |
| 2535 | } |
| 2536 | |
| 2537 | static int cpuhp_cpufreq_offline(unsigned int cpu) |
| 2538 | { |
| 2539 | cpufreq_offline(cpu); |
| 2540 | |
| 2541 | return 0; |
| 2542 | } |
| 2543 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2544 | /** |
| 2545 | * cpufreq_register_driver - register a CPU Frequency driver |
| 2546 | * @driver_data: A struct cpufreq_driver containing the values# |
| 2547 | * submitted by the CPU Frequency driver. |
| 2548 | * |
Viresh Kumar | bb176f7 | 2013-06-19 14:19:33 +0530 | [diff] [blame] | 2549 | * Registers a CPU Frequency driver to this core code. This code |
Eric Biggers | 63af405 | 2016-02-20 21:50:01 -0600 | [diff] [blame] | 2550 | * returns zero on success, -EEXIST when another driver got here first |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 2551 | * (and isn't unregistered in the meantime). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2552 | * |
| 2553 | */ |
Linus Torvalds | 221dee2 | 2007-02-26 14:55:48 -0800 | [diff] [blame] | 2554 | int cpufreq_register_driver(struct cpufreq_driver *driver_data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2555 | { |
| 2556 | unsigned long flags; |
| 2557 | int ret; |
| 2558 | |
Konrad Rzeszutek Wilk | a7b422c | 2012-03-13 19:18:39 -0400 | [diff] [blame] | 2559 | if (cpufreq_disabled()) |
| 2560 | return -ENODEV; |
| 2561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2562 | if (!driver_data || !driver_data->verify || !driver_data->init || |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 2563 | !(driver_data->setpolicy || driver_data->target_index || |
Rafael J. Wysocki | 9832235 | 2014-03-19 12:48:30 +0100 | [diff] [blame] | 2564 | driver_data->target) || |
| 2565 | (driver_data->setpolicy && (driver_data->target_index || |
Viresh Kumar | 1c03a2d | 2014-06-02 22:49:28 +0530 | [diff] [blame] | 2566 | driver_data->target)) || |
Viresh Kumar | a9a22b5 | 2019-02-14 16:16:21 +0530 | [diff] [blame] | 2567 | (!driver_data->get_intermediate != !driver_data->target_intermediate) || |
Viresh Kumar | 91a12e9 | 2019-02-12 16:36:04 +0530 | [diff] [blame] | 2568 | (!driver_data->online != !driver_data->offline)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2569 | return -EINVAL; |
| 2570 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 2571 | pr_debug("trying to register driver %s\n", driver_data->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2572 | |
Rafael J. Wysocki | fdd320d | 2015-07-30 01:45:07 +0200 | [diff] [blame] | 2573 | /* Protect against concurrent CPU online/offline. */ |
Sebastian Andrzej Siewior | a92551e | 2017-05-24 10:15:20 +0200 | [diff] [blame] | 2574 | cpus_read_lock(); |
Rafael J. Wysocki | fdd320d | 2015-07-30 01:45:07 +0200 | [diff] [blame] | 2575 | |
Nathan Zimmer | 0d1857a | 2013-02-22 16:24:34 +0000 | [diff] [blame] | 2576 | write_lock_irqsave(&cpufreq_driver_lock, flags); |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 2577 | if (cpufreq_driver) { |
Nathan Zimmer | 0d1857a | 2013-02-22 16:24:34 +0000 | [diff] [blame] | 2578 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
Rafael J. Wysocki | fdd320d | 2015-07-30 01:45:07 +0200 | [diff] [blame] | 2579 | ret = -EEXIST; |
| 2580 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2581 | } |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 2582 | cpufreq_driver = driver_data; |
Nathan Zimmer | 0d1857a | 2013-02-22 16:24:34 +0000 | [diff] [blame] | 2583 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2584 | |
Viresh Kumar | bc68b7d | 2015-01-02 12:34:30 +0530 | [diff] [blame] | 2585 | if (driver_data->setpolicy) |
| 2586 | driver_data->flags |= CPUFREQ_CONST_LOOPS; |
| 2587 | |
Rafael J. Wysocki | 7a6c79f | 2015-12-27 00:27:38 +0100 | [diff] [blame] | 2588 | if (cpufreq_boost_supported()) { |
| 2589 | ret = create_boost_sysfs_file(); |
| 2590 | if (ret) |
| 2591 | goto err_null_driver; |
| 2592 | } |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 2593 | |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 2594 | ret = subsys_interface_register(&cpufreq_interface); |
Jiri Slaby | 8f5bc2a | 2011-03-01 17:41:10 +0100 | [diff] [blame] | 2595 | if (ret) |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 2596 | goto err_boost_unreg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2597 | |
Viresh Kumar | ce1bcfe | 2015-01-02 12:34:35 +0530 | [diff] [blame] | 2598 | if (!(cpufreq_driver->flags & CPUFREQ_STICKY) && |
| 2599 | list_empty(&cpufreq_policy_list)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2600 | /* if all ->init() calls failed, unregister */ |
David Arcari | 6c77003 | 2017-05-26 11:37:31 -0400 | [diff] [blame] | 2601 | ret = -ENODEV; |
Viresh Kumar | ce1bcfe | 2015-01-02 12:34:35 +0530 | [diff] [blame] | 2602 | pr_debug("%s: No CPU initialized for driver %s\n", __func__, |
| 2603 | driver_data->name); |
| 2604 | goto err_if_unreg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2605 | } |
| 2606 | |
Sebastian Andrzej Siewior | a92551e | 2017-05-24 10:15:20 +0200 | [diff] [blame] | 2607 | ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN, |
| 2608 | "cpufreq:online", |
| 2609 | cpuhp_cpufreq_online, |
| 2610 | cpuhp_cpufreq_offline); |
Sebastian Andrzej Siewior | 27622b0 | 2016-09-06 19:04:48 +0200 | [diff] [blame] | 2611 | if (ret < 0) |
| 2612 | goto err_if_unreg; |
| 2613 | hp_online = ret; |
Sebastian Andrzej Siewior | 5372e05 | 2016-09-20 16:56:28 +0200 | [diff] [blame] | 2614 | ret = 0; |
Sebastian Andrzej Siewior | 27622b0 | 2016-09-06 19:04:48 +0200 | [diff] [blame] | 2615 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 2616 | pr_debug("driver %s up and running\n", driver_data->name); |
Pankaj Gupta | 3834abb | 2016-05-16 11:07:19 +0000 | [diff] [blame] | 2617 | goto out; |
Rafael J. Wysocki | fdd320d | 2015-07-30 01:45:07 +0200 | [diff] [blame] | 2618 | |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 2619 | err_if_unreg: |
| 2620 | subsys_interface_unregister(&cpufreq_interface); |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 2621 | err_boost_unreg: |
Viresh Kumar | 44139ed | 2015-07-29 16:23:09 +0530 | [diff] [blame] | 2622 | remove_boost_sysfs_file(); |
Jiri Slaby | 8f5bc2a | 2011-03-01 17:41:10 +0100 | [diff] [blame] | 2623 | err_null_driver: |
Nathan Zimmer | 0d1857a | 2013-02-22 16:24:34 +0000 | [diff] [blame] | 2624 | write_lock_irqsave(&cpufreq_driver_lock, flags); |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 2625 | cpufreq_driver = NULL; |
Nathan Zimmer | 0d1857a | 2013-02-22 16:24:34 +0000 | [diff] [blame] | 2626 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
Pankaj Gupta | 3834abb | 2016-05-16 11:07:19 +0000 | [diff] [blame] | 2627 | out: |
Sebastian Andrzej Siewior | a92551e | 2017-05-24 10:15:20 +0200 | [diff] [blame] | 2628 | cpus_read_unlock(); |
Pankaj Gupta | 3834abb | 2016-05-16 11:07:19 +0000 | [diff] [blame] | 2629 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2630 | } |
| 2631 | EXPORT_SYMBOL_GPL(cpufreq_register_driver); |
| 2632 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2633 | /** |
| 2634 | * cpufreq_unregister_driver - unregister the current CPUFreq driver |
| 2635 | * |
Viresh Kumar | bb176f7 | 2013-06-19 14:19:33 +0530 | [diff] [blame] | 2636 | * Unregister the current CPUFreq driver. Only call this if you have |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2637 | * the right to do so, i.e. if you have succeeded in initialising before! |
| 2638 | * Returns zero if successful, and -EINVAL if the cpufreq_driver is |
| 2639 | * currently not initialised. |
| 2640 | */ |
Linus Torvalds | 221dee2 | 2007-02-26 14:55:48 -0800 | [diff] [blame] | 2641 | int cpufreq_unregister_driver(struct cpufreq_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2642 | { |
| 2643 | unsigned long flags; |
| 2644 | |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 2645 | if (!cpufreq_driver || (driver != cpufreq_driver)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2646 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2647 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 2648 | pr_debug("unregistering driver %s\n", driver->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2649 | |
Sebastian Andrzej Siewior | 454d3a2 | 2015-07-22 17:59:11 +0200 | [diff] [blame] | 2650 | /* Protect against concurrent cpu hotplug */ |
Sebastian Andrzej Siewior | a92551e | 2017-05-24 10:15:20 +0200 | [diff] [blame] | 2651 | cpus_read_lock(); |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 2652 | subsys_interface_unregister(&cpufreq_interface); |
Viresh Kumar | 44139ed | 2015-07-29 16:23:09 +0530 | [diff] [blame] | 2653 | remove_boost_sysfs_file(); |
Sebastian Andrzej Siewior | a92551e | 2017-05-24 10:15:20 +0200 | [diff] [blame] | 2654 | cpuhp_remove_state_nocalls_cpuslocked(hp_online); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2655 | |
Nathan Zimmer | 0d1857a | 2013-02-22 16:24:34 +0000 | [diff] [blame] | 2656 | write_lock_irqsave(&cpufreq_driver_lock, flags); |
Viresh Kumar | 6eed940 | 2013-08-06 22:53:11 +0530 | [diff] [blame] | 2657 | |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 2658 | cpufreq_driver = NULL; |
Viresh Kumar | 6eed940 | 2013-08-06 22:53:11 +0530 | [diff] [blame] | 2659 | |
Nathan Zimmer | 0d1857a | 2013-02-22 16:24:34 +0000 | [diff] [blame] | 2660 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
Sebastian Andrzej Siewior | a92551e | 2017-05-24 10:15:20 +0200 | [diff] [blame] | 2661 | cpus_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2662 | |
| 2663 | return 0; |
| 2664 | } |
| 2665 | EXPORT_SYMBOL_GPL(cpufreq_unregister_driver); |
Venkatesh Pallipadi | 5a01f2e | 2007-02-05 16:12:44 -0800 | [diff] [blame] | 2666 | |
Doug Anderson | 90de2a4 | 2014-12-23 22:09:48 -0800 | [diff] [blame] | 2667 | /* |
| 2668 | * Stop cpufreq at shutdown to make sure it isn't holding any locks |
| 2669 | * or mutexes when secondary CPUs are halted. |
| 2670 | */ |
| 2671 | static struct syscore_ops cpufreq_syscore_ops = { |
| 2672 | .shutdown = cpufreq_suspend, |
| 2673 | }; |
| 2674 | |
Viresh Kumar | c82bd44 | 2015-10-15 21:35:23 +0530 | [diff] [blame] | 2675 | struct kobject *cpufreq_global_kobject; |
| 2676 | EXPORT_SYMBOL(cpufreq_global_kobject); |
| 2677 | |
Venkatesh Pallipadi | 5a01f2e | 2007-02-05 16:12:44 -0800 | [diff] [blame] | 2678 | static int __init cpufreq_core_init(void) |
| 2679 | { |
Konrad Rzeszutek Wilk | a7b422c | 2012-03-13 19:18:39 -0400 | [diff] [blame] | 2680 | if (cpufreq_disabled()) |
| 2681 | return -ENODEV; |
| 2682 | |
Viresh Kumar | 8eec102 | 2015-10-15 21:35:22 +0530 | [diff] [blame] | 2683 | cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj); |
Thomas Renninger | 8aa84ad | 2009-07-24 15:25:05 +0200 | [diff] [blame] | 2684 | BUG_ON(!cpufreq_global_kobject); |
| 2685 | |
Doug Anderson | 90de2a4 | 2014-12-23 22:09:48 -0800 | [diff] [blame] | 2686 | register_syscore_ops(&cpufreq_syscore_ops); |
| 2687 | |
Venkatesh Pallipadi | 5a01f2e | 2007-02-05 16:12:44 -0800 | [diff] [blame] | 2688 | return 0; |
| 2689 | } |
Len Brown | d82f269 | 2017-02-28 16:44:16 -0500 | [diff] [blame] | 2690 | module_param(off, int, 0444); |
Venkatesh Pallipadi | 5a01f2e | 2007-02-05 16:12:44 -0800 | [diff] [blame] | 2691 | core_initcall(cpufreq_core_init); |