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