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> |
Matthew Wilcox | ae60608 | 2016-12-21 09:47:05 -0800 | [diff] [blame] | 29 | #include <linux/idr.h> |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 30 | #include <linux/pm_opp.h> |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 31 | #include <linux/slab.h> |
| 32 | #include <linux/cpu.h> |
| 33 | #include <linux/cpu_cooling.h> |
| 34 | |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 35 | #include <trace/events/thermal.h> |
| 36 | |
Viresh Kumar | 07d888d | 2014-12-04 09:41:49 +0530 | [diff] [blame] | 37 | /* |
| 38 | * Cooling state <-> CPUFreq frequency |
| 39 | * |
| 40 | * Cooling states are translated to frequencies throughout this driver and this |
| 41 | * is the relation between them. |
| 42 | * |
| 43 | * Highest cooling state corresponds to lowest possible frequency. |
| 44 | * |
| 45 | * i.e. |
| 46 | * level 0 --> 1st Max Freq |
| 47 | * level 1 --> 2nd Max Freq |
| 48 | * ... |
| 49 | */ |
| 50 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 51 | /** |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 52 | * struct freq_table - frequency table along with power entries |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 53 | * @frequency: frequency in KHz |
| 54 | * @power: power in mW |
| 55 | * |
| 56 | * This structure is built when the cooling device registers and helps |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 57 | * in translating frequency to power and vice versa. |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 58 | */ |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 59 | struct freq_table { |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 60 | u32 frequency; |
| 61 | u32 power; |
| 62 | }; |
| 63 | |
| 64 | /** |
Viresh Kumar | 81ee14d | 2017-04-25 15:57:20 +0530 | [diff] [blame] | 65 | * struct time_in_idle - Idle time stats |
| 66 | * @time: previous reading of the absolute time that this cpu was idle |
| 67 | * @timestamp: wall time of the last invocation of get_cpu_idle_time_us() |
| 68 | */ |
| 69 | struct time_in_idle { |
| 70 | u64 time; |
| 71 | u64 timestamp; |
| 72 | }; |
| 73 | |
| 74 | /** |
Eduardo Valentin | 3b3c074 | 2013-04-17 17:11:56 +0000 | [diff] [blame] | 75 | * struct cpufreq_cooling_device - data for cooling device with cpufreq |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 76 | * @id: unique integer value corresponding to each cpufreq_cooling_device |
| 77 | * registered. |
Viresh Kumar | d72b401 | 2017-04-25 15:57:24 +0530 | [diff] [blame] | 78 | * @last_load: load measured by the latest call to cpufreq_get_requested_power() |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 79 | * @cpufreq_state: integer value representing the current state of cpufreq |
| 80 | * cooling devices. |
Viresh Kumar | 59f0d21 | 2015-07-30 12:40:33 +0530 | [diff] [blame] | 81 | * @clipped_freq: integer value representing the absolute value of the clipped |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 82 | * frequency. |
Viresh Kumar | dcc6c7f | 2014-12-04 09:42:02 +0530 | [diff] [blame] | 83 | * @max_level: maximum cooling level. One less than total number of valid |
| 84 | * cpufreq frequencies. |
Viresh Kumar | d72b401 | 2017-04-25 15:57:24 +0530 | [diff] [blame] | 85 | * @freq_table: Freq table in descending order of frequencies |
| 86 | * @cdev: thermal_cooling_device pointer to keep track of the |
| 87 | * registered cooling device. |
| 88 | * @policy: cpufreq policy. |
Javi Merino | fc4de35 | 2014-12-15 16:55:52 +0000 | [diff] [blame] | 89 | * @node: list_head to link all cpufreq_cooling_device together. |
Viresh Kumar | 81ee14d | 2017-04-25 15:57:20 +0530 | [diff] [blame] | 90 | * @idle_time: idle time stats |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 91 | * |
Viresh Kumar | beca605 | 2014-12-04 09:41:48 +0530 | [diff] [blame] | 92 | * This structure is required for keeping information of each registered |
| 93 | * cpufreq_cooling_device. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 94 | */ |
| 95 | struct cpufreq_cooling_device { |
| 96 | int id; |
Viresh Kumar | d72b401 | 2017-04-25 15:57:24 +0530 | [diff] [blame] | 97 | u32 last_load; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 98 | unsigned int cpufreq_state; |
Viresh Kumar | 59f0d21 | 2015-07-30 12:40:33 +0530 | [diff] [blame] | 99 | unsigned int clipped_freq; |
Viresh Kumar | dcc6c7f | 2014-12-04 09:42:02 +0530 | [diff] [blame] | 100 | unsigned int max_level; |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 101 | struct freq_table *freq_table; /* In descending order */ |
Viresh Kumar | d72b401 | 2017-04-25 15:57:24 +0530 | [diff] [blame] | 102 | struct thermal_cooling_device *cdev; |
| 103 | struct cpufreq_policy *policy; |
Yadwinder Singh Brar | 2dcd851 | 2014-11-07 19:12:29 +0530 | [diff] [blame] | 104 | struct list_head node; |
Viresh Kumar | 81ee14d | 2017-04-25 15:57:20 +0530 | [diff] [blame] | 105 | struct time_in_idle *idle_time; |
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 | |
Viresh Kumar | fb8ea30 | 2017-04-25 15:57:09 +0530 | [diff] [blame] | 108 | static DEFINE_IDA(cpufreq_ida); |
Russell King | 02373d7 | 2015-08-12 15:22:16 +0530 | [diff] [blame] | 109 | static DEFINE_MUTEX(cooling_list_lock); |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 110 | static LIST_HEAD(cpufreq_cdev_list); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 111 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 112 | /* Below code defines functions to be used for cpufreq as cooling device */ |
| 113 | |
| 114 | /** |
Viresh Kumar | 4843c4a | 2014-12-04 09:42:07 +0530 | [diff] [blame] | 115 | * get_level: Find the level for a particular frequency |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 116 | * @cpufreq_cdev: cpufreq_cdev for which the property is required |
Viresh Kumar | 4843c4a | 2014-12-04 09:42:07 +0530 | [diff] [blame] | 117 | * @freq: Frequency |
Eduardo Valentin | 82b9ee4 | 2013-04-17 17:12:00 +0000 | [diff] [blame] | 118 | * |
Viresh Kumar | da27f69 | 2017-04-25 15:57:21 +0530 | [diff] [blame] | 119 | * Return: level corresponding to the frequency. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 120 | */ |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 121 | static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev, |
Viresh Kumar | 4843c4a | 2014-12-04 09:42:07 +0530 | [diff] [blame] | 122 | unsigned int freq) |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 123 | { |
Viresh Kumar | da27f69 | 2017-04-25 15:57:21 +0530 | [diff] [blame] | 124 | struct freq_table *freq_table = cpufreq_cdev->freq_table; |
Viresh Kumar | 4843c4a | 2014-12-04 09:42:07 +0530 | [diff] [blame] | 125 | unsigned long level; |
Eduardo Valentin | 79491e5 | 2013-04-17 17:11:59 +0000 | [diff] [blame] | 126 | |
Viresh Kumar | da27f69 | 2017-04-25 15:57:21 +0530 | [diff] [blame] | 127 | for (level = 1; level <= cpufreq_cdev->max_level; level++) |
| 128 | if (freq > freq_table[level].frequency) |
Viresh Kumar | 4843c4a | 2014-12-04 09:42:07 +0530 | [diff] [blame] | 129 | break; |
Zhang Rui | a116776 | 2014-01-02 11:57:48 +0800 | [diff] [blame] | 130 | |
Viresh Kumar | da27f69 | 2017-04-25 15:57:21 +0530 | [diff] [blame] | 131 | return level - 1; |
Zhang Rui | fc35b35 | 2013-02-08 13:09:32 +0800 | [diff] [blame] | 132 | } |
| 133 | |
Eduardo Valentin | 44952d3 | 2013-04-17 17:12:05 +0000 | [diff] [blame] | 134 | /** |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 135 | * cpufreq_thermal_notifier - notifier callback for cpufreq policy change. |
| 136 | * @nb: struct notifier_block * with callback info. |
| 137 | * @event: value showing cpufreq event for which this function invoked. |
| 138 | * @data: callback-specific data |
Eduardo Valentin | bab3055 | 2013-04-17 17:12:09 +0000 | [diff] [blame] | 139 | * |
Javi Merino | 9746b6e | 2014-06-25 18:11:17 +0100 | [diff] [blame] | 140 | * Callback to hijack the notification on cpufreq policy transition. |
Eduardo Valentin | bab3055 | 2013-04-17 17:12:09 +0000 | [diff] [blame] | 141 | * Every time there is a change in policy, we will intercept and |
| 142 | * update the cpufreq policy with thermal constraints. |
| 143 | * |
| 144 | * Return: 0 (success) |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 145 | */ |
| 146 | static int cpufreq_thermal_notifier(struct notifier_block *nb, |
Eduardo Valentin | 5fda7f6 | 2013-04-17 17:12:11 +0000 | [diff] [blame] | 147 | unsigned long event, void *data) |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 148 | { |
| 149 | struct cpufreq_policy *policy = data; |
Viresh Kumar | abcbcc2 | 2015-07-30 12:40:34 +0530 | [diff] [blame] | 150 | unsigned long clipped_freq; |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 151 | struct cpufreq_cooling_device *cpufreq_cdev; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 152 | |
Viresh Kumar | a24af23 | 2015-07-30 12:40:32 +0530 | [diff] [blame] | 153 | if (event != CPUFREQ_ADJUST) |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 154 | return NOTIFY_DONE; |
Viresh Kumar | a24af23 | 2015-07-30 12:40:32 +0530 | [diff] [blame] | 155 | |
| 156 | mutex_lock(&cooling_list_lock); |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 157 | list_for_each_entry(cpufreq_cdev, &cpufreq_cdev_list, node) { |
Viresh Kumar | ba76dd9 | 2017-04-25 15:57:18 +0530 | [diff] [blame] | 158 | /* |
| 159 | * A new copy of the policy is sent to the notifier and can't |
| 160 | * compare that directly. |
| 161 | */ |
| 162 | if (policy->cpu != cpufreq_cdev->policy->cpu) |
Viresh Kumar | a24af23 | 2015-07-30 12:40:32 +0530 | [diff] [blame] | 163 | continue; |
| 164 | |
Viresh Kumar | 1afb9c5 | 2015-07-30 12:40:35 +0530 | [diff] [blame] | 165 | /* |
| 166 | * policy->max is the maximum allowed frequency defined by user |
| 167 | * and clipped_freq is the maximum that thermal constraints |
| 168 | * allow. |
| 169 | * |
| 170 | * If clipped_freq is lower than policy->max, then we need to |
| 171 | * readjust policy->max. |
| 172 | * |
| 173 | * But, if clipped_freq is greater than policy->max, we don't |
| 174 | * need to do anything. |
| 175 | */ |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 176 | clipped_freq = cpufreq_cdev->clipped_freq; |
Viresh Kumar | a24af23 | 2015-07-30 12:40:32 +0530 | [diff] [blame] | 177 | |
Viresh Kumar | 1afb9c5 | 2015-07-30 12:40:35 +0530 | [diff] [blame] | 178 | if (policy->max > clipped_freq) |
Viresh Kumar | abcbcc2 | 2015-07-30 12:40:34 +0530 | [diff] [blame] | 179 | cpufreq_verify_within_limits(policy, 0, clipped_freq); |
Viresh Kumar | a24af23 | 2015-07-30 12:40:32 +0530 | [diff] [blame] | 180 | break; |
Yadwinder Singh Brar | 2dcd851 | 2014-11-07 19:12:29 +0530 | [diff] [blame] | 181 | } |
Viresh Kumar | a24af23 | 2015-07-30 12:40:32 +0530 | [diff] [blame] | 182 | mutex_unlock(&cooling_list_lock); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 183 | |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 184 | return NOTIFY_OK; |
| 185 | } |
| 186 | |
| 187 | /** |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 188 | * update_freq_table() - Update the freq table with power numbers |
| 189 | * @cpufreq_cdev: the cpufreq cooling device in which to update the table |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 190 | * @capacitance: dynamic power coefficient for these cpus |
| 191 | * |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 192 | * Update the freq table with power numbers. This table will be used in |
| 193 | * cpu_power_to_freq() and cpu_freq_to_power() to convert between power and |
| 194 | * frequency efficiently. Power is stored in mW, frequency in KHz. The |
| 195 | * resulting table is in descending order. |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 196 | * |
Javi Merino | 459ac37 | 2015-08-17 19:21:42 +0100 | [diff] [blame] | 197 | * Return: 0 on success, -EINVAL if there are no OPPs for any CPUs, |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 198 | * or -ENOMEM if we run out of memory. |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 199 | */ |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 200 | static int update_freq_table(struct cpufreq_cooling_device *cpufreq_cdev, |
| 201 | u32 capacitance) |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 202 | { |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 203 | struct freq_table *freq_table = cpufreq_cdev->freq_table; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 204 | struct dev_pm_opp *opp; |
| 205 | struct device *dev = NULL; |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 206 | int num_opps = 0, cpu = cpufreq_cdev->policy->cpu, i; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 207 | |
Viresh Kumar | 02bacb2 | 2017-04-25 15:57:17 +0530 | [diff] [blame] | 208 | dev = get_cpu_device(cpu); |
| 209 | if (unlikely(!dev)) { |
| 210 | dev_warn(&cpufreq_cdev->cdev->device, |
| 211 | "No cpu device for cpu %d\n", cpu); |
| 212 | return -ENODEV; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Viresh Kumar | 02bacb2 | 2017-04-25 15:57:17 +0530 | [diff] [blame] | 215 | num_opps = dev_pm_opp_get_opp_count(dev); |
| 216 | if (num_opps < 0) |
| 217 | return num_opps; |
| 218 | |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 219 | /* |
| 220 | * The cpufreq table is also built from the OPP table and so the count |
| 221 | * should match. |
| 222 | */ |
| 223 | if (num_opps != cpufreq_cdev->max_level + 1) { |
| 224 | dev_warn(dev, "Number of OPPs not matching with max_levels\n"); |
Javi Merino | 459ac37 | 2015-08-17 19:21:42 +0100 | [diff] [blame] | 225 | return -EINVAL; |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 226 | } |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 227 | |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 228 | for (i = 0; i <= cpufreq_cdev->max_level; i++) { |
| 229 | unsigned long freq = freq_table[i].frequency * 1000; |
| 230 | u32 freq_mhz = freq_table[i].frequency / 1000; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 231 | u64 power; |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 232 | u32 voltage_mv; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 233 | |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 234 | /* |
| 235 | * Find ceil frequency as 'freq' may be slightly lower than OPP |
| 236 | * freq due to truncation while converting to kHz. |
| 237 | */ |
| 238 | opp = dev_pm_opp_find_freq_ceil(dev, &freq); |
| 239 | if (IS_ERR(opp)) { |
| 240 | dev_err(dev, "failed to get opp for %lu frequency\n", |
| 241 | freq); |
| 242 | return -EINVAL; |
Javi Merino | 459ac37 | 2015-08-17 19:21:42 +0100 | [diff] [blame] | 243 | } |
| 244 | |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 245 | voltage_mv = dev_pm_opp_get_voltage(opp) / 1000; |
Viresh Kumar | 8a31d9d9 | 2017-01-23 10:11:47 +0530 | [diff] [blame] | 246 | dev_pm_opp_put(opp); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 247 | |
| 248 | /* |
| 249 | * Do the multiplication with MHz and millivolt so as |
| 250 | * to not overflow. |
| 251 | */ |
| 252 | power = (u64)capacitance * freq_mhz * voltage_mv * voltage_mv; |
| 253 | do_div(power, 1000000000); |
| 254 | |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 255 | /* power is stored in mW */ |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 256 | freq_table[i].power = power; |
Javi Merino | eba4f88 | 2015-08-17 19:21:43 +0100 | [diff] [blame] | 257 | } |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 258 | |
Javi Merino | 459ac37 | 2015-08-17 19:21:42 +0100 | [diff] [blame] | 259 | return 0; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 262 | static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_cdev, |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 263 | u32 freq) |
| 264 | { |
| 265 | int i; |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 266 | struct freq_table *freq_table = cpufreq_cdev->freq_table; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 267 | |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 268 | for (i = 1; i <= cpufreq_cdev->max_level; i++) |
| 269 | if (freq > freq_table[i].frequency) |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 270 | break; |
| 271 | |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 272 | return freq_table[i - 1].power; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 275 | static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_cdev, |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 276 | u32 power) |
| 277 | { |
| 278 | int i; |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 279 | struct freq_table *freq_table = cpufreq_cdev->freq_table; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 280 | |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 281 | for (i = 1; i <= cpufreq_cdev->max_level; i++) |
| 282 | if (power > freq_table[i].power) |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 283 | break; |
| 284 | |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 285 | return freq_table[i - 1].frequency; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /** |
| 289 | * get_load() - get load for a cpu since last updated |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 290 | * @cpufreq_cdev: &struct cpufreq_cooling_device for this cpu |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 291 | * @cpu: cpu number |
Viresh Kumar | ba76dd9 | 2017-04-25 15:57:18 +0530 | [diff] [blame] | 292 | * @cpu_idx: index of the cpu in time_in_idle* |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 293 | * |
| 294 | * Return: The average load of cpu @cpu in percentage since this |
| 295 | * function was last called. |
| 296 | */ |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 297 | static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu, |
Javi Merino | a53b839 | 2016-02-11 12:00:51 +0000 | [diff] [blame] | 298 | int cpu_idx) |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 299 | { |
| 300 | u32 load; |
| 301 | u64 now, now_idle, delta_time, delta_idle; |
Viresh Kumar | 81ee14d | 2017-04-25 15:57:20 +0530 | [diff] [blame] | 302 | struct time_in_idle *idle_time = &cpufreq_cdev->idle_time[cpu_idx]; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 303 | |
| 304 | now_idle = get_cpu_idle_time(cpu, &now, 0); |
Viresh Kumar | 81ee14d | 2017-04-25 15:57:20 +0530 | [diff] [blame] | 305 | delta_idle = now_idle - idle_time->time; |
| 306 | delta_time = now - idle_time->timestamp; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 307 | |
| 308 | if (delta_time <= delta_idle) |
| 309 | load = 0; |
| 310 | else |
| 311 | load = div64_u64(100 * (delta_time - delta_idle), delta_time); |
| 312 | |
Viresh Kumar | 81ee14d | 2017-04-25 15:57:20 +0530 | [diff] [blame] | 313 | idle_time->time = now_idle; |
| 314 | idle_time->timestamp = now; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 315 | |
| 316 | return load; |
| 317 | } |
| 318 | |
| 319 | /** |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 320 | * get_dynamic_power() - calculate the dynamic power |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 321 | * @cpufreq_cdev: &cpufreq_cooling_device for this cdev |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 322 | * @freq: current frequency |
| 323 | * |
| 324 | * Return: the dynamic power consumed by the cpus described by |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 325 | * @cpufreq_cdev. |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 326 | */ |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 327 | static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_cdev, |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 328 | unsigned long freq) |
| 329 | { |
| 330 | u32 raw_cpu_power; |
| 331 | |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 332 | raw_cpu_power = cpu_freq_to_power(cpufreq_cdev, freq); |
| 333 | return (raw_cpu_power * cpufreq_cdev->last_load) / 100; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 334 | } |
| 335 | |
Eduardo Valentin | 1b9e352 | 2013-04-17 17:12:02 +0000 | [diff] [blame] | 336 | /* cpufreq cooling device callback functions are defined below */ |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 337 | |
| 338 | /** |
| 339 | * cpufreq_get_max_state - callback function to get the max cooling state. |
| 340 | * @cdev: thermal cooling device pointer. |
| 341 | * @state: fill this variable with the max cooling state. |
Eduardo Valentin | 62c0042 | 2013-04-17 17:12:12 +0000 | [diff] [blame] | 342 | * |
| 343 | * Callback for the thermal cooling device to return the cpufreq |
| 344 | * max cooling state. |
| 345 | * |
| 346 | * Return: 0 on success, an error code otherwise. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 347 | */ |
| 348 | static int cpufreq_get_max_state(struct thermal_cooling_device *cdev, |
| 349 | unsigned long *state) |
| 350 | { |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 351 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 352 | |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 353 | *state = cpufreq_cdev->max_level; |
Viresh Kumar | dcc6c7f | 2014-12-04 09:42:02 +0530 | [diff] [blame] | 354 | return 0; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | /** |
| 358 | * cpufreq_get_cur_state - callback function to get the current cooling state. |
| 359 | * @cdev: thermal cooling device pointer. |
| 360 | * @state: fill this variable with the current cooling state. |
Eduardo Valentin | 3672552 | 2013-04-17 17:12:13 +0000 | [diff] [blame] | 361 | * |
| 362 | * Callback for the thermal cooling device to return the cpufreq |
| 363 | * current cooling state. |
| 364 | * |
| 365 | * Return: 0 on success, an error code otherwise. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 366 | */ |
| 367 | static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev, |
| 368 | unsigned long *state) |
| 369 | { |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 370 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 371 | |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 372 | *state = cpufreq_cdev->cpufreq_state; |
Eduardo Valentin | 79491e5 | 2013-04-17 17:11:59 +0000 | [diff] [blame] | 373 | |
hongbo.zhang | 160b7d8 | 2012-10-30 17:48:59 +0100 | [diff] [blame] | 374 | return 0; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | /** |
| 378 | * cpufreq_set_cur_state - callback function to set the current cooling state. |
| 379 | * @cdev: thermal cooling device pointer. |
| 380 | * @state: set this variable to the current cooling state. |
Eduardo Valentin | 56e05fd | 2013-04-17 17:12:14 +0000 | [diff] [blame] | 381 | * |
| 382 | * Callback for the thermal cooling device to change the cpufreq |
| 383 | * current cooling state. |
| 384 | * |
| 385 | * Return: 0 on success, an error code otherwise. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 386 | */ |
| 387 | static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev, |
| 388 | unsigned long state) |
| 389 | { |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 390 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
Viresh Kumar | 5194fe4 | 2014-12-04 09:42:00 +0530 | [diff] [blame] | 391 | unsigned int clip_freq; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 392 | |
Viresh Kumar | 4843c4a | 2014-12-04 09:42:07 +0530 | [diff] [blame] | 393 | /* Request state should be less than max_level */ |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 394 | if (WARN_ON(state > cpufreq_cdev->max_level)) |
Viresh Kumar | 4843c4a | 2014-12-04 09:42:07 +0530 | [diff] [blame] | 395 | return -EINVAL; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 396 | |
Viresh Kumar | 5194fe4 | 2014-12-04 09:42:00 +0530 | [diff] [blame] | 397 | /* Check if the old cooling action is same as new cooling action */ |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 398 | if (cpufreq_cdev->cpufreq_state == state) |
Viresh Kumar | 5194fe4 | 2014-12-04 09:42:00 +0530 | [diff] [blame] | 399 | return 0; |
| 400 | |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 401 | clip_freq = cpufreq_cdev->freq_table[state].frequency; |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 402 | cpufreq_cdev->cpufreq_state = state; |
| 403 | cpufreq_cdev->clipped_freq = clip_freq; |
Viresh Kumar | 5194fe4 | 2014-12-04 09:42:00 +0530 | [diff] [blame] | 404 | |
Viresh Kumar | ba76dd9 | 2017-04-25 15:57:18 +0530 | [diff] [blame] | 405 | cpufreq_update_policy(cpufreq_cdev->policy->cpu); |
Viresh Kumar | 5194fe4 | 2014-12-04 09:42:00 +0530 | [diff] [blame] | 406 | |
| 407 | return 0; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 408 | } |
| 409 | |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 410 | /** |
| 411 | * cpufreq_get_requested_power() - get the current power |
| 412 | * @cdev: &thermal_cooling_device pointer |
| 413 | * @tz: a valid thermal zone device pointer |
| 414 | * @power: pointer in which to store the resulting power |
| 415 | * |
| 416 | * Calculate the current power consumption of the cpus in milliwatts |
| 417 | * and store it in @power. This function should actually calculate |
| 418 | * the requested power, but it's hard to get the frequency that |
| 419 | * cpufreq would have assigned if there were no thermal limits. |
| 420 | * Instead, we calculate the current power on the assumption that the |
| 421 | * immediate future will look like the immediate past. |
| 422 | * |
| 423 | * We use the current frequency and the average load since this |
| 424 | * function was last called. In reality, there could have been |
| 425 | * multiple opps since this function was last called and that affects |
| 426 | * the load calculation. While it's not perfectly accurate, this |
| 427 | * simplification is good enough and works. REVISIT this, as more |
| 428 | * complex code may be needed if experiments show that it's not |
| 429 | * accurate enough. |
| 430 | * |
| 431 | * Return: 0 on success, -E* if getting the static power failed. |
| 432 | */ |
| 433 | static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev, |
| 434 | struct thermal_zone_device *tz, |
| 435 | u32 *power) |
| 436 | { |
| 437 | unsigned long freq; |
Viresh Kumar | 84fe2ca | 2017-12-05 11:02:46 +0530 | [diff] [blame] | 438 | int i = 0, cpu; |
| 439 | u32 total_load = 0; |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 440 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
Viresh Kumar | ba76dd9 | 2017-04-25 15:57:18 +0530 | [diff] [blame] | 441 | struct cpufreq_policy *policy = cpufreq_cdev->policy; |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 442 | u32 *load_cpu = NULL; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 443 | |
Viresh Kumar | ba76dd9 | 2017-04-25 15:57:18 +0530 | [diff] [blame] | 444 | freq = cpufreq_quick_get(policy->cpu); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 445 | |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 446 | if (trace_thermal_power_cpu_get_power_enabled()) { |
Viresh Kumar | ba76dd9 | 2017-04-25 15:57:18 +0530 | [diff] [blame] | 447 | u32 ncpus = cpumask_weight(policy->related_cpus); |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 448 | |
Vaishali Thakkar | a71544c | 2015-08-19 11:52:19 +0530 | [diff] [blame] | 449 | load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL); |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Viresh Kumar | ba76dd9 | 2017-04-25 15:57:18 +0530 | [diff] [blame] | 452 | for_each_cpu(cpu, policy->related_cpus) { |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 453 | u32 load; |
| 454 | |
| 455 | if (cpu_online(cpu)) |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 456 | load = get_load(cpufreq_cdev, cpu, i); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 457 | else |
| 458 | load = 0; |
| 459 | |
| 460 | total_load += load; |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 461 | if (trace_thermal_power_cpu_limit_enabled() && load_cpu) |
| 462 | load_cpu[i] = load; |
| 463 | |
| 464 | i++; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 467 | cpufreq_cdev->last_load = total_load; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 468 | |
Viresh Kumar | 84fe2ca | 2017-12-05 11:02:46 +0530 | [diff] [blame] | 469 | *power = get_dynamic_power(cpufreq_cdev, freq); |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 470 | |
| 471 | if (load_cpu) { |
Viresh Kumar | ba76dd9 | 2017-04-25 15:57:18 +0530 | [diff] [blame] | 472 | trace_thermal_power_cpu_get_power(policy->related_cpus, freq, |
Viresh Kumar | 84fe2ca | 2017-12-05 11:02:46 +0530 | [diff] [blame] | 473 | load_cpu, i, *power); |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 474 | |
Vaishali Thakkar | a71544c | 2015-08-19 11:52:19 +0530 | [diff] [blame] | 475 | kfree(load_cpu); |
Javi Merino | 6828a47 | 2015-03-02 17:17:20 +0000 | [diff] [blame] | 476 | } |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 477 | |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * cpufreq_state2power() - convert a cpu cdev state to power consumed |
| 483 | * @cdev: &thermal_cooling_device pointer |
| 484 | * @tz: a valid thermal zone device pointer |
| 485 | * @state: cooling device state to be converted |
| 486 | * @power: pointer in which to store the resulting power |
| 487 | * |
| 488 | * Convert cooling device state @state into power consumption in |
| 489 | * milliwatts assuming 100% load. Store the calculated power in |
| 490 | * @power. |
| 491 | * |
| 492 | * Return: 0 on success, -EINVAL if the cooling device state could not |
| 493 | * be converted into a frequency or other -E* if there was an error |
| 494 | * when calculating the static power. |
| 495 | */ |
| 496 | static int cpufreq_state2power(struct thermal_cooling_device *cdev, |
| 497 | struct thermal_zone_device *tz, |
| 498 | unsigned long state, u32 *power) |
| 499 | { |
| 500 | unsigned int freq, num_cpus; |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 501 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 502 | |
Viresh Kumar | cb1b631 | 2017-04-25 15:57:23 +0530 | [diff] [blame] | 503 | /* Request state should be less than max_level */ |
| 504 | if (WARN_ON(state > cpufreq_cdev->max_level)) |
| 505 | return -EINVAL; |
| 506 | |
Viresh Kumar | ba76dd9 | 2017-04-25 15:57:18 +0530 | [diff] [blame] | 507 | num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 508 | |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 509 | freq = cpufreq_cdev->freq_table[state].frequency; |
Viresh Kumar | 84fe2ca | 2017-12-05 11:02:46 +0530 | [diff] [blame] | 510 | *power = cpu_freq_to_power(cpufreq_cdev, freq) * num_cpus; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 511 | |
Viresh Kumar | 84fe2ca | 2017-12-05 11:02:46 +0530 | [diff] [blame] | 512 | return 0; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | /** |
| 516 | * cpufreq_power2state() - convert power to a cooling device state |
| 517 | * @cdev: &thermal_cooling_device pointer |
| 518 | * @tz: a valid thermal zone device pointer |
| 519 | * @power: power in milliwatts to be converted |
| 520 | * @state: pointer in which to store the resulting state |
| 521 | * |
| 522 | * Calculate a cooling device state for the cpus described by @cdev |
| 523 | * that would allow them to consume at most @power mW and store it in |
| 524 | * @state. Note that this calculation depends on external factors |
| 525 | * such as the cpu load or the current static power. Calling this |
| 526 | * function with the same power as input can yield different cooling |
| 527 | * device states depending on those external factors. |
| 528 | * |
| 529 | * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if |
| 530 | * the calculated frequency could not be converted to a valid state. |
| 531 | * The latter should not happen unless the frequencies available to |
| 532 | * cpufreq have changed since the initialization of the cpu cooling |
| 533 | * device. |
| 534 | */ |
| 535 | static int cpufreq_power2state(struct thermal_cooling_device *cdev, |
| 536 | struct thermal_zone_device *tz, u32 power, |
| 537 | unsigned long *state) |
| 538 | { |
Viresh Kumar | ba76dd9 | 2017-04-25 15:57:18 +0530 | [diff] [blame] | 539 | unsigned int cur_freq, target_freq; |
Viresh Kumar | 84fe2ca | 2017-12-05 11:02:46 +0530 | [diff] [blame] | 540 | u32 last_load, normalised_power; |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 541 | struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata; |
Viresh Kumar | ba76dd9 | 2017-04-25 15:57:18 +0530 | [diff] [blame] | 542 | struct cpufreq_policy *policy = cpufreq_cdev->policy; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 543 | |
Viresh Kumar | ba76dd9 | 2017-04-25 15:57:18 +0530 | [diff] [blame] | 544 | cur_freq = cpufreq_quick_get(policy->cpu); |
Viresh Kumar | 84fe2ca | 2017-12-05 11:02:46 +0530 | [diff] [blame] | 545 | power = power > 0 ? power : 0; |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 546 | last_load = cpufreq_cdev->last_load ?: 1; |
Viresh Kumar | 84fe2ca | 2017-12-05 11:02:46 +0530 | [diff] [blame] | 547 | normalised_power = (power * 100) / last_load; |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 548 | target_freq = cpu_power_to_freq(cpufreq_cdev, normalised_power); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 549 | |
Viresh Kumar | 3e08b2d | 2017-04-25 15:57:12 +0530 | [diff] [blame] | 550 | *state = get_level(cpufreq_cdev, target_freq); |
Viresh Kumar | ba76dd9 | 2017-04-25 15:57:18 +0530 | [diff] [blame] | 551 | trace_thermal_power_cpu_limit(policy->related_cpus, target_freq, *state, |
| 552 | power); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 553 | return 0; |
| 554 | } |
| 555 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 556 | /* Bind cpufreq callbacks to thermal cooling device ops */ |
Brendan Jackman | a305a43 | 2016-08-17 16:14:59 +0100 | [diff] [blame] | 557 | |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 558 | static struct thermal_cooling_device_ops cpufreq_cooling_ops = { |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 559 | .get_max_state = cpufreq_get_max_state, |
| 560 | .get_cur_state = cpufreq_get_cur_state, |
| 561 | .set_cur_state = cpufreq_set_cur_state, |
| 562 | }; |
| 563 | |
Brendan Jackman | a305a43 | 2016-08-17 16:14:59 +0100 | [diff] [blame] | 564 | static struct thermal_cooling_device_ops cpufreq_power_cooling_ops = { |
| 565 | .get_max_state = cpufreq_get_max_state, |
| 566 | .get_cur_state = cpufreq_get_cur_state, |
| 567 | .set_cur_state = cpufreq_set_cur_state, |
| 568 | .get_requested_power = cpufreq_get_requested_power, |
| 569 | .state2power = cpufreq_state2power, |
| 570 | .power2state = cpufreq_power2state, |
| 571 | }; |
| 572 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 573 | /* Notifier for cpufreq policy change */ |
| 574 | static struct notifier_block thermal_cpufreq_notifier_block = { |
| 575 | .notifier_call = cpufreq_thermal_notifier, |
| 576 | }; |
| 577 | |
Viresh Kumar | f685901 | 2014-12-04 09:42:06 +0530 | [diff] [blame] | 578 | static unsigned int find_next_max(struct cpufreq_frequency_table *table, |
| 579 | unsigned int prev_max) |
| 580 | { |
| 581 | struct cpufreq_frequency_table *pos; |
| 582 | unsigned int max = 0; |
| 583 | |
| 584 | cpufreq_for_each_valid_entry(pos, table) { |
| 585 | if (pos->frequency > max && pos->frequency < prev_max) |
| 586 | max = pos->frequency; |
| 587 | } |
| 588 | |
| 589 | return max; |
| 590 | } |
| 591 | |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 592 | /** |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 593 | * __cpufreq_cooling_register - helper function to create cpufreq cooling device |
| 594 | * @np: a valid struct device_node to the cooling device device tree node |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 595 | * @policy: cpufreq policy |
Viresh Kumar | 405fb82 | 2014-12-04 09:41:55 +0530 | [diff] [blame] | 596 | * Normally this should be same as cpufreq policy->related_cpus. |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 597 | * @capacitance: dynamic power coefficient for these cpus |
Eduardo Valentin | 12cb08b | 2013-04-17 17:12:15 +0000 | [diff] [blame] | 598 | * |
| 599 | * This interface function registers the cpufreq cooling device with the name |
| 600 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 601 | * cooling devices. It also gives the opportunity to link the cooling device |
| 602 | * 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] | 603 | * |
| 604 | * Return: a valid struct thermal_cooling_device pointer on success, |
| 605 | * on failure, it returns a corresponding ERR_PTR(). |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 606 | */ |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 607 | static struct thermal_cooling_device * |
| 608 | __cpufreq_cooling_register(struct device_node *np, |
Viresh Kumar | 84fe2ca | 2017-12-05 11:02:46 +0530 | [diff] [blame] | 609 | struct cpufreq_policy *policy, u32 capacitance) |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 610 | { |
Viresh Kumar | 04bdbdf | 2017-04-25 15:57:11 +0530 | [diff] [blame] | 611 | struct thermal_cooling_device *cdev; |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 612 | struct cpufreq_cooling_device *cpufreq_cdev; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 613 | char dev_name[THERMAL_NAME_LENGTH]; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 614 | unsigned int freq, i, num_cpus; |
Viresh Kumar | 405fb82 | 2014-12-04 09:41:55 +0530 | [diff] [blame] | 615 | int ret; |
Brendan Jackman | a305a43 | 2016-08-17 16:14:59 +0100 | [diff] [blame] | 616 | struct thermal_cooling_device_ops *cooling_ops; |
Matthew Wilcox | 088db93 | 2017-03-10 18:33:28 +0000 | [diff] [blame] | 617 | bool first; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 618 | |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 619 | if (IS_ERR_OR_NULL(policy)) { |
Arvind Yadav | b2fd708 | 2017-10-24 13:20:39 +0530 | [diff] [blame] | 620 | pr_err("%s: cpufreq policy isn't valid: %p\n", __func__, policy); |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 621 | return ERR_PTR(-EINVAL); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 622 | } |
Eduardo Valentin | 0f1be51 | 2014-12-04 09:41:43 +0530 | [diff] [blame] | 623 | |
Viresh Kumar | 55d8529 | 2017-04-25 15:57:15 +0530 | [diff] [blame] | 624 | i = cpufreq_table_count_valid_entries(policy); |
| 625 | if (!i) { |
| 626 | pr_debug("%s: CPUFreq table not found or has no valid entries\n", |
| 627 | __func__); |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 628 | return ERR_PTR(-ENODEV); |
Viresh Kumar | f8bfc11 | 2016-06-03 10:58:47 +0530 | [diff] [blame] | 629 | } |
| 630 | |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 631 | cpufreq_cdev = kzalloc(sizeof(*cpufreq_cdev), GFP_KERNEL); |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 632 | if (!cpufreq_cdev) |
| 633 | return ERR_PTR(-ENOMEM); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 634 | |
Viresh Kumar | b12b651 | 2017-04-25 15:57:16 +0530 | [diff] [blame] | 635 | cpufreq_cdev->policy = policy; |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 636 | num_cpus = cpumask_weight(policy->related_cpus); |
Viresh Kumar | 81ee14d | 2017-04-25 15:57:20 +0530 | [diff] [blame] | 637 | cpufreq_cdev->idle_time = kcalloc(num_cpus, |
| 638 | sizeof(*cpufreq_cdev->idle_time), |
| 639 | GFP_KERNEL); |
| 640 | if (!cpufreq_cdev->idle_time) { |
Viresh Kumar | 04bdbdf | 2017-04-25 15:57:11 +0530 | [diff] [blame] | 641 | cdev = ERR_PTR(-ENOMEM); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 642 | goto free_cdev; |
| 643 | } |
| 644 | |
Viresh Kumar | 55d8529 | 2017-04-25 15:57:15 +0530 | [diff] [blame] | 645 | /* max_level is an index, not a counter */ |
| 646 | cpufreq_cdev->max_level = i - 1; |
Viresh Kumar | dcc6c7f | 2014-12-04 09:42:02 +0530 | [diff] [blame] | 647 | |
Viresh Kumar | f19b1a1 | 2017-05-23 12:33:06 +0530 | [diff] [blame] | 648 | cpufreq_cdev->freq_table = kmalloc_array(i, |
| 649 | sizeof(*cpufreq_cdev->freq_table), |
| 650 | GFP_KERNEL); |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 651 | if (!cpufreq_cdev->freq_table) { |
Viresh Kumar | 04bdbdf | 2017-04-25 15:57:11 +0530 | [diff] [blame] | 652 | cdev = ERR_PTR(-ENOMEM); |
Viresh Kumar | 81ee14d | 2017-04-25 15:57:20 +0530 | [diff] [blame] | 653 | goto free_idle_time; |
Viresh Kumar | f685901 | 2014-12-04 09:42:06 +0530 | [diff] [blame] | 654 | } |
| 655 | |
Matthew Wilcox | ae60608 | 2016-12-21 09:47:05 -0800 | [diff] [blame] | 656 | ret = ida_simple_get(&cpufreq_ida, 0, 0, GFP_KERNEL); |
| 657 | if (ret < 0) { |
Viresh Kumar | 04bdbdf | 2017-04-25 15:57:11 +0530 | [diff] [blame] | 658 | cdev = ERR_PTR(ret); |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 659 | goto free_table; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 660 | } |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 661 | cpufreq_cdev->id = ret; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 662 | |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 663 | snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d", |
| 664 | cpufreq_cdev->id); |
| 665 | |
Viresh Kumar | f685901 | 2014-12-04 09:42:06 +0530 | [diff] [blame] | 666 | /* Fill freq-table in descending order of frequencies */ |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 667 | for (i = 0, freq = -1; i <= cpufreq_cdev->max_level; i++) { |
Viresh Kumar | 55d8529 | 2017-04-25 15:57:15 +0530 | [diff] [blame] | 668 | freq = find_next_max(policy->freq_table, freq); |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 669 | cpufreq_cdev->freq_table[i].frequency = freq; |
Viresh Kumar | f685901 | 2014-12-04 09:42:06 +0530 | [diff] [blame] | 670 | |
| 671 | /* Warn for duplicate entries */ |
| 672 | if (!freq) |
| 673 | pr_warn("%s: table has duplicate entries\n", __func__); |
| 674 | else |
| 675 | pr_debug("%s: freq:%u KHz\n", __func__, freq); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 676 | } |
Viresh Kumar | f685901 | 2014-12-04 09:42:06 +0530 | [diff] [blame] | 677 | |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 678 | if (capacitance) { |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 679 | ret = update_freq_table(cpufreq_cdev, capacitance); |
| 680 | if (ret) { |
| 681 | cdev = ERR_PTR(ret); |
| 682 | goto remove_ida; |
| 683 | } |
| 684 | |
| 685 | cooling_ops = &cpufreq_power_cooling_ops; |
| 686 | } else { |
| 687 | cooling_ops = &cpufreq_cooling_ops; |
| 688 | } |
Lukasz Luba | f840ab1 | 2016-05-31 11:32:02 +0100 | [diff] [blame] | 689 | |
Viresh Kumar | 04bdbdf | 2017-04-25 15:57:11 +0530 | [diff] [blame] | 690 | cdev = thermal_of_cooling_device_register(np, dev_name, cpufreq_cdev, |
| 691 | cooling_ops); |
| 692 | if (IS_ERR(cdev)) |
Matthew Wilcox | ae60608 | 2016-12-21 09:47:05 -0800 | [diff] [blame] | 693 | goto remove_ida; |
Lukasz Luba | f840ab1 | 2016-05-31 11:32:02 +0100 | [diff] [blame] | 694 | |
Viresh Kumar | 349d39d | 2017-04-25 15:57:19 +0530 | [diff] [blame] | 695 | cpufreq_cdev->clipped_freq = cpufreq_cdev->freq_table[0].frequency; |
Viresh Kumar | 04bdbdf | 2017-04-25 15:57:11 +0530 | [diff] [blame] | 696 | cpufreq_cdev->cdev = cdev; |
Viresh Kumar | 92e615e | 2014-12-04 09:41:51 +0530 | [diff] [blame] | 697 | |
Russell King | 02373d7 | 2015-08-12 15:22:16 +0530 | [diff] [blame] | 698 | mutex_lock(&cooling_list_lock); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 699 | /* Register the notifier for first cpufreq cooling device */ |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 700 | first = list_empty(&cpufreq_cdev_list); |
| 701 | list_add(&cpufreq_cdev->node, &cpufreq_cdev_list); |
Matthew Wilcox | 088db93 | 2017-03-10 18:33:28 +0000 | [diff] [blame] | 702 | mutex_unlock(&cooling_list_lock); |
| 703 | |
| 704 | if (first) |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 705 | cpufreq_register_notifier(&thermal_cpufreq_notifier_block, |
Eduardo Valentin | 5fda7f6 | 2013-04-17 17:12:11 +0000 | [diff] [blame] | 706 | CPUFREQ_POLICY_NOTIFIER); |
Eduardo Valentin | 79491e5 | 2013-04-17 17:11:59 +0000 | [diff] [blame] | 707 | |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 708 | return cdev; |
Viresh Kumar | 730abe0 | 2014-12-04 09:41:58 +0530 | [diff] [blame] | 709 | |
Matthew Wilcox | ae60608 | 2016-12-21 09:47:05 -0800 | [diff] [blame] | 710 | remove_ida: |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 711 | ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id); |
Viresh Kumar | f685901 | 2014-12-04 09:42:06 +0530 | [diff] [blame] | 712 | free_table: |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 713 | kfree(cpufreq_cdev->freq_table); |
Viresh Kumar | 81ee14d | 2017-04-25 15:57:20 +0530 | [diff] [blame] | 714 | free_idle_time: |
| 715 | kfree(cpufreq_cdev->idle_time); |
Viresh Kumar | 730abe0 | 2014-12-04 09:41:58 +0530 | [diff] [blame] | 716 | free_cdev: |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 717 | kfree(cpufreq_cdev); |
Viresh Kumar | 04bdbdf | 2017-04-25 15:57:11 +0530 | [diff] [blame] | 718 | return cdev; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 719 | } |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 720 | |
| 721 | /** |
| 722 | * cpufreq_cooling_register - function to create cpufreq cooling device. |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 723 | * @policy: cpufreq policy |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 724 | * |
| 725 | * This interface function registers the cpufreq cooling device with the name |
| 726 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq |
| 727 | * cooling devices. |
| 728 | * |
| 729 | * Return: a valid struct thermal_cooling_device pointer on success, |
| 730 | * on failure, it returns a corresponding ERR_PTR(). |
| 731 | */ |
| 732 | struct thermal_cooling_device * |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 733 | cpufreq_cooling_register(struct cpufreq_policy *policy) |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 734 | { |
Viresh Kumar | 84fe2ca | 2017-12-05 11:02:46 +0530 | [diff] [blame] | 735 | return __cpufreq_cooling_register(NULL, policy, 0); |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 736 | } |
Eduardo Valentin | 243dbd9 | 2013-04-17 17:11:57 +0000 | [diff] [blame] | 737 | EXPORT_SYMBOL_GPL(cpufreq_cooling_register); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 738 | |
| 739 | /** |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 740 | * of_cpufreq_cooling_register - function to create cpufreq cooling device. |
Viresh Kumar | 4d753aa | 2017-04-25 15:57:14 +0530 | [diff] [blame] | 741 | * @policy: cpufreq policy |
Eduardo Valentin | 39d99cf | 2013-09-12 19:26:45 -0400 | [diff] [blame] | 742 | * |
| 743 | * This interface function registers the cpufreq cooling device with the name |
| 744 | * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq |
| 745 | * cooling devices. Using this API, the cpufreq cooling device will be |
| 746 | * linked to the device tree node provided. |
| 747 | * |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 748 | * Using this function, the cooling device will implement the power |
| 749 | * extensions by using a simple cpu power model. The cpus must have |
| 750 | * registered their OPPs using the OPP library. |
| 751 | * |
Viresh Kumar | f5f263f | 2017-12-05 11:02:43 +0530 | [diff] [blame] | 752 | * It also takes into account, if property present in policy CPU node, the |
| 753 | * static power consumed by the cpu. |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 754 | * |
| 755 | * Return: a valid struct thermal_cooling_device pointer on success, |
Viresh Kumar | f5f263f | 2017-12-05 11:02:43 +0530 | [diff] [blame] | 756 | * and NULL on failure. |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 757 | */ |
| 758 | struct thermal_cooling_device * |
Viresh Kumar | 3ebb62f | 2017-12-05 11:02:45 +0530 | [diff] [blame] | 759 | of_cpufreq_cooling_register(struct cpufreq_policy *policy) |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 760 | { |
Viresh Kumar | f5f263f | 2017-12-05 11:02:43 +0530 | [diff] [blame] | 761 | struct device_node *np = of_get_cpu_node(policy->cpu, NULL); |
| 762 | struct thermal_cooling_device *cdev = NULL; |
| 763 | u32 capacitance = 0; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 764 | |
Viresh Kumar | f5f263f | 2017-12-05 11:02:43 +0530 | [diff] [blame] | 765 | if (!np) { |
| 766 | pr_err("cpu_cooling: OF node not available for cpu%d\n", |
| 767 | policy->cpu); |
| 768 | return NULL; |
| 769 | } |
| 770 | |
| 771 | if (of_find_property(np, "#cooling-cells", NULL)) { |
| 772 | of_property_read_u32(np, "dynamic-power-coefficient", |
| 773 | &capacitance); |
| 774 | |
Viresh Kumar | 84fe2ca | 2017-12-05 11:02:46 +0530 | [diff] [blame] | 775 | cdev = __cpufreq_cooling_register(np, policy, capacitance); |
Viresh Kumar | f5f263f | 2017-12-05 11:02:43 +0530 | [diff] [blame] | 776 | if (IS_ERR(cdev)) { |
| 777 | pr_err("cpu_cooling: cpu%d is not running as cooling device: %ld\n", |
| 778 | policy->cpu, PTR_ERR(cdev)); |
| 779 | cdev = NULL; |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | of_node_put(np); |
| 784 | return cdev; |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 785 | } |
Viresh Kumar | 3ebb62f | 2017-12-05 11:02:45 +0530 | [diff] [blame] | 786 | EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register); |
Javi Merino | c36cf07 | 2015-02-26 19:00:29 +0000 | [diff] [blame] | 787 | |
| 788 | /** |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 789 | * cpufreq_cooling_unregister - function to remove cpufreq cooling device. |
| 790 | * @cdev: thermal cooling device pointer. |
Eduardo Valentin | 135266b | 2013-04-17 17:12:16 +0000 | [diff] [blame] | 791 | * |
| 792 | * This interface function unregisters the "thermal-cpufreq-%x" cooling device. |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 793 | */ |
| 794 | void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev) |
| 795 | { |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 796 | struct cpufreq_cooling_device *cpufreq_cdev; |
Matthew Wilcox | 088db93 | 2017-03-10 18:33:28 +0000 | [diff] [blame] | 797 | bool last; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 798 | |
Eduardo Valentin | 50e66c7 | 2013-08-15 10:54:46 -0400 | [diff] [blame] | 799 | if (!cdev) |
| 800 | return; |
| 801 | |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 802 | cpufreq_cdev = cdev->devdata; |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 803 | |
Matthew Wilcox | ae60608 | 2016-12-21 09:47:05 -0800 | [diff] [blame] | 804 | mutex_lock(&cooling_list_lock); |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 805 | list_del(&cpufreq_cdev->node); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 806 | /* Unregister the notifier for the last cpufreq cooling device */ |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 807 | last = list_empty(&cpufreq_cdev_list); |
Matthew Wilcox | 088db93 | 2017-03-10 18:33:28 +0000 | [diff] [blame] | 808 | mutex_unlock(&cooling_list_lock); |
| 809 | |
| 810 | if (last) |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 811 | cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block, |
Eduardo Valentin | 5fda7f6 | 2013-04-17 17:12:11 +0000 | [diff] [blame] | 812 | CPUFREQ_POLICY_NOTIFIER); |
Russell King | 02373d7 | 2015-08-12 15:22:16 +0530 | [diff] [blame] | 813 | |
Viresh Kumar | 04bdbdf | 2017-04-25 15:57:11 +0530 | [diff] [blame] | 814 | thermal_cooling_device_unregister(cpufreq_cdev->cdev); |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 815 | ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id); |
Viresh Kumar | 81ee14d | 2017-04-25 15:57:20 +0530 | [diff] [blame] | 816 | kfree(cpufreq_cdev->idle_time); |
Viresh Kumar | 1dea432 | 2017-04-25 15:57:10 +0530 | [diff] [blame] | 817 | kfree(cpufreq_cdev->freq_table); |
| 818 | kfree(cpufreq_cdev); |
Amit Daniel Kachhap | 0236141 | 2012-08-16 17:11:40 +0530 | [diff] [blame] | 819 | } |
Eduardo Valentin | 243dbd9 | 2013-04-17 17:11:57 +0000 | [diff] [blame] | 820 | EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister); |