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