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