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