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