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