Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/thermal/cpu_cooling.c |
| 3 | * |
| 4 | * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com) |
| 5 | * Copyright (C) 2012 Amit Daniel <amit.kachhap@linaro.org> |
| 6 | * |
Viresh Kumar | 73904cb | 2014-12-04 09:42:08 +0530 | [diff] [blame] | 7 | * Copyright (C) 2014 Viresh Kumar <viresh.kumar@linaro.org> |
| 8 | * |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 9 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; version 2 of the License. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 22 | * |
| 23 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 24 | */ |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 25 | #include <linux/module.h> |
| 26 | #include <linux/thermal.h> |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 27 | #include <linux/cpufreq.h> |
| 28 | #include <linux/err.h> |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 29 | #include <linux/pm_opp.h> |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 30 | #include <linux/slab.h> |
| 31 | #include <linux/cpu.h> |
| 32 | #include <linux/cpu_cooling.h> |
Lina Iyer | 97a13ed | 2016-07-15 14:53:58 -0600 | [diff] [blame] | 33 | #include <linux/sched.h> |
Lina Iyer | 986fde1 | 2016-02-23 13:08:31 -0700 | [diff] [blame] | 34 | #include <linux/of_device.h> |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 35 | |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 36 | #include <trace/events/thermal.h> |
| 37 | |
Viresh Kumar | 07d888d | 2014-12-04 09:41:49 +0530 | [diff] [blame] | 38 | /* |
| 39 | * Cooling state <-> CPUFreq frequency |
| 40 | * |
| 41 | * Cooling states are translated to frequencies throughout this driver and this |
| 42 | * is the relation between them. |
| 43 | * |
| 44 | * Highest cooling state corresponds to lowest possible frequency. |
| 45 | * |
| 46 | * i.e. |
| 47 | * level 0 --> 1st Max Freq |
| 48 | * level 1 --> 2nd Max Freq |
| 49 | * ... |
Lina Iyer | 97a13ed | 2016-07-15 14:53:58 -0600 | [diff] [blame] | 50 | * leven n --> core isolated |
Viresh Kumar | 07d888d | 2014-12-04 09:41:49 +0530 | [diff] [blame] | 51 | */ |
| 52 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 53 | /** |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 54 | * struct power_table - frequency to power conversion |
| 55 | * @frequency: frequency in KHz |
| 56 | * @power: power in mW |
| 57 | * |
| 58 | * This structure is built when the cooling device registers and helps |
| 59 | * in translating frequency to power and viceversa. |
| 60 | */ |
| 61 | struct power_table { |
| 62 | u32 frequency; |
| 63 | u32 power; |
| 64 | }; |
| 65 | |
| 66 | /** |
Eduardo Valentin | 3b3c074 | 2013-04-17 17:11:56 +0000 | [diff] [blame] | 67 | * struct cpufreq_cooling_device - data for cooling device with cpufreq |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 68 | * @id: unique integer value corresponding to each cpufreq_cooling_device |
| 69 | * registered. |
Eduardo Valentin | 3b3c074 | 2013-04-17 17:11:56 +0000 | [diff] [blame] | 70 | * @cool_dev: thermal_cooling_device pointer to keep track of the |
| 71 | * registered cooling device. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 72 | * @cpufreq_state: integer value representing the current state of cpufreq |
| 73 | * cooling devices. |
Viresh Kumar | 59f0d21 | 2015-07-30 12:40:33 +0530 | [diff] [blame] | 74 | * @clipped_freq: integer value representing the absolute value of the clipped |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 75 | * frequency. |
Ram Chandrasekar | d8e4bf2 | 2016-09-21 17:08:06 -0600 | [diff] [blame] | 76 | * @cpufreq_floor_state: integer value representing the frequency floor state |
| 77 | * of cpufreq cooling devices. |
| 78 | * @floor_freq: integer value representing the absolute value of the floor |
| 79 | * frequency. |
Lina Iyer | 97a13ed | 2016-07-15 14:53:58 -0600 | [diff] [blame] | 80 | * @max_level: maximum cooling level. [0..max_level-1: <freq> |
| 81 | * max_level: Core unavailable] |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 82 | * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device. |
Javi Merino | fc4de35 | 2014-12-15 16:55:52 +0000 | [diff] [blame] | 83 | * @node: list_head to link all cpufreq_cooling_device together. |
Hugh Kang | 0744f13 | 2016-09-07 09:35:39 +0900 | [diff] [blame] | 84 | * @last_load: load measured by the latest call to cpufreq_get_requested_power() |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 85 | * @time_in_idle: previous reading of the absolute time that this cpu was idle |
| 86 | * @time_in_idle_timestamp: wall time of the last invocation of |
| 87 | * get_cpu_idle_time_us() |
| 88 | * @dyn_power_table: array of struct power_table for frequency to power |
| 89 | * conversion, sorted in ascending order. |
| 90 | * @dyn_power_table_entries: number of entries in the @dyn_power_table array |
| 91 | * @cpu_dev: the first cpu_device from @allowed_cpus that has OPPs registered |
| 92 | * @plat_get_static_power: callback to calculate the static power |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 93 | * |
Viresh Kumar | beca605 | 2014-12-04 09:41:48 +0530 | [diff] [blame] | 94 | * This structure is required for keeping information of each registered |
| 95 | * cpufreq_cooling_device. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 96 | */ |
| 97 | struct cpufreq_cooling_device { |
| 98 | int id; |
| 99 | struct thermal_cooling_device *cool_dev; |
| 100 | unsigned int cpufreq_state; |
Viresh Kumar | 59f0d21 | 2015-07-30 12:40:33 +0530 | [diff] [blame] | 101 | unsigned int clipped_freq; |
Ram Chandrasekar | d8e4bf2 | 2016-09-21 17:08:06 -0600 | [diff] [blame] | 102 | unsigned int cpufreq_floor_state; |
| 103 | unsigned int floor_freq; |
Viresh Kumar | dcc6c7f | 2014-12-04 09:42:02 +0530 | [diff] [blame] | 104 | unsigned int max_level; |
Viresh Kumar | f685901 | 2014-12-04 09:42:06 +0530 | [diff] [blame] | 105 | unsigned int *freq_table; /* In descending order */ |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 106 | struct cpumask allowed_cpus; |
Yadwinder Singh Brar | 2dcd851 | 2014-11-07 19:12:29 +0530 | [diff] [blame] | 107 | struct list_head node; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 108 | u32 last_load; |
| 109 | u64 *time_in_idle; |
| 110 | u64 *time_in_idle_timestamp; |
| 111 | struct power_table *dyn_power_table; |
| 112 | int dyn_power_table_entries; |
| 113 | struct device *cpu_dev; |
| 114 | get_static_t plat_get_static_power; |
Lina Iyer | 986fde1 | 2016-02-23 13:08:31 -0700 | [diff] [blame] | 115 | struct cpu_cooling_ops *plat_ops; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 116 | }; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 117 | static DEFINE_IDR(cpufreq_idr); |
hongbo.zhang | 160b7d8 | 2012-10-30 17:48:59 +0100 | [diff] [blame] | 118 | static DEFINE_MUTEX(cooling_cpufreq_lock); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 119 | |
Russell King | 02373d7 | 2015-08-12 15:22:16 +0530 | [diff] [blame] | 120 | static unsigned int cpufreq_dev_count; |
| 121 | |
| 122 | static DEFINE_MUTEX(cooling_list_lock); |
Yadwinder Singh Brar | 2dcd851 | 2014-11-07 19:12:29 +0530 | [diff] [blame] | 123 | static LIST_HEAD(cpufreq_dev_list); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 124 | |
| 125 | /** |
| 126 | * get_idr - function to get a unique id. |
| 127 | * @idr: struct idr * handle used to create a id. |
| 128 | * @id: int * value generated by this function. |
Eduardo Valentin | 79491e5 | 2013-04-17 17:11:59 +0000 | [diff] [blame] | 129 | * |
| 130 | * This function will populate @id with an unique |
| 131 | * id, using the idr API. |
| 132 | * |
| 133 | * Return: 0 on success, an error code on failure. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 134 | */ |
| 135 | static int get_idr(struct idr *idr, int *id) |
| 136 | { |
Tejun Heo | 6deb69f | 2013-02-27 17:04:46 -0800 | [diff] [blame] | 137 | int ret; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 138 | |
| 139 | mutex_lock(&cooling_cpufreq_lock); |
Tejun Heo | 6deb69f | 2013-02-27 17:04:46 -0800 | [diff] [blame] | 140 | ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 141 | mutex_unlock(&cooling_cpufreq_lock); |
Tejun Heo | 6deb69f | 2013-02-27 17:04:46 -0800 | [diff] [blame] | 142 | if (unlikely(ret < 0)) |
| 143 | return ret; |
| 144 | *id = ret; |
Eduardo Valentin | 79491e5 | 2013-04-17 17:11:59 +0000 | [diff] [blame] | 145 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * release_idr - function to free the unique id. |
| 151 | * @idr: struct idr * handle used for creating the id. |
| 152 | * @id: int value representing the unique id. |
| 153 | */ |
| 154 | static void release_idr(struct idr *idr, int id) |
| 155 | { |
| 156 | mutex_lock(&cooling_cpufreq_lock); |
| 157 | idr_remove(idr, id); |
| 158 | mutex_unlock(&cooling_cpufreq_lock); |
| 159 | } |
| 160 | |
| 161 | /* Below code defines functions to be used for cpufreq as cooling device */ |
| 162 | |
| 163 | /** |
Viresh Kumar | 4843c4a | 2014-12-04 09:42:07 +0530 | [diff] [blame] | 164 | * get_level: Find the level for a particular frequency |
Viresh Kumar | b9f8b41 | 2014-12-04 09:42:05 +0530 | [diff] [blame] | 165 | * @cpufreq_dev: cpufreq_dev for which the property is required |
Viresh Kumar | 4843c4a | 2014-12-04 09:42:07 +0530 | [diff] [blame] | 166 | * @freq: Frequency |
Eduardo Valentin | 82b9ee4 | 2013-04-17 17:12:00 +0000 | [diff] [blame] | 167 | * |
Viresh Kumar | 4843c4a | 2014-12-04 09:42:07 +0530 | [diff] [blame] | 168 | * Return: level on success, THERMAL_CSTATE_INVALID on error. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 169 | */ |
Viresh Kumar | 4843c4a | 2014-12-04 09:42:07 +0530 | [diff] [blame] | 170 | static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_dev, |
| 171 | unsigned int freq) |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 172 | { |
Viresh Kumar | 4843c4a | 2014-12-04 09:42:07 +0530 | [diff] [blame] | 173 | unsigned long level; |
Eduardo Valentin | 79491e5 | 2013-04-17 17:11:59 +0000 | [diff] [blame] | 174 | |
Lina Iyer | 97a13ed | 2016-07-15 14:53:58 -0600 | [diff] [blame] | 175 | for (level = 0; level < cpufreq_dev->max_level; level++) { |
Viresh Kumar | 4843c4a | 2014-12-04 09:42:07 +0530 | [diff] [blame] | 176 | if (freq == cpufreq_dev->freq_table[level]) |
| 177 | return level; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 178 | |
Viresh Kumar | 4843c4a | 2014-12-04 09:42:07 +0530 | [diff] [blame] | 179 | if (freq > cpufreq_dev->freq_table[level]) |
| 180 | break; |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 181 | } |
Zhang Rui | a116776 | 2014-01-02 11:57:48 +0800 | [diff] [blame] | 182 | |
Viresh Kumar | 4843c4a | 2014-12-04 09:42:07 +0530 | [diff] [blame] | 183 | return THERMAL_CSTATE_INVALID; |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 184 | } |
| 185 | |
Eduardo Valentin | 44952d3 | 2013-04-17 17:12:05 +0000 | [diff] [blame] | 186 | /** |
Viresh Kumar | 728c03c | 2014-12-04 09:41:47 +0530 | [diff] [blame] | 187 | * cpufreq_cooling_get_level - for a given cpu, return the cooling level. |
Eduardo Valentin | 44952d3 | 2013-04-17 17:12:05 +0000 | [diff] [blame] | 188 | * @cpu: cpu for which the level is required |
| 189 | * @freq: the frequency of interest |
| 190 | * |
| 191 | * This function will match the cooling level corresponding to the |
| 192 | * requested @freq and return it. |
| 193 | * |
| 194 | * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID |
| 195 | * otherwise. |
| 196 | */ |
Zhang Rui | 57df810 | 2013-02-08 14:52:06 +0800 | [diff] [blame] | 197 | unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq) |
| 198 | { |
Viresh Kumar | b9f8b41 | 2014-12-04 09:42:05 +0530 | [diff] [blame] | 199 | struct cpufreq_cooling_device *cpufreq_dev; |
Zhang Rui | 57df810 | 2013-02-08 14:52:06 +0800 | [diff] [blame] | 200 | |
Russell King | 02373d7 | 2015-08-12 15:22:16 +0530 | [diff] [blame] | 201 | mutex_lock(&cooling_list_lock); |
Viresh Kumar | b9f8b41 | 2014-12-04 09:42:05 +0530 | [diff] [blame] | 202 | list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) { |
| 203 | if (cpumask_test_cpu(cpu, &cpufreq_dev->allowed_cpus)) { |
Russell King | 02373d7 | 2015-08-12 15:22:16 +0530 | [diff] [blame] | 204 | mutex_unlock(&cooling_list_lock); |
Viresh Kumar | 4843c4a | 2014-12-04 09:42:07 +0530 | [diff] [blame] | 205 | return get_level(cpufreq_dev, freq); |
Viresh Kumar | b9f8b41 | 2014-12-04 09:42:05 +0530 | [diff] [blame] | 206 | } |
| 207 | } |
Russell King | 02373d7 | 2015-08-12 15:22:16 +0530 | [diff] [blame] | 208 | mutex_unlock(&cooling_list_lock); |
Eduardo Valentin | 79491e5 | 2013-04-17 17:11:59 +0000 | [diff] [blame] | 209 | |
Viresh Kumar | b9f8b41 | 2014-12-04 09:42:05 +0530 | [diff] [blame] | 210 | pr_err("%s: cpu:%d not part of any cooling device\n", __func__, cpu); |
| 211 | return THERMAL_CSTATE_INVALID; |
Zhang Rui | 57df810 | 2013-02-08 14:52:06 +0800 | [diff] [blame] | 212 | } |
Eduardo Valentin | 243dbd9 | 2013-04-17 17:11:57 +0000 | [diff] [blame] | 213 | EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level); |
Zhang Rui | 57df810 | 2013-02-08 14:52:06 +0800 | [diff] [blame] | 214 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 215 | /** |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 216 | * cpufreq_thermal_notifier - notifier callback for cpufreq policy change. |
| 217 | * @nb: struct notifier_block * with callback info. |
| 218 | * @event: value showing cpufreq event for which this function invoked. |
| 219 | * @data: callback-specific data |
Eduardo Valentin | bab3055 | 2013-04-17 17:12:09 +0000 | [diff] [blame] | 220 | * |
Javi Merino | 9746b6e | 2014-06-25 18:11:17 +0100 | [diff] [blame] | 221 | * Callback to hijack the notification on cpufreq policy transition. |
Eduardo Valentin | bab3055 | 2013-04-17 17:12:09 +0000 | [diff] [blame] | 222 | * Every time there is a change in policy, we will intercept and |
| 223 | * update the cpufreq policy with thermal constraints. |
| 224 | * |
| 225 | * Return: 0 (success) |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 226 | */ |
| 227 | static int cpufreq_thermal_notifier(struct notifier_block *nb, |
Eduardo Valentin | 5fda7f6 | 2013-04-17 17:12:11 +0000 | [diff] [blame] | 228 | unsigned long event, void *data) |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 229 | { |
| 230 | struct cpufreq_policy *policy = data; |
Ram Chandrasekar | d8e4bf2 | 2016-09-21 17:08:06 -0600 | [diff] [blame] | 231 | unsigned long clipped_freq, floor_freq; |
Yadwinder Singh Brar | 2dcd851 | 2014-11-07 19:12:29 +0530 | [diff] [blame] | 232 | struct cpufreq_cooling_device *cpufreq_dev; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 233 | |
Viresh Kumar | a24af23 | 2015-07-30 12:40:32 +0530 | [diff] [blame] | 234 | if (event != CPUFREQ_ADJUST) |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 235 | return NOTIFY_DONE; |
Viresh Kumar | a24af23 | 2015-07-30 12:40:32 +0530 | [diff] [blame] | 236 | |
| 237 | mutex_lock(&cooling_list_lock); |
| 238 | list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) { |
| 239 | if (!cpumask_test_cpu(policy->cpu, &cpufreq_dev->allowed_cpus)) |
| 240 | continue; |
| 241 | |
Viresh Kumar | 1afb9c5 | 2015-07-30 12:40:35 +0530 | [diff] [blame] | 242 | /* |
| 243 | * policy->max is the maximum allowed frequency defined by user |
| 244 | * and clipped_freq is the maximum that thermal constraints |
| 245 | * allow. |
| 246 | * |
| 247 | * If clipped_freq is lower than policy->max, then we need to |
| 248 | * readjust policy->max. |
| 249 | * |
| 250 | * But, if clipped_freq is greater than policy->max, we don't |
| 251 | * need to do anything. |
Ram Chandrasekar | d8e4bf2 | 2016-09-21 17:08:06 -0600 | [diff] [blame] | 252 | * |
| 253 | * Similarly, if policy minimum set by the user is less than |
| 254 | * the floor_frequency, then adjust the policy->min. |
Viresh Kumar | 1afb9c5 | 2015-07-30 12:40:35 +0530 | [diff] [blame] | 255 | */ |
Viresh Kumar | abcbcc2 | 2015-07-30 12:40:34 +0530 | [diff] [blame] | 256 | clipped_freq = cpufreq_dev->clipped_freq; |
Ram Chandrasekar | d8e4bf2 | 2016-09-21 17:08:06 -0600 | [diff] [blame] | 257 | floor_freq = cpufreq_dev->floor_freq; |
Viresh Kumar | a24af23 | 2015-07-30 12:40:32 +0530 | [diff] [blame] | 258 | |
Ram Chandrasekar | d8e4bf2 | 2016-09-21 17:08:06 -0600 | [diff] [blame] | 259 | if (policy->max > clipped_freq || policy->min < floor_freq) |
| 260 | cpufreq_verify_within_limits(policy, floor_freq, |
| 261 | clipped_freq); |
Viresh Kumar | a24af23 | 2015-07-30 12:40:32 +0530 | [diff] [blame] | 262 | break; |
Yadwinder Singh Brar | 2dcd851 | 2014-11-07 19:12:29 +0530 | [diff] [blame] | 263 | } |
Viresh Kumar | a24af23 | 2015-07-30 12:40:32 +0530 | [diff] [blame] | 264 | mutex_unlock(&cooling_list_lock); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 265 | |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 266 | return NOTIFY_OK; |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * build_dyn_power_table() - create a dynamic power to frequency table |
| 271 | * @cpufreq_device: the cpufreq cooling device in which to store the table |
| 272 | * @capacitance: dynamic power coefficient for these cpus |
| 273 | * |
| 274 | * Build a dynamic power to frequency table for this cpu and store it |
| 275 | * in @cpufreq_device. This table will be used in cpu_power_to_freq() and |
| 276 | * cpu_freq_to_power() to convert between power and frequency |
| 277 | * efficiently. Power is stored in mW, frequency in KHz. The |
| 278 | * resulting table is in ascending order. |
| 279 | * |
Javi Merino | 459ac37 | 2015-08-17 19:21:42 +0100 | [diff] [blame] | 280 | * Return: 0 on success, -EINVAL if there are no OPPs for any CPUs, |
| 281 | * -ENOMEM if we run out of memory or -EAGAIN if an OPP was |
| 282 | * added/enabled while the function was executing. |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 283 | */ |
| 284 | static int build_dyn_power_table(struct cpufreq_cooling_device *cpufreq_device, |
| 285 | u32 capacitance) |
| 286 | { |
| 287 | struct power_table *power_table; |
| 288 | struct dev_pm_opp *opp; |
| 289 | struct device *dev = NULL; |
Javi Merino | eba4f88 | 2015-08-17 19:21:43 +0100 | [diff] [blame] | 290 | int num_opps = 0, cpu, i, ret = 0; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 291 | unsigned long freq; |
| 292 | |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 293 | for_each_cpu(cpu, &cpufreq_device->allowed_cpus) { |
| 294 | dev = get_cpu_device(cpu); |
| 295 | if (!dev) { |
| 296 | dev_warn(&cpufreq_device->cool_dev->device, |
| 297 | "No cpu device for cpu %d\n", cpu); |
| 298 | continue; |
| 299 | } |
| 300 | |
| 301 | num_opps = dev_pm_opp_get_opp_count(dev); |
Javi Merino | 459ac37 | 2015-08-17 19:21:42 +0100 | [diff] [blame] | 302 | if (num_opps > 0) |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 303 | break; |
Javi Merino | 459ac37 | 2015-08-17 19:21:42 +0100 | [diff] [blame] | 304 | else if (num_opps < 0) |
| 305 | return num_opps; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Javi Merino | 459ac37 | 2015-08-17 19:21:42 +0100 | [diff] [blame] | 308 | if (num_opps == 0) |
| 309 | return -EINVAL; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 310 | |
| 311 | power_table = kcalloc(num_opps, sizeof(*power_table), GFP_KERNEL); |
Javi Merino | 459ac37 | 2015-08-17 19:21:42 +0100 | [diff] [blame] | 312 | if (!power_table) |
| 313 | return -ENOMEM; |
| 314 | |
| 315 | rcu_read_lock(); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 316 | |
| 317 | for (freq = 0, i = 0; |
| 318 | opp = dev_pm_opp_find_freq_ceil(dev, &freq), !IS_ERR(opp); |
| 319 | freq++, i++) { |
| 320 | u32 freq_mhz, voltage_mv; |
| 321 | u64 power; |
| 322 | |
Javi Merino | 459ac37 | 2015-08-17 19:21:42 +0100 | [diff] [blame] | 323 | if (i >= num_opps) { |
| 324 | rcu_read_unlock(); |
Javi Merino | eba4f88 | 2015-08-17 19:21:43 +0100 | [diff] [blame] | 325 | ret = -EAGAIN; |
| 326 | goto free_power_table; |
Javi Merino | 459ac37 | 2015-08-17 19:21:42 +0100 | [diff] [blame] | 327 | } |
| 328 | |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 329 | freq_mhz = freq / 1000000; |
| 330 | voltage_mv = dev_pm_opp_get_voltage(opp) / 1000; |
| 331 | |
| 332 | /* |
| 333 | * Do the multiplication with MHz and millivolt so as |
| 334 | * to not overflow. |
| 335 | */ |
| 336 | power = (u64)capacitance * freq_mhz * voltage_mv * voltage_mv; |
| 337 | do_div(power, 1000000000); |
| 338 | |
| 339 | /* frequency is stored in power_table in KHz */ |
| 340 | power_table[i].frequency = freq / 1000; |
| 341 | |
| 342 | /* power is stored in mW */ |
| 343 | power_table[i].power = power; |
| 344 | } |
| 345 | |
Javi Merino | 459ac37 | 2015-08-17 19:21:42 +0100 | [diff] [blame] | 346 | rcu_read_unlock(); |
| 347 | |
Javi Merino | eba4f88 | 2015-08-17 19:21:43 +0100 | [diff] [blame] | 348 | if (i != num_opps) { |
| 349 | ret = PTR_ERR(opp); |
| 350 | goto free_power_table; |
| 351 | } |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 352 | |
| 353 | cpufreq_device->cpu_dev = dev; |
| 354 | cpufreq_device->dyn_power_table = power_table; |
| 355 | cpufreq_device->dyn_power_table_entries = i; |
| 356 | |
Javi Merino | 459ac37 | 2015-08-17 19:21:42 +0100 | [diff] [blame] | 357 | return 0; |
Javi Merino | eba4f88 | 2015-08-17 19:21:43 +0100 | [diff] [blame] | 358 | |
| 359 | free_power_table: |
| 360 | kfree(power_table); |
| 361 | |
| 362 | return ret; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_device, |
| 366 | u32 freq) |
| 367 | { |
| 368 | int i; |
| 369 | struct power_table *pt = cpufreq_device->dyn_power_table; |
| 370 | |
| 371 | for (i = 1; i < cpufreq_device->dyn_power_table_entries; i++) |
| 372 | if (freq < pt[i].frequency) |
| 373 | break; |
| 374 | |
| 375 | return pt[i - 1].power; |
| 376 | } |
| 377 | |
| 378 | static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_device, |
| 379 | u32 power) |
| 380 | { |
| 381 | int i; |
| 382 | struct power_table *pt = cpufreq_device->dyn_power_table; |
| 383 | |
| 384 | for (i = 1; i < cpufreq_device->dyn_power_table_entries; i++) |
| 385 | if (power < pt[i].power) |
| 386 | break; |
| 387 | |
| 388 | return pt[i - 1].frequency; |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * get_load() - get load for a cpu since last updated |
| 393 | * @cpufreq_device: &struct cpufreq_cooling_device for this cpu |
| 394 | * @cpu: cpu number |
Javi Merino | a53b839 | 2016-02-11 12:00:51 +0000 | [diff] [blame] | 395 | * @cpu_idx: index of the cpu in cpufreq_device->allowed_cpus |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 396 | * |
| 397 | * Return: The average load of cpu @cpu in percentage since this |
| 398 | * function was last called. |
| 399 | */ |
Javi Merino | a53b839 | 2016-02-11 12:00:51 +0000 | [diff] [blame] | 400 | static u32 get_load(struct cpufreq_cooling_device *cpufreq_device, int cpu, |
| 401 | int cpu_idx) |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 402 | { |
| 403 | u32 load; |
| 404 | u64 now, now_idle, delta_time, delta_idle; |
| 405 | |
| 406 | now_idle = get_cpu_idle_time(cpu, &now, 0); |
Javi Merino | a53b839 | 2016-02-11 12:00:51 +0000 | [diff] [blame] | 407 | delta_idle = now_idle - cpufreq_device->time_in_idle[cpu_idx]; |
| 408 | delta_time = now - cpufreq_device->time_in_idle_timestamp[cpu_idx]; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 409 | |
| 410 | if (delta_time <= delta_idle) |
| 411 | load = 0; |
| 412 | else |
| 413 | load = div64_u64(100 * (delta_time - delta_idle), delta_time); |
| 414 | |
Javi Merino | a53b839 | 2016-02-11 12:00:51 +0000 | [diff] [blame] | 415 | cpufreq_device->time_in_idle[cpu_idx] = now_idle; |
| 416 | cpufreq_device->time_in_idle_timestamp[cpu_idx] = now; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 417 | |
| 418 | return load; |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * get_static_power() - calculate the static power consumed by the cpus |
| 423 | * @cpufreq_device: struct &cpufreq_cooling_device for this cpu cdev |
| 424 | * @tz: thermal zone device in which we're operating |
| 425 | * @freq: frequency in KHz |
| 426 | * @power: pointer in which to store the calculated static power |
| 427 | * |
| 428 | * Calculate the static power consumed by the cpus described by |
| 429 | * @cpu_actor running at frequency @freq. This function relies on a |
| 430 | * platform specific function that should have been provided when the |
| 431 | * actor was registered. If it wasn't, the static power is assumed to |
| 432 | * be negligible. The calculated static power is stored in @power. |
| 433 | * |
| 434 | * Return: 0 on success, -E* on failure. |
| 435 | */ |
| 436 | static int get_static_power(struct cpufreq_cooling_device *cpufreq_device, |
| 437 | struct thermal_zone_device *tz, unsigned long freq, |
| 438 | u32 *power) |
| 439 | { |
| 440 | struct dev_pm_opp *opp; |
| 441 | unsigned long voltage; |
| 442 | struct cpumask *cpumask = &cpufreq_device->allowed_cpus; |
| 443 | unsigned long freq_hz = freq * 1000; |
| 444 | |
| 445 | if (!cpufreq_device->plat_get_static_power || |
| 446 | !cpufreq_device->cpu_dev) { |
| 447 | *power = 0; |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | rcu_read_lock(); |
| 452 | |
| 453 | opp = dev_pm_opp_find_freq_exact(cpufreq_device->cpu_dev, freq_hz, |
| 454 | true); |
| 455 | voltage = dev_pm_opp_get_voltage(opp); |
| 456 | |
| 457 | rcu_read_unlock(); |
| 458 | |
| 459 | if (voltage == 0) { |
| 460 | dev_warn_ratelimited(cpufreq_device->cpu_dev, |
| 461 | "Failed to get voltage for frequency %lu: %ld\n", |
| 462 | freq_hz, IS_ERR(opp) ? PTR_ERR(opp) : 0); |
| 463 | return -EINVAL; |
| 464 | } |
| 465 | |
| 466 | return cpufreq_device->plat_get_static_power(cpumask, tz->passive_delay, |
| 467 | voltage, power); |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * get_dynamic_power() - calculate the dynamic power |
| 472 | * @cpufreq_device: &cpufreq_cooling_device for this cdev |
| 473 | * @freq: current frequency |
| 474 | * |
| 475 | * Return: the dynamic power consumed by the cpus described by |
| 476 | * @cpufreq_device. |
| 477 | */ |
| 478 | static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_device, |
| 479 | unsigned long freq) |
| 480 | { |
| 481 | u32 raw_cpu_power; |
| 482 | |
| 483 | raw_cpu_power = cpu_freq_to_power(cpufreq_device, freq); |
| 484 | return (raw_cpu_power * cpufreq_device->last_load) / 100; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 485 | } |
| 486 | |
Eduardo Valentin | 1b9e352 | 2013-04-17 17:12:02 +0000 | [diff] [blame] | 487 | /* cpufreq cooling device callback functions are defined below */ |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 488 | |
| 489 | /** |
| 490 | * cpufreq_get_max_state - callback function to get the max cooling state. |
| 491 | * @cdev: thermal cooling device pointer. |
| 492 | * @state: fill this variable with the max cooling state. |
Eduardo Valentin | 62c0042 | 2013-04-17 17:12:12 +0000 | [diff] [blame] | 493 | * |
| 494 | * Callback for the thermal cooling device to return the cpufreq |
| 495 | * max cooling state. |
| 496 | * |
| 497 | * Return: 0 on success, an error code otherwise. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 498 | */ |
| 499 | static int cpufreq_get_max_state(struct thermal_cooling_device *cdev, |
| 500 | unsigned long *state) |
| 501 | { |
hongbo.zhang | 160b7d8 | 2012-10-30 17:48:59 +0100 | [diff] [blame] | 502 | struct cpufreq_cooling_device *cpufreq_device = cdev->devdata; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 503 | |
Viresh Kumar | dcc6c7f | 2014-12-04 09:42:02 +0530 | [diff] [blame] | 504 | *state = cpufreq_device->max_level; |
| 505 | return 0; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | /** |
Ram Chandrasekar | d8e4bf2 | 2016-09-21 17:08:06 -0600 | [diff] [blame] | 509 | * cpufreq_get_min_state - callback function to get the device floor state. |
| 510 | * @cdev: thermal cooling device pointer. |
| 511 | * @state: fill this variable with the cooling device floor. |
| 512 | * |
| 513 | * Callback for the thermal cooling device to return the cpufreq |
| 514 | * floor state. |
| 515 | * |
| 516 | * Return: 0 on success, an error code otherwise. |
| 517 | */ |
| 518 | static int cpufreq_get_min_state(struct thermal_cooling_device *cdev, |
| 519 | unsigned long *state) |
| 520 | { |
| 521 | struct cpufreq_cooling_device *cpufreq_device = cdev->devdata; |
| 522 | |
| 523 | *state = cpufreq_device->cpufreq_floor_state; |
| 524 | |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | /** |
| 529 | * cpufreq_set_min_state - callback function to set the device floor state. |
| 530 | * @cdev: thermal cooling device pointer. |
| 531 | * @state: set this variable to the current cooling state. |
| 532 | * |
| 533 | * Callback for the thermal cooling device to change the cpufreq |
| 534 | * floor state. |
| 535 | * |
| 536 | * Return: 0 on success, an error code otherwise. |
| 537 | */ |
| 538 | static int cpufreq_set_min_state(struct thermal_cooling_device *cdev, |
| 539 | unsigned long state) |
| 540 | { |
| 541 | struct cpufreq_cooling_device *cpufreq_device = cdev->devdata; |
| 542 | unsigned int cpu = cpumask_any(&cpufreq_device->allowed_cpus); |
| 543 | unsigned int floor_freq; |
| 544 | |
| 545 | if (state > cpufreq_device->max_level) |
| 546 | state = cpufreq_device->max_level; |
| 547 | |
| 548 | if (cpufreq_device->cpufreq_floor_state == state) |
| 549 | return 0; |
| 550 | |
Ram Chandrasekar | d8e4bf2 | 2016-09-21 17:08:06 -0600 | [diff] [blame] | 551 | cpufreq_device->cpufreq_floor_state = state; |
Ram Chandrasekar | d8e4bf2 | 2016-09-21 17:08:06 -0600 | [diff] [blame] | 552 | |
Ram Chandrasekar | 23b2db4 | 2017-04-19 13:23:31 -0600 | [diff] [blame] | 553 | /* |
| 554 | * Check if the device has a platform mitigation function that |
| 555 | * can handle the CPU freq mitigation, if not, notify cpufreq |
| 556 | * framework. |
| 557 | */ |
| 558 | if (cpufreq_device->plat_ops && |
| 559 | cpufreq_device->plat_ops->floor_limit) { |
| 560 | /* |
| 561 | * Last level is core isolation so use the frequency |
| 562 | * of previous state. |
| 563 | */ |
| 564 | if (state == cpufreq_device->max_level) |
| 565 | state--; |
| 566 | floor_freq = cpufreq_device->freq_table[state]; |
| 567 | cpufreq_device->floor_freq = floor_freq; |
| 568 | cpufreq_device->plat_ops->floor_limit(cpu, floor_freq); |
| 569 | } else { |
| 570 | floor_freq = cpufreq_device->freq_table[state]; |
| 571 | cpufreq_device->floor_freq = floor_freq; |
| 572 | cpufreq_update_policy(cpu); |
| 573 | } |
Ram Chandrasekar | d8e4bf2 | 2016-09-21 17:08:06 -0600 | [diff] [blame] | 574 | |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | /** |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 579 | * cpufreq_get_cur_state - callback function to get the current cooling state. |
| 580 | * @cdev: thermal cooling device pointer. |
| 581 | * @state: fill this variable with the current cooling state. |
Eduardo Valentin | 3672552 | 2013-04-17 17:12:13 +0000 | [diff] [blame] | 582 | * |
| 583 | * Callback for the thermal cooling device to return the cpufreq |
| 584 | * current cooling state. |
| 585 | * |
| 586 | * Return: 0 on success, an error code otherwise. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 587 | */ |
| 588 | static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev, |
| 589 | unsigned long *state) |
| 590 | { |
hongbo.zhang | 160b7d8 | 2012-10-30 17:48:59 +0100 | [diff] [blame] | 591 | struct cpufreq_cooling_device *cpufreq_device = cdev->devdata; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 592 | |
hongbo.zhang | 160b7d8 | 2012-10-30 17:48:59 +0100 | [diff] [blame] | 593 | *state = cpufreq_device->cpufreq_state; |
Eduardo Valentin | 79491e5 | 2013-04-17 17:11:59 +0000 | [diff] [blame] | 594 | |
hongbo.zhang | 160b7d8 | 2012-10-30 17:48:59 +0100 | [diff] [blame] | 595 | return 0; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | /** |
| 599 | * cpufreq_set_cur_state - callback function to set the current cooling state. |
| 600 | * @cdev: thermal cooling device pointer. |
| 601 | * @state: set this variable to the current cooling state. |
Eduardo Valentin | 56e05fdb | 2013-04-17 17:12:14 +0000 | [diff] [blame] | 602 | * |
| 603 | * Callback for the thermal cooling device to change the cpufreq |
| 604 | * current cooling state. |
| 605 | * |
| 606 | * Return: 0 on success, an error code otherwise. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 607 | */ |
| 608 | static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev, |
| 609 | unsigned long state) |
| 610 | { |
hongbo.zhang | 160b7d8 | 2012-10-30 17:48:59 +0100 | [diff] [blame] | 611 | struct cpufreq_cooling_device *cpufreq_device = cdev->devdata; |
Viresh Kumar | 5194fe4 | 2014-12-04 09:42:00 +0530 | [diff] [blame] | 612 | unsigned int cpu = cpumask_any(&cpufreq_device->allowed_cpus); |
| 613 | unsigned int clip_freq; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 614 | |
Viresh Kumar | 4843c4a | 2014-12-04 09:42:07 +0530 | [diff] [blame] | 615 | /* Request state should be less than max_level */ |
| 616 | if (WARN_ON(state > cpufreq_device->max_level)) |
| 617 | return -EINVAL; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 618 | |
Viresh Kumar | 5194fe4 | 2014-12-04 09:42:00 +0530 | [diff] [blame] | 619 | /* Check if the old cooling action is same as new cooling action */ |
| 620 | if (cpufreq_device->cpufreq_state == state) |
| 621 | return 0; |
| 622 | |
Lina Iyer | 97a13ed | 2016-07-15 14:53:58 -0600 | [diff] [blame] | 623 | /* If state is the last, isolate the CPU */ |
| 624 | if (state == cpufreq_device->max_level) |
| 625 | return sched_isolate_cpu(cpu); |
| 626 | else if (state < cpufreq_device->max_level) |
| 627 | sched_unisolate_cpu(cpu); |
| 628 | |
Viresh Kumar | 4843c4a | 2014-12-04 09:42:07 +0530 | [diff] [blame] | 629 | clip_freq = cpufreq_device->freq_table[state]; |
Viresh Kumar | 5194fe4 | 2014-12-04 09:42:00 +0530 | [diff] [blame] | 630 | cpufreq_device->cpufreq_state = state; |
Viresh Kumar | 59f0d21 | 2015-07-30 12:40:33 +0530 | [diff] [blame] | 631 | cpufreq_device->clipped_freq = clip_freq; |
Viresh Kumar | 5194fe4 | 2014-12-04 09:42:00 +0530 | [diff] [blame] | 632 | |
Lina Iyer | 986fde1 | 2016-02-23 13:08:31 -0700 | [diff] [blame] | 633 | /* Check if the device has a platform mitigation function that |
| 634 | * can handle the CPU freq mitigation, if not, notify cpufreq |
| 635 | * framework. |
| 636 | */ |
| 637 | if (cpufreq_device->plat_ops) { |
| 638 | if (cpufreq_device->plat_ops->ceil_limit) |
| 639 | cpufreq_device->plat_ops->ceil_limit(cpu, |
| 640 | clip_freq); |
| 641 | } else { |
| 642 | cpufreq_update_policy(cpu); |
| 643 | } |
Viresh Kumar | 5194fe4 | 2014-12-04 09:42:00 +0530 | [diff] [blame] | 644 | |
| 645 | return 0; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 646 | } |
| 647 | |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 648 | /** |
| 649 | * cpufreq_get_requested_power() - get the current power |
| 650 | * @cdev: &thermal_cooling_device pointer |
| 651 | * @tz: a valid thermal zone device pointer |
| 652 | * @power: pointer in which to store the resulting power |
| 653 | * |
| 654 | * Calculate the current power consumption of the cpus in milliwatts |
| 655 | * and store it in @power. This function should actually calculate |
| 656 | * the requested power, but it's hard to get the frequency that |
| 657 | * cpufreq would have assigned if there were no thermal limits. |
| 658 | * Instead, we calculate the current power on the assumption that the |
| 659 | * immediate future will look like the immediate past. |
| 660 | * |
| 661 | * We use the current frequency and the average load since this |
| 662 | * function was last called. In reality, there could have been |
| 663 | * multiple opps since this function was last called and that affects |
| 664 | * the load calculation. While it's not perfectly accurate, this |
| 665 | * simplification is good enough and works. REVISIT this, as more |
| 666 | * complex code may be needed if experiments show that it's not |
| 667 | * accurate enough. |
| 668 | * |
| 669 | * Return: 0 on success, -E* if getting the static power failed. |
| 670 | */ |
| 671 | static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev, |
| 672 | struct thermal_zone_device *tz, |
| 673 | u32 *power) |
| 674 | { |
| 675 | unsigned long freq; |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 676 | int i = 0, cpu, ret; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 677 | u32 static_power, dynamic_power, total_load = 0; |
| 678 | struct cpufreq_cooling_device *cpufreq_device = cdev->devdata; |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 679 | u32 *load_cpu = NULL; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 680 | |
Kapileshwar Singh | dd658e0 | 2015-03-16 12:00:51 +0000 | [diff] [blame] | 681 | cpu = cpumask_any_and(&cpufreq_device->allowed_cpus, cpu_online_mask); |
| 682 | |
| 683 | /* |
| 684 | * All the CPUs are offline, thus the requested power by |
| 685 | * the cdev is 0 |
| 686 | */ |
| 687 | if (cpu >= nr_cpu_ids) { |
| 688 | *power = 0; |
| 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | freq = cpufreq_quick_get(cpu); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 693 | |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 694 | if (trace_thermal_power_cpu_get_power_enabled()) { |
| 695 | u32 ncpus = cpumask_weight(&cpufreq_device->allowed_cpus); |
| 696 | |
Vaishali Thakkar | a71544c | 2015-08-19 11:52:19 +0530 | [diff] [blame] | 697 | load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL); |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 700 | for_each_cpu(cpu, &cpufreq_device->allowed_cpus) { |
| 701 | u32 load; |
| 702 | |
| 703 | if (cpu_online(cpu)) |
Javi Merino | a53b839 | 2016-02-11 12:00:51 +0000 | [diff] [blame] | 704 | load = get_load(cpufreq_device, cpu, i); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 705 | else |
| 706 | load = 0; |
| 707 | |
| 708 | total_load += load; |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 709 | if (trace_thermal_power_cpu_limit_enabled() && load_cpu) |
| 710 | load_cpu[i] = load; |
| 711 | |
| 712 | i++; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | cpufreq_device->last_load = total_load; |
| 716 | |
| 717 | dynamic_power = get_dynamic_power(cpufreq_device, freq); |
| 718 | ret = get_static_power(cpufreq_device, tz, freq, &static_power); |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 719 | if (ret) { |
Vaishali Thakkar | a71544c | 2015-08-19 11:52:19 +0530 | [diff] [blame] | 720 | kfree(load_cpu); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 721 | return ret; |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | if (load_cpu) { |
| 725 | trace_thermal_power_cpu_get_power( |
| 726 | &cpufreq_device->allowed_cpus, |
| 727 | freq, load_cpu, i, dynamic_power, static_power); |
| 728 | |
Vaishali Thakkar | a71544c | 2015-08-19 11:52:19 +0530 | [diff] [blame] | 729 | kfree(load_cpu); |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 730 | } |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 731 | |
| 732 | *power = static_power + dynamic_power; |
| 733 | return 0; |
| 734 | } |
| 735 | |
| 736 | /** |
| 737 | * cpufreq_state2power() - convert a cpu cdev state to power consumed |
| 738 | * @cdev: &thermal_cooling_device pointer |
| 739 | * @tz: a valid thermal zone device pointer |
| 740 | * @state: cooling device state to be converted |
| 741 | * @power: pointer in which to store the resulting power |
| 742 | * |
| 743 | * Convert cooling device state @state into power consumption in |
| 744 | * milliwatts assuming 100% load. Store the calculated power in |
| 745 | * @power. |
| 746 | * |
| 747 | * Return: 0 on success, -EINVAL if the cooling device state could not |
| 748 | * be converted into a frequency or other -E* if there was an error |
| 749 | * when calculating the static power. |
| 750 | */ |
| 751 | static int cpufreq_state2power(struct thermal_cooling_device *cdev, |
| 752 | struct thermal_zone_device *tz, |
| 753 | unsigned long state, u32 *power) |
| 754 | { |
| 755 | unsigned int freq, num_cpus; |
| 756 | cpumask_t cpumask; |
| 757 | u32 static_power, dynamic_power; |
| 758 | int ret; |
| 759 | struct cpufreq_cooling_device *cpufreq_device = cdev->devdata; |
| 760 | |
| 761 | cpumask_and(&cpumask, &cpufreq_device->allowed_cpus, cpu_online_mask); |
| 762 | num_cpus = cpumask_weight(&cpumask); |
| 763 | |
| 764 | /* None of our cpus are online, so no power */ |
| 765 | if (num_cpus == 0) { |
| 766 | *power = 0; |
| 767 | return 0; |
| 768 | } |
| 769 | |
| 770 | freq = cpufreq_device->freq_table[state]; |
| 771 | if (!freq) |
| 772 | return -EINVAL; |
| 773 | |
| 774 | dynamic_power = cpu_freq_to_power(cpufreq_device, freq) * num_cpus; |
| 775 | ret = get_static_power(cpufreq_device, tz, freq, &static_power); |
| 776 | if (ret) |
| 777 | return ret; |
| 778 | |
| 779 | *power = static_power + dynamic_power; |
| 780 | return 0; |
| 781 | } |
| 782 | |
| 783 | /** |
| 784 | * cpufreq_power2state() - convert power to a cooling device state |
| 785 | * @cdev: &thermal_cooling_device pointer |
| 786 | * @tz: a valid thermal zone device pointer |
| 787 | * @power: power in milliwatts to be converted |
| 788 | * @state: pointer in which to store the resulting state |
| 789 | * |
| 790 | * Calculate a cooling device state for the cpus described by @cdev |
| 791 | * that would allow them to consume at most @power mW and store it in |
| 792 | * @state. Note that this calculation depends on external factors |
| 793 | * such as the cpu load or the current static power. Calling this |
| 794 | * function with the same power as input can yield different cooling |
| 795 | * device states depending on those external factors. |
| 796 | * |
| 797 | * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if |
| 798 | * the calculated frequency could not be converted to a valid state. |
| 799 | * The latter should not happen unless the frequencies available to |
| 800 | * cpufreq have changed since the initialization of the cpu cooling |
| 801 | * device. |
| 802 | */ |
| 803 | static int cpufreq_power2state(struct thermal_cooling_device *cdev, |
| 804 | struct thermal_zone_device *tz, u32 power, |
| 805 | unsigned long *state) |
| 806 | { |
| 807 | unsigned int cpu, cur_freq, target_freq; |
| 808 | int ret; |
| 809 | s32 dyn_power; |
| 810 | u32 last_load, normalised_power, static_power; |
| 811 | struct cpufreq_cooling_device *cpufreq_device = cdev->devdata; |
| 812 | |
| 813 | cpu = cpumask_any_and(&cpufreq_device->allowed_cpus, cpu_online_mask); |
| 814 | |
| 815 | /* None of our cpus are online */ |
| 816 | if (cpu >= nr_cpu_ids) |
| 817 | return -ENODEV; |
| 818 | |
| 819 | cur_freq = cpufreq_quick_get(cpu); |
| 820 | ret = get_static_power(cpufreq_device, tz, cur_freq, &static_power); |
| 821 | if (ret) |
| 822 | return ret; |
| 823 | |
| 824 | dyn_power = power - static_power; |
| 825 | dyn_power = dyn_power > 0 ? dyn_power : 0; |
| 826 | last_load = cpufreq_device->last_load ?: 1; |
| 827 | normalised_power = (dyn_power * 100) / last_load; |
| 828 | target_freq = cpu_power_to_freq(cpufreq_device, normalised_power); |
| 829 | |
| 830 | *state = cpufreq_cooling_get_level(cpu, target_freq); |
| 831 | if (*state == THERMAL_CSTATE_INVALID) { |
| 832 | dev_warn_ratelimited(&cdev->device, |
| 833 | "Failed to convert %dKHz for cpu %d into a cdev state\n", |
| 834 | target_freq, cpu); |
| 835 | return -EINVAL; |
| 836 | } |
| 837 | |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 838 | trace_thermal_power_cpu_limit(&cpufreq_device->allowed_cpus, |
| 839 | target_freq, *state, power); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 840 | return 0; |
| 841 | } |
| 842 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 843 | /* Bind cpufreq callbacks to thermal cooling device ops */ |
Brendan Jackman | a305a43 | 2016-08-17 16:14:59 +0100 | [diff] [blame] | 844 | |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 845 | static struct thermal_cooling_device_ops cpufreq_cooling_ops = { |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 846 | .get_max_state = cpufreq_get_max_state, |
| 847 | .get_cur_state = cpufreq_get_cur_state, |
| 848 | .set_cur_state = cpufreq_set_cur_state, |
Ram Chandrasekar | d8e4bf2 | 2016-09-21 17:08:06 -0600 | [diff] [blame] | 849 | .set_min_state = cpufreq_set_min_state, |
| 850 | .get_min_state = cpufreq_get_min_state, |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 851 | }; |
| 852 | |
Brendan Jackman | a305a43 | 2016-08-17 16:14:59 +0100 | [diff] [blame] | 853 | static struct thermal_cooling_device_ops cpufreq_power_cooling_ops = { |
| 854 | .get_max_state = cpufreq_get_max_state, |
| 855 | .get_cur_state = cpufreq_get_cur_state, |
| 856 | .set_cur_state = cpufreq_set_cur_state, |
| 857 | .get_requested_power = cpufreq_get_requested_power, |
| 858 | .state2power = cpufreq_state2power, |
| 859 | .power2state = cpufreq_power2state, |
| 860 | }; |
| 861 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 862 | /* Notifier for cpufreq policy change */ |
| 863 | static struct notifier_block thermal_cpufreq_notifier_block = { |
| 864 | .notifier_call = cpufreq_thermal_notifier, |
| 865 | }; |
| 866 | |
Viresh Kumar | f685901 | 2014-12-04 09:42:06 +0530 | [diff] [blame] | 867 | static unsigned int find_next_max(struct cpufreq_frequency_table *table, |
| 868 | unsigned int prev_max) |
| 869 | { |
| 870 | struct cpufreq_frequency_table *pos; |
| 871 | unsigned int max = 0; |
| 872 | |
| 873 | cpufreq_for_each_valid_entry(pos, table) { |
| 874 | if (pos->frequency > max && pos->frequency < prev_max) |
| 875 | max = pos->frequency; |
| 876 | } |
| 877 | |
| 878 | return max; |
| 879 | } |
| 880 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 881 | /** |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 882 | * __cpufreq_cooling_register - helper function to create cpufreq cooling device |
| 883 | * @np: a valid struct device_node to the cooling device device tree node |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 884 | * @clip_cpus: cpumask of cpus where the frequency constraints will happen. |
Viresh Kumar | 405fb82 | 2014-12-04 09:41:55 +0530 | [diff] [blame] | 885 | * Normally this should be same as cpufreq policy->related_cpus. |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 886 | * @capacitance: dynamic power coefficient for these cpus |
| 887 | * @plat_static_func: function to calculate the static power consumed by these |
| 888 | * cpus (optional) |
Lina Iyer | 986fde1 | 2016-02-23 13:08:31 -0700 | [diff] [blame] | 889 | * @plat_mitig_func: function that does the mitigation by changing the |
| 890 | * frequencies (Optional). By default, cpufreq framweork will |
| 891 | * be notified of the new limits. |
Eduardo Valentin | 12cb08b | 2013-04-17 17:12:15 +0000 | [diff] [blame] | 892 | * |
| 893 | * This interface function registers the cpufreq cooling device with the name |
| 894 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 895 | * cooling devices. It also gives the opportunity to link the cooling device |
| 896 | * with a device tree node, in order to bind it via the thermal DT code. |
Eduardo Valentin | 12cb08b | 2013-04-17 17:12:15 +0000 | [diff] [blame] | 897 | * |
| 898 | * Return: a valid struct thermal_cooling_device pointer on success, |
| 899 | * on failure, it returns a corresponding ERR_PTR(). |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 900 | */ |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 901 | static struct thermal_cooling_device * |
| 902 | __cpufreq_cooling_register(struct device_node *np, |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 903 | const struct cpumask *clip_cpus, u32 capacitance, |
Lina Iyer | 986fde1 | 2016-02-23 13:08:31 -0700 | [diff] [blame] | 904 | get_static_t plat_static_func, |
| 905 | struct cpu_cooling_ops *plat_ops) |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 906 | { |
Viresh Kumar | f8bfc11 | 2016-06-03 10:58:47 +0530 | [diff] [blame] | 907 | struct cpufreq_policy *policy; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 908 | struct thermal_cooling_device *cool_dev; |
Viresh Kumar | 5d3bdb8 | 2014-12-04 09:41:52 +0530 | [diff] [blame] | 909 | struct cpufreq_cooling_device *cpufreq_dev; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 910 | char dev_name[THERMAL_NAME_LENGTH]; |
Viresh Kumar | dcc6c7f | 2014-12-04 09:42:02 +0530 | [diff] [blame] | 911 | struct cpufreq_frequency_table *pos, *table; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 912 | unsigned int freq, i, num_cpus; |
Viresh Kumar | 405fb82 | 2014-12-04 09:41:55 +0530 | [diff] [blame] | 913 | int ret; |
Brendan Jackman | a305a43 | 2016-08-17 16:14:59 +0100 | [diff] [blame] | 914 | struct thermal_cooling_device_ops *cooling_ops; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 915 | |
Ram Chandrasekar | 5682d55 | 2017-05-03 16:01:53 -0600 | [diff] [blame^] | 916 | policy = cpufreq_cpu_get(cpumask_first(clip_cpus)); |
Viresh Kumar | f8bfc11 | 2016-06-03 10:58:47 +0530 | [diff] [blame] | 917 | if (!policy) { |
| 918 | pr_debug("%s: CPUFreq policy not found\n", __func__); |
Eduardo Valentin | 0f1be51 | 2014-12-04 09:41:43 +0530 | [diff] [blame] | 919 | return ERR_PTR(-EPROBE_DEFER); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 920 | } |
Eduardo Valentin | 0f1be51 | 2014-12-04 09:41:43 +0530 | [diff] [blame] | 921 | |
Viresh Kumar | f8bfc11 | 2016-06-03 10:58:47 +0530 | [diff] [blame] | 922 | table = policy->freq_table; |
| 923 | if (!table) { |
| 924 | pr_debug("%s: CPUFreq table not found\n", __func__); |
| 925 | cool_dev = ERR_PTR(-ENODEV); |
| 926 | goto put_policy; |
| 927 | } |
| 928 | |
Viresh Kumar | 98d522f | 2014-12-04 09:41:50 +0530 | [diff] [blame] | 929 | cpufreq_dev = kzalloc(sizeof(*cpufreq_dev), GFP_KERNEL); |
Viresh Kumar | f8bfc11 | 2016-06-03 10:58:47 +0530 | [diff] [blame] | 930 | if (!cpufreq_dev) { |
| 931 | cool_dev = ERR_PTR(-ENOMEM); |
| 932 | goto put_policy; |
| 933 | } |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 934 | |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 935 | num_cpus = cpumask_weight(clip_cpus); |
| 936 | cpufreq_dev->time_in_idle = kcalloc(num_cpus, |
| 937 | sizeof(*cpufreq_dev->time_in_idle), |
| 938 | GFP_KERNEL); |
| 939 | if (!cpufreq_dev->time_in_idle) { |
| 940 | cool_dev = ERR_PTR(-ENOMEM); |
| 941 | goto free_cdev; |
| 942 | } |
| 943 | |
| 944 | cpufreq_dev->time_in_idle_timestamp = |
| 945 | kcalloc(num_cpus, sizeof(*cpufreq_dev->time_in_idle_timestamp), |
| 946 | GFP_KERNEL); |
| 947 | if (!cpufreq_dev->time_in_idle_timestamp) { |
| 948 | cool_dev = ERR_PTR(-ENOMEM); |
| 949 | goto free_time_in_idle; |
| 950 | } |
| 951 | |
Viresh Kumar | dcc6c7f | 2014-12-04 09:42:02 +0530 | [diff] [blame] | 952 | /* Find max levels */ |
| 953 | cpufreq_for_each_valid_entry(pos, table) |
| 954 | cpufreq_dev->max_level++; |
| 955 | |
Lina Iyer | 97a13ed | 2016-07-15 14:53:58 -0600 | [diff] [blame] | 956 | /* Last level will indicate the core will be isolated. */ |
| 957 | cpufreq_dev->max_level++; |
| 958 | cpufreq_dev->freq_table = kzalloc(sizeof(*cpufreq_dev->freq_table) * |
Viresh Kumar | f685901 | 2014-12-04 09:42:06 +0530 | [diff] [blame] | 959 | cpufreq_dev->max_level, GFP_KERNEL); |
| 960 | if (!cpufreq_dev->freq_table) { |
Viresh Kumar | f685901 | 2014-12-04 09:42:06 +0530 | [diff] [blame] | 961 | cool_dev = ERR_PTR(-ENOMEM); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 962 | goto free_time_in_idle_timestamp; |
Viresh Kumar | f685901 | 2014-12-04 09:42:06 +0530 | [diff] [blame] | 963 | } |
| 964 | |
Viresh Kumar | dcc6c7f | 2014-12-04 09:42:02 +0530 | [diff] [blame] | 965 | /* max_level is an index, not a counter */ |
| 966 | cpufreq_dev->max_level--; |
| 967 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 968 | cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus); |
| 969 | |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 970 | if (capacitance) { |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 971 | cpufreq_dev->plat_get_static_power = plat_static_func; |
| 972 | |
| 973 | ret = build_dyn_power_table(cpufreq_dev, capacitance); |
| 974 | if (ret) { |
| 975 | cool_dev = ERR_PTR(ret); |
| 976 | goto free_table; |
| 977 | } |
Brendan Jackman | a305a43 | 2016-08-17 16:14:59 +0100 | [diff] [blame] | 978 | |
| 979 | cooling_ops = &cpufreq_power_cooling_ops; |
| 980 | } else { |
| 981 | cooling_ops = &cpufreq_cooling_ops; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 982 | } |
| 983 | |
Lina Iyer | 986fde1 | 2016-02-23 13:08:31 -0700 | [diff] [blame] | 984 | cpufreq_dev->plat_ops = plat_ops; |
| 985 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 986 | ret = get_idr(&cpufreq_idr, &cpufreq_dev->id); |
| 987 | if (ret) { |
Viresh Kumar | 730abe0 | 2014-12-04 09:41:58 +0530 | [diff] [blame] | 988 | cool_dev = ERR_PTR(ret); |
Javi Merino | eba4f88 | 2015-08-17 19:21:43 +0100 | [diff] [blame] | 989 | goto free_power_table; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 990 | } |
| 991 | |
Viresh Kumar | f685901 | 2014-12-04 09:42:06 +0530 | [diff] [blame] | 992 | /* Fill freq-table in descending order of frequencies */ |
Lina Iyer | 97a13ed | 2016-07-15 14:53:58 -0600 | [diff] [blame] | 993 | for (i = 0, freq = -1; i < cpufreq_dev->max_level; i++) { |
Viresh Kumar | f685901 | 2014-12-04 09:42:06 +0530 | [diff] [blame] | 994 | freq = find_next_max(table, freq); |
| 995 | cpufreq_dev->freq_table[i] = freq; |
| 996 | |
| 997 | /* Warn for duplicate entries */ |
| 998 | if (!freq) |
| 999 | pr_warn("%s: table has duplicate entries\n", __func__); |
| 1000 | else |
| 1001 | pr_debug("%s: freq:%u KHz\n", __func__, freq); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 1002 | } |
Viresh Kumar | f685901 | 2014-12-04 09:42:06 +0530 | [diff] [blame] | 1003 | |
Lukasz Luba | f840ab1 | 2016-05-31 11:32:02 +0100 | [diff] [blame] | 1004 | snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d", |
| 1005 | cpufreq_dev->id); |
| 1006 | |
| 1007 | cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev, |
Brendan Jackman | a305a43 | 2016-08-17 16:14:59 +0100 | [diff] [blame] | 1008 | cooling_ops); |
Lukasz Luba | f840ab1 | 2016-05-31 11:32:02 +0100 | [diff] [blame] | 1009 | if (IS_ERR(cool_dev)) |
| 1010 | goto remove_idr; |
| 1011 | |
Viresh Kumar | 59f0d21 | 2015-07-30 12:40:33 +0530 | [diff] [blame] | 1012 | cpufreq_dev->clipped_freq = cpufreq_dev->freq_table[0]; |
Ram Chandrasekar | d8e4bf2 | 2016-09-21 17:08:06 -0600 | [diff] [blame] | 1013 | cpufreq_dev->floor_freq = |
| 1014 | cpufreq_dev->freq_table[cpufreq_dev->max_level]; |
| 1015 | cpufreq_dev->cpufreq_floor_state = cpufreq_dev->max_level; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 1016 | cpufreq_dev->cool_dev = cool_dev; |
Viresh Kumar | 92e615e | 2014-12-04 09:41:51 +0530 | [diff] [blame] | 1017 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 1018 | mutex_lock(&cooling_cpufreq_lock); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 1019 | |
Russell King | 02373d7 | 2015-08-12 15:22:16 +0530 | [diff] [blame] | 1020 | mutex_lock(&cooling_list_lock); |
| 1021 | list_add(&cpufreq_dev->node, &cpufreq_dev_list); |
| 1022 | mutex_unlock(&cooling_list_lock); |
| 1023 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 1024 | /* Register the notifier for first cpufreq cooling device */ |
Russell King | 02373d7 | 2015-08-12 15:22:16 +0530 | [diff] [blame] | 1025 | if (!cpufreq_dev_count++) |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 1026 | cpufreq_register_notifier(&thermal_cpufreq_notifier_block, |
Eduardo Valentin | 5fda7f6 | 2013-04-17 17:12:11 +0000 | [diff] [blame] | 1027 | CPUFREQ_POLICY_NOTIFIER); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 1028 | mutex_unlock(&cooling_cpufreq_lock); |
Eduardo Valentin | 79491e5 | 2013-04-17 17:11:59 +0000 | [diff] [blame] | 1029 | |
Viresh Kumar | f8bfc11 | 2016-06-03 10:58:47 +0530 | [diff] [blame] | 1030 | goto put_policy; |
Viresh Kumar | 730abe0 | 2014-12-04 09:41:58 +0530 | [diff] [blame] | 1031 | |
| 1032 | remove_idr: |
| 1033 | release_idr(&cpufreq_idr, cpufreq_dev->id); |
Javi Merino | eba4f88 | 2015-08-17 19:21:43 +0100 | [diff] [blame] | 1034 | free_power_table: |
| 1035 | kfree(cpufreq_dev->dyn_power_table); |
Viresh Kumar | f685901 | 2014-12-04 09:42:06 +0530 | [diff] [blame] | 1036 | free_table: |
| 1037 | kfree(cpufreq_dev->freq_table); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 1038 | free_time_in_idle_timestamp: |
| 1039 | kfree(cpufreq_dev->time_in_idle_timestamp); |
| 1040 | free_time_in_idle: |
| 1041 | kfree(cpufreq_dev->time_in_idle); |
Viresh Kumar | 730abe0 | 2014-12-04 09:41:58 +0530 | [diff] [blame] | 1042 | free_cdev: |
| 1043 | kfree(cpufreq_dev); |
Viresh Kumar | f8bfc11 | 2016-06-03 10:58:47 +0530 | [diff] [blame] | 1044 | put_policy: |
| 1045 | cpufreq_cpu_put(policy); |
Viresh Kumar | 730abe0 | 2014-12-04 09:41:58 +0530 | [diff] [blame] | 1046 | |
| 1047 | return cool_dev; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 1048 | } |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 1049 | |
| 1050 | /** |
| 1051 | * cpufreq_cooling_register - function to create cpufreq cooling device. |
| 1052 | * @clip_cpus: cpumask of cpus where the frequency constraints will happen. |
| 1053 | * |
| 1054 | * This interface function registers the cpufreq cooling device with the name |
| 1055 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq |
| 1056 | * cooling devices. |
| 1057 | * |
| 1058 | * Return: a valid struct thermal_cooling_device pointer on success, |
| 1059 | * on failure, it returns a corresponding ERR_PTR(). |
| 1060 | */ |
| 1061 | struct thermal_cooling_device * |
| 1062 | cpufreq_cooling_register(const struct cpumask *clip_cpus) |
| 1063 | { |
Lina Iyer | 986fde1 | 2016-02-23 13:08:31 -0700 | [diff] [blame] | 1064 | return __cpufreq_cooling_register(NULL, clip_cpus, 0, NULL, NULL); |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 1065 | } |
Eduardo Valentin | 243dbd9 | 2013-04-17 17:11:57 +0000 | [diff] [blame] | 1066 | EXPORT_SYMBOL_GPL(cpufreq_cooling_register); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 1067 | |
| 1068 | /** |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 1069 | * of_cpufreq_cooling_register - function to create cpufreq cooling device. |
| 1070 | * @np: a valid struct device_node to the cooling device device tree node |
| 1071 | * @clip_cpus: cpumask of cpus where the frequency constraints will happen. |
| 1072 | * |
| 1073 | * This interface function registers the cpufreq cooling device with the name |
| 1074 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq |
| 1075 | * cooling devices. Using this API, the cpufreq cooling device will be |
| 1076 | * linked to the device tree node provided. |
| 1077 | * |
| 1078 | * Return: a valid struct thermal_cooling_device pointer on success, |
| 1079 | * on failure, it returns a corresponding ERR_PTR(). |
| 1080 | */ |
| 1081 | struct thermal_cooling_device * |
| 1082 | of_cpufreq_cooling_register(struct device_node *np, |
| 1083 | const struct cpumask *clip_cpus) |
| 1084 | { |
| 1085 | if (!np) |
| 1086 | return ERR_PTR(-EINVAL); |
| 1087 | |
Lina Iyer | 986fde1 | 2016-02-23 13:08:31 -0700 | [diff] [blame] | 1088 | return __cpufreq_cooling_register(np, clip_cpus, 0, NULL, NULL); |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 1089 | } |
| 1090 | EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register); |
| 1091 | |
| 1092 | /** |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 1093 | * cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions |
| 1094 | * @clip_cpus: cpumask of cpus where the frequency constraints will happen |
| 1095 | * @capacitance: dynamic power coefficient for these cpus |
| 1096 | * @plat_static_func: function to calculate the static power consumed by these |
| 1097 | * cpus (optional) |
| 1098 | * |
| 1099 | * This interface function registers the cpufreq cooling device with |
| 1100 | * the name "thermal-cpufreq-%x". This api can support multiple |
| 1101 | * instances of cpufreq cooling devices. Using this function, the |
| 1102 | * cooling device will implement the power extensions by using a |
| 1103 | * simple cpu power model. The cpus must have registered their OPPs |
| 1104 | * using the OPP library. |
| 1105 | * |
| 1106 | * An optional @plat_static_func may be provided to calculate the |
| 1107 | * static power consumed by these cpus. If the platform's static |
| 1108 | * power consumption is unknown or negligible, make it NULL. |
| 1109 | * |
| 1110 | * Return: a valid struct thermal_cooling_device pointer on success, |
| 1111 | * on failure, it returns a corresponding ERR_PTR(). |
| 1112 | */ |
| 1113 | struct thermal_cooling_device * |
| 1114 | cpufreq_power_cooling_register(const struct cpumask *clip_cpus, u32 capacitance, |
| 1115 | get_static_t plat_static_func) |
| 1116 | { |
| 1117 | return __cpufreq_cooling_register(NULL, clip_cpus, capacitance, |
Lina Iyer | 986fde1 | 2016-02-23 13:08:31 -0700 | [diff] [blame] | 1118 | plat_static_func, NULL); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 1119 | } |
| 1120 | EXPORT_SYMBOL(cpufreq_power_cooling_register); |
| 1121 | |
| 1122 | /** |
Lina Iyer | 986fde1 | 2016-02-23 13:08:31 -0700 | [diff] [blame] | 1123 | * cpufreq_platform_cooling_register() - create cpufreq cooling device with |
| 1124 | * additional platform specific mitigation function. |
| 1125 | * |
| 1126 | * @clip_cpus: cpumask of cpus where the frequency constraints will happen |
| 1127 | * @plat_ops: the platform mitigation functions that will be called insted of |
| 1128 | * cpufreq, if provided. |
| 1129 | * |
| 1130 | * Return: a valid struct thermal_cooling_device pointer on success, |
| 1131 | * on failure, it returns a corresponding ERR_PTR(). |
| 1132 | */ |
| 1133 | struct thermal_cooling_device * |
| 1134 | cpufreq_platform_cooling_register(const struct cpumask *clip_cpus, |
| 1135 | struct cpu_cooling_ops *plat_ops) |
| 1136 | { |
| 1137 | struct device_node *cpu_node; |
| 1138 | |
| 1139 | cpu_node = of_cpu_device_node_get(cpumask_first(clip_cpus)); |
| 1140 | return __cpufreq_cooling_register(cpu_node, clip_cpus, 0, NULL, |
| 1141 | plat_ops); |
| 1142 | } |
| 1143 | EXPORT_SYMBOL(cpufreq_platform_cooling_register); |
| 1144 | |
| 1145 | /** |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 1146 | * of_cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions |
| 1147 | * @np: a valid struct device_node to the cooling device device tree node |
| 1148 | * @clip_cpus: cpumask of cpus where the frequency constraints will happen |
| 1149 | * @capacitance: dynamic power coefficient for these cpus |
| 1150 | * @plat_static_func: function to calculate the static power consumed by these |
| 1151 | * cpus (optional) |
| 1152 | * |
| 1153 | * This interface function registers the cpufreq cooling device with |
| 1154 | * the name "thermal-cpufreq-%x". This api can support multiple |
| 1155 | * instances of cpufreq cooling devices. Using this API, the cpufreq |
| 1156 | * cooling device will be linked to the device tree node provided. |
| 1157 | * Using this function, the cooling device will implement the power |
| 1158 | * extensions by using a simple cpu power model. The cpus must have |
| 1159 | * registered their OPPs using the OPP library. |
| 1160 | * |
| 1161 | * An optional @plat_static_func may be provided to calculate the |
| 1162 | * static power consumed by these cpus. If the platform's static |
| 1163 | * power consumption is unknown or negligible, make it NULL. |
| 1164 | * |
| 1165 | * Return: a valid struct thermal_cooling_device pointer on success, |
| 1166 | * on failure, it returns a corresponding ERR_PTR(). |
| 1167 | */ |
| 1168 | struct thermal_cooling_device * |
| 1169 | of_cpufreq_power_cooling_register(struct device_node *np, |
| 1170 | const struct cpumask *clip_cpus, |
| 1171 | u32 capacitance, |
| 1172 | get_static_t plat_static_func) |
| 1173 | { |
| 1174 | if (!np) |
| 1175 | return ERR_PTR(-EINVAL); |
| 1176 | |
| 1177 | return __cpufreq_cooling_register(np, clip_cpus, capacitance, |
Lina Iyer | 986fde1 | 2016-02-23 13:08:31 -0700 | [diff] [blame] | 1178 | plat_static_func, NULL); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 1179 | } |
| 1180 | EXPORT_SYMBOL(of_cpufreq_power_cooling_register); |
| 1181 | |
| 1182 | /** |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 1183 | * cpufreq_cooling_unregister - function to remove cpufreq cooling device. |
| 1184 | * @cdev: thermal cooling device pointer. |
Eduardo Valentin | 135266b | 2013-04-17 17:12:16 +0000 | [diff] [blame] | 1185 | * |
| 1186 | * This interface function unregisters the "thermal-cpufreq-%x" cooling device. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 1187 | */ |
| 1188 | void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev) |
| 1189 | { |
Eduardo Valentin | 50e66c7 | 2013-08-15 10:54:46 -0400 | [diff] [blame] | 1190 | struct cpufreq_cooling_device *cpufreq_dev; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 1191 | |
Eduardo Valentin | 50e66c7 | 2013-08-15 10:54:46 -0400 | [diff] [blame] | 1192 | if (!cdev) |
| 1193 | return; |
| 1194 | |
| 1195 | cpufreq_dev = cdev->devdata; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 1196 | |
| 1197 | /* Unregister the notifier for the last cpufreq cooling device */ |
Russell King | 02373d7 | 2015-08-12 15:22:16 +0530 | [diff] [blame] | 1198 | mutex_lock(&cooling_cpufreq_lock); |
| 1199 | if (!--cpufreq_dev_count) |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 1200 | cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block, |
Eduardo Valentin | 5fda7f6 | 2013-04-17 17:12:11 +0000 | [diff] [blame] | 1201 | CPUFREQ_POLICY_NOTIFIER); |
Russell King | 02373d7 | 2015-08-12 15:22:16 +0530 | [diff] [blame] | 1202 | |
| 1203 | mutex_lock(&cooling_list_lock); |
| 1204 | list_del(&cpufreq_dev->node); |
| 1205 | mutex_unlock(&cooling_list_lock); |
| 1206 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 1207 | mutex_unlock(&cooling_cpufreq_lock); |
hongbo.zhang | 160b7d8 | 2012-10-30 17:48:59 +0100 | [diff] [blame] | 1208 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 1209 | thermal_cooling_device_unregister(cpufreq_dev->cool_dev); |
| 1210 | release_idr(&cpufreq_idr, cpufreq_dev->id); |
Javi Merino | eba4f88 | 2015-08-17 19:21:43 +0100 | [diff] [blame] | 1211 | kfree(cpufreq_dev->dyn_power_table); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 1212 | kfree(cpufreq_dev->time_in_idle_timestamp); |
| 1213 | kfree(cpufreq_dev->time_in_idle); |
Viresh Kumar | f685901 | 2014-12-04 09:42:06 +0530 | [diff] [blame] | 1214 | kfree(cpufreq_dev->freq_table); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 1215 | kfree(cpufreq_dev); |
| 1216 | } |
Eduardo Valentin | 243dbd9 | 2013-04-17 17:11:57 +0000 | [diff] [blame] | 1217 | EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister); |