blob: 4c5db59a619b07c23aafeb50e0f5fad6aff98517 [file] [log] [blame]
Daniel Lezcano0fac9e2f2019-04-28 11:51:04 +02001// SPDX-License-Identifier: GPL-2.0
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05302/*
3 * linux/drivers/thermal/cpu_cooling.c
4 *
5 * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05306 *
Daniel Lezcano42cd9b02019-04-28 11:51:03 +02007 * Copyright (C) 2012-2018 Linaro Limited.
8 *
9 * Authors: Amit Daniel <amit.kachhap@linaro.org>
10 * Viresh Kumar <viresh.kumar@linaro.org>
Viresh Kumar73904cb2014-12-04 09:42:08 +053011 *
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053012 */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053013#include <linux/module.h>
14#include <linux/thermal.h>
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053015#include <linux/cpufreq.h>
16#include <linux/err.h>
Matthew Wilcoxae606082016-12-21 09:47:05 -080017#include <linux/idr.h>
Javi Merinoc36cf072015-02-26 19:00:29 +000018#include <linux/pm_opp.h>
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053019#include <linux/slab.h>
20#include <linux/cpu.h>
21#include <linux/cpu_cooling.h>
22
Javi Merino6828a472015-03-02 17:17:20 +000023#include <trace/events/thermal.h>
24
Viresh Kumar07d888d2014-12-04 09:41:49 +053025/*
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 Kachhap02361412012-08-16 17:11:40 +053039/**
Viresh Kumar349d39d2017-04-25 15:57:19 +053040 * struct freq_table - frequency table along with power entries
Javi Merinoc36cf072015-02-26 19:00:29 +000041 * @frequency: frequency in KHz
42 * @power: power in mW
43 *
44 * This structure is built when the cooling device registers and helps
Viresh Kumar349d39d2017-04-25 15:57:19 +053045 * in translating frequency to power and vice versa.
Javi Merinoc36cf072015-02-26 19:00:29 +000046 */
Viresh Kumar349d39d2017-04-25 15:57:19 +053047struct freq_table {
Javi Merinoc36cf072015-02-26 19:00:29 +000048 u32 frequency;
49 u32 power;
50};
51
52/**
Viresh Kumar81ee14d2017-04-25 15:57:20 +053053 * 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 */
57struct time_in_idle {
58 u64 time;
59 u64 timestamp;
60};
61
62/**
Eduardo Valentin3b3c0742013-04-17 17:11:56 +000063 * struct cpufreq_cooling_device - data for cooling device with cpufreq
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053064 * @id: unique integer value corresponding to each cpufreq_cooling_device
65 * registered.
Viresh Kumard72b4012017-04-25 15:57:24 +053066 * @last_load: load measured by the latest call to cpufreq_get_requested_power()
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053067 * @cpufreq_state: integer value representing the current state of cpufreq
68 * cooling devices.
Viresh Kumar59f0d212015-07-30 12:40:33 +053069 * @clipped_freq: integer value representing the absolute value of the clipped
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053070 * frequency.
Viresh Kumardcc6c7f2014-12-04 09:42:02 +053071 * @max_level: maximum cooling level. One less than total number of valid
72 * cpufreq frequencies.
Viresh Kumard72b4012017-04-25 15:57:24 +053073 * @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 Merinofc4de352014-12-15 16:55:52 +000077 * @node: list_head to link all cpufreq_cooling_device together.
Viresh Kumar81ee14d2017-04-25 15:57:20 +053078 * @idle_time: idle time stats
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053079 *
Viresh Kumarbeca6052014-12-04 09:41:48 +053080 * This structure is required for keeping information of each registered
81 * cpufreq_cooling_device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053082 */
83struct cpufreq_cooling_device {
84 int id;
Viresh Kumard72b4012017-04-25 15:57:24 +053085 u32 last_load;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053086 unsigned int cpufreq_state;
Viresh Kumar59f0d212015-07-30 12:40:33 +053087 unsigned int clipped_freq;
Viresh Kumardcc6c7f2014-12-04 09:42:02 +053088 unsigned int max_level;
Viresh Kumar349d39d2017-04-25 15:57:19 +053089 struct freq_table *freq_table; /* In descending order */
Viresh Kumard72b4012017-04-25 15:57:24 +053090 struct cpufreq_policy *policy;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +053091 struct list_head node;
Viresh Kumar81ee14d2017-04-25 15:57:20 +053092 struct time_in_idle *idle_time;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053093};
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053094
Viresh Kumarfb8ea302017-04-25 15:57:09 +053095static DEFINE_IDA(cpufreq_ida);
Russell King02373d72015-08-12 15:22:16 +053096static DEFINE_MUTEX(cooling_list_lock);
Viresh Kumar1dea4322017-04-25 15:57:10 +053097static LIST_HEAD(cpufreq_cdev_list);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053098
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053099/* Below code defines functions to be used for cpufreq as cooling device */
100
101/**
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530102 * get_level: Find the level for a particular frequency
Viresh Kumar1dea4322017-04-25 15:57:10 +0530103 * @cpufreq_cdev: cpufreq_cdev for which the property is required
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530104 * @freq: Frequency
Eduardo Valentin82b9ee42013-04-17 17:12:00 +0000105 *
Viresh Kumarda27f692017-04-25 15:57:21 +0530106 * Return: level corresponding to the frequency.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530107 */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530108static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev,
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530109 unsigned int freq)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530110{
Viresh Kumarda27f692017-04-25 15:57:21 +0530111 struct freq_table *freq_table = cpufreq_cdev->freq_table;
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530112 unsigned long level;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000113
Viresh Kumarda27f692017-04-25 15:57:21 +0530114 for (level = 1; level <= cpufreq_cdev->max_level; level++)
115 if (freq > freq_table[level].frequency)
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530116 break;
Zhang Ruia1167762014-01-02 11:57:48 +0800117
Viresh Kumarda27f692017-04-25 15:57:21 +0530118 return level - 1;
Zhang Ruifc35b352013-02-08 13:09:32 +0800119}
120
Eduardo Valentin44952d32013-04-17 17:12:05 +0000121/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530122 * 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 Valentinbab30552013-04-17 17:12:09 +0000126 *
Javi Merino9746b6e2014-06-25 18:11:17 +0100127 * Callback to hijack the notification on cpufreq policy transition.
Eduardo Valentinbab30552013-04-17 17:12:09 +0000128 * 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 Kachhap02361412012-08-16 17:11:40 +0530132 */
133static int cpufreq_thermal_notifier(struct notifier_block *nb,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000134 unsigned long event, void *data)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530135{
136 struct cpufreq_policy *policy = data;
Viresh Kumarabcbcc22015-07-30 12:40:34 +0530137 unsigned long clipped_freq;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530138 struct cpufreq_cooling_device *cpufreq_cdev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530139
Viresh Kumara24af232015-07-30 12:40:32 +0530140 if (event != CPUFREQ_ADJUST)
Javi Merinoc36cf072015-02-26 19:00:29 +0000141 return NOTIFY_DONE;
Viresh Kumara24af232015-07-30 12:40:32 +0530142
143 mutex_lock(&cooling_list_lock);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530144 list_for_each_entry(cpufreq_cdev, &cpufreq_cdev_list, node) {
Viresh Kumarba76dd92017-04-25 15:57:18 +0530145 /*
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 Kumara24af232015-07-30 12:40:32 +0530150 continue;
151
Viresh Kumar1afb9c52015-07-30 12:40:35 +0530152 /*
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 Kumar1dea4322017-04-25 15:57:10 +0530163 clipped_freq = cpufreq_cdev->clipped_freq;
Viresh Kumara24af232015-07-30 12:40:32 +0530164
Viresh Kumar1afb9c52015-07-30 12:40:35 +0530165 if (policy->max > clipped_freq)
Viresh Kumarabcbcc22015-07-30 12:40:34 +0530166 cpufreq_verify_within_limits(policy, 0, clipped_freq);
Viresh Kumara24af232015-07-30 12:40:32 +0530167 break;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530168 }
Viresh Kumara24af232015-07-30 12:40:32 +0530169 mutex_unlock(&cooling_list_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530170
Javi Merinoc36cf072015-02-26 19:00:29 +0000171 return NOTIFY_OK;
172}
173
174/**
Viresh Kumar349d39d2017-04-25 15:57:19 +0530175 * update_freq_table() - Update the freq table with power numbers
176 * @cpufreq_cdev: the cpufreq cooling device in which to update the table
Javi Merinoc36cf072015-02-26 19:00:29 +0000177 * @capacitance: dynamic power coefficient for these cpus
178 *
Viresh Kumar349d39d2017-04-25 15:57:19 +0530179 * 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 Merinoc36cf072015-02-26 19:00:29 +0000183 *
Javi Merino459ac372015-08-17 19:21:42 +0100184 * Return: 0 on success, -EINVAL if there are no OPPs for any CPUs,
Viresh Kumar349d39d2017-04-25 15:57:19 +0530185 * or -ENOMEM if we run out of memory.
Javi Merinoc36cf072015-02-26 19:00:29 +0000186 */
Viresh Kumar349d39d2017-04-25 15:57:19 +0530187static int update_freq_table(struct cpufreq_cooling_device *cpufreq_cdev,
188 u32 capacitance)
Javi Merinoc36cf072015-02-26 19:00:29 +0000189{
Viresh Kumar349d39d2017-04-25 15:57:19 +0530190 struct freq_table *freq_table = cpufreq_cdev->freq_table;
Javi Merinoc36cf072015-02-26 19:00:29 +0000191 struct dev_pm_opp *opp;
192 struct device *dev = NULL;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530193 int num_opps = 0, cpu = cpufreq_cdev->policy->cpu, i;
Javi Merinoc36cf072015-02-26 19:00:29 +0000194
Viresh Kumar02bacb22017-04-25 15:57:17 +0530195 dev = get_cpu_device(cpu);
196 if (unlikely(!dev)) {
Daniel Lezcano72554a72019-04-28 11:51:05 +0200197 pr_warn("No cpu device for cpu %d\n", cpu);
Viresh Kumar02bacb22017-04-25 15:57:17 +0530198 return -ENODEV;
Javi Merinoc36cf072015-02-26 19:00:29 +0000199 }
200
Viresh Kumar02bacb22017-04-25 15:57:17 +0530201 num_opps = dev_pm_opp_get_opp_count(dev);
202 if (num_opps < 0)
203 return num_opps;
204
Viresh Kumar349d39d2017-04-25 15:57:19 +0530205 /*
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 Merino459ac372015-08-17 19:21:42 +0100211 return -EINVAL;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530212 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000213
Viresh Kumar349d39d2017-04-25 15:57:19 +0530214 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 Merinoc36cf072015-02-26 19:00:29 +0000217 u64 power;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530218 u32 voltage_mv;
Javi Merinoc36cf072015-02-26 19:00:29 +0000219
Viresh Kumar349d39d2017-04-25 15:57:19 +0530220 /*
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 Merino459ac372015-08-17 19:21:42 +0100229 }
230
Javi Merinoc36cf072015-02-26 19:00:29 +0000231 voltage_mv = dev_pm_opp_get_voltage(opp) / 1000;
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530232 dev_pm_opp_put(opp);
Javi Merinoc36cf072015-02-26 19:00:29 +0000233
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 Merinoc36cf072015-02-26 19:00:29 +0000241 /* power is stored in mW */
Viresh Kumar349d39d2017-04-25 15:57:19 +0530242 freq_table[i].power = power;
Javi Merinoeba4f882015-08-17 19:21:43 +0100243 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000244
Javi Merino459ac372015-08-17 19:21:42 +0100245 return 0;
Javi Merinoc36cf072015-02-26 19:00:29 +0000246}
247
Viresh Kumar1dea4322017-04-25 15:57:10 +0530248static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_cdev,
Javi Merinoc36cf072015-02-26 19:00:29 +0000249 u32 freq)
250{
251 int i;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530252 struct freq_table *freq_table = cpufreq_cdev->freq_table;
Javi Merinoc36cf072015-02-26 19:00:29 +0000253
Viresh Kumar349d39d2017-04-25 15:57:19 +0530254 for (i = 1; i <= cpufreq_cdev->max_level; i++)
255 if (freq > freq_table[i].frequency)
Javi Merinoc36cf072015-02-26 19:00:29 +0000256 break;
257
Viresh Kumar349d39d2017-04-25 15:57:19 +0530258 return freq_table[i - 1].power;
Javi Merinoc36cf072015-02-26 19:00:29 +0000259}
260
Viresh Kumar1dea4322017-04-25 15:57:10 +0530261static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_cdev,
Javi Merinoc36cf072015-02-26 19:00:29 +0000262 u32 power)
263{
264 int i;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530265 struct freq_table *freq_table = cpufreq_cdev->freq_table;
Javi Merinoc36cf072015-02-26 19:00:29 +0000266
Viresh Kumar349d39d2017-04-25 15:57:19 +0530267 for (i = 1; i <= cpufreq_cdev->max_level; i++)
268 if (power > freq_table[i].power)
Javi Merinoc36cf072015-02-26 19:00:29 +0000269 break;
270
Viresh Kumar349d39d2017-04-25 15:57:19 +0530271 return freq_table[i - 1].frequency;
Javi Merinoc36cf072015-02-26 19:00:29 +0000272}
273
274/**
275 * get_load() - get load for a cpu since last updated
Viresh Kumar1dea4322017-04-25 15:57:10 +0530276 * @cpufreq_cdev: &struct cpufreq_cooling_device for this cpu
Javi Merinoc36cf072015-02-26 19:00:29 +0000277 * @cpu: cpu number
Viresh Kumarba76dd92017-04-25 15:57:18 +0530278 * @cpu_idx: index of the cpu in time_in_idle*
Javi Merinoc36cf072015-02-26 19:00:29 +0000279 *
280 * Return: The average load of cpu @cpu in percentage since this
281 * function was last called.
282 */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530283static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu,
Javi Merinoa53b8392016-02-11 12:00:51 +0000284 int cpu_idx)
Javi Merinoc36cf072015-02-26 19:00:29 +0000285{
286 u32 load;
287 u64 now, now_idle, delta_time, delta_idle;
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530288 struct time_in_idle *idle_time = &cpufreq_cdev->idle_time[cpu_idx];
Javi Merinoc36cf072015-02-26 19:00:29 +0000289
290 now_idle = get_cpu_idle_time(cpu, &now, 0);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530291 delta_idle = now_idle - idle_time->time;
292 delta_time = now - idle_time->timestamp;
Javi Merinoc36cf072015-02-26 19:00:29 +0000293
294 if (delta_time <= delta_idle)
295 load = 0;
296 else
297 load = div64_u64(100 * (delta_time - delta_idle), delta_time);
298
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530299 idle_time->time = now_idle;
300 idle_time->timestamp = now;
Javi Merinoc36cf072015-02-26 19:00:29 +0000301
302 return load;
303}
304
305/**
Javi Merinoc36cf072015-02-26 19:00:29 +0000306 * get_dynamic_power() - calculate the dynamic power
Viresh Kumar1dea4322017-04-25 15:57:10 +0530307 * @cpufreq_cdev: &cpufreq_cooling_device for this cdev
Javi Merinoc36cf072015-02-26 19:00:29 +0000308 * @freq: current frequency
309 *
310 * Return: the dynamic power consumed by the cpus described by
Viresh Kumar1dea4322017-04-25 15:57:10 +0530311 * @cpufreq_cdev.
Javi Merinoc36cf072015-02-26 19:00:29 +0000312 */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530313static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_cdev,
Javi Merinoc36cf072015-02-26 19:00:29 +0000314 unsigned long freq)
315{
316 u32 raw_cpu_power;
317
Viresh Kumar1dea4322017-04-25 15:57:10 +0530318 raw_cpu_power = cpu_freq_to_power(cpufreq_cdev, freq);
319 return (raw_cpu_power * cpufreq_cdev->last_load) / 100;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530320}
321
Eduardo Valentin1b9e3522013-04-17 17:12:02 +0000322/* cpufreq cooling device callback functions are defined below */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530323
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 Valentin62c00422013-04-17 17:12:12 +0000328 *
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 Kachhap02361412012-08-16 17:11:40 +0530333 */
334static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
335 unsigned long *state)
336{
Viresh Kumar1dea4322017-04-25 15:57:10 +0530337 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530338
Viresh Kumar1dea4322017-04-25 15:57:10 +0530339 *state = cpufreq_cdev->max_level;
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530340 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530341}
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 Valentin36725522013-04-17 17:12:13 +0000347 *
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 Kachhap02361412012-08-16 17:11:40 +0530352 */
353static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
354 unsigned long *state)
355{
Viresh Kumar1dea4322017-04-25 15:57:10 +0530356 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530357
Viresh Kumar1dea4322017-04-25 15:57:10 +0530358 *state = cpufreq_cdev->cpufreq_state;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000359
hongbo.zhang160b7d82012-10-30 17:48:59 +0100360 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530361}
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 Valentin56e05fd2013-04-17 17:12:14 +0000367 *
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 Kachhap02361412012-08-16 17:11:40 +0530372 */
373static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
374 unsigned long state)
375{
Viresh Kumar1dea4322017-04-25 15:57:10 +0530376 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530377 unsigned int clip_freq;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530378
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530379 /* Request state should be less than max_level */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530380 if (WARN_ON(state > cpufreq_cdev->max_level))
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530381 return -EINVAL;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530382
Viresh Kumar5194fe42014-12-04 09:42:00 +0530383 /* Check if the old cooling action is same as new cooling action */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530384 if (cpufreq_cdev->cpufreq_state == state)
Viresh Kumar5194fe42014-12-04 09:42:00 +0530385 return 0;
386
Viresh Kumar349d39d2017-04-25 15:57:19 +0530387 clip_freq = cpufreq_cdev->freq_table[state].frequency;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530388 cpufreq_cdev->cpufreq_state = state;
389 cpufreq_cdev->clipped_freq = clip_freq;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530390
Viresh Kumarba76dd92017-04-25 15:57:18 +0530391 cpufreq_update_policy(cpufreq_cdev->policy->cpu);
Viresh Kumar5194fe42014-12-04 09:42:00 +0530392
393 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530394}
395
Javi Merinoc36cf072015-02-26 19:00:29 +0000396/**
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 */
419static 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 Kumar84fe2ca2017-12-05 11:02:46 +0530424 int i = 0, cpu;
425 u32 total_load = 0;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530426 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Viresh Kumarba76dd92017-04-25 15:57:18 +0530427 struct cpufreq_policy *policy = cpufreq_cdev->policy;
Javi Merino6828a472015-03-02 17:17:20 +0000428 u32 *load_cpu = NULL;
Javi Merinoc36cf072015-02-26 19:00:29 +0000429
Viresh Kumarba76dd92017-04-25 15:57:18 +0530430 freq = cpufreq_quick_get(policy->cpu);
Javi Merinoc36cf072015-02-26 19:00:29 +0000431
Javi Merino6828a472015-03-02 17:17:20 +0000432 if (trace_thermal_power_cpu_get_power_enabled()) {
Viresh Kumarba76dd92017-04-25 15:57:18 +0530433 u32 ncpus = cpumask_weight(policy->related_cpus);
Javi Merino6828a472015-03-02 17:17:20 +0000434
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530435 load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL);
Javi Merino6828a472015-03-02 17:17:20 +0000436 }
437
Viresh Kumarba76dd92017-04-25 15:57:18 +0530438 for_each_cpu(cpu, policy->related_cpus) {
Javi Merinoc36cf072015-02-26 19:00:29 +0000439 u32 load;
440
441 if (cpu_online(cpu))
Viresh Kumar1dea4322017-04-25 15:57:10 +0530442 load = get_load(cpufreq_cdev, cpu, i);
Javi Merinoc36cf072015-02-26 19:00:29 +0000443 else
444 load = 0;
445
446 total_load += load;
Matthias Kaehlckebf45ac12019-05-02 11:32:38 -0700447 if (load_cpu)
Javi Merino6828a472015-03-02 17:17:20 +0000448 load_cpu[i] = load;
449
450 i++;
Javi Merinoc36cf072015-02-26 19:00:29 +0000451 }
452
Viresh Kumar1dea4322017-04-25 15:57:10 +0530453 cpufreq_cdev->last_load = total_load;
Javi Merinoc36cf072015-02-26 19:00:29 +0000454
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530455 *power = get_dynamic_power(cpufreq_cdev, freq);
Javi Merino6828a472015-03-02 17:17:20 +0000456
457 if (load_cpu) {
Viresh Kumarba76dd92017-04-25 15:57:18 +0530458 trace_thermal_power_cpu_get_power(policy->related_cpus, freq,
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530459 load_cpu, i, *power);
Javi Merino6828a472015-03-02 17:17:20 +0000460
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530461 kfree(load_cpu);
Javi Merino6828a472015-03-02 17:17:20 +0000462 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000463
Javi Merinoc36cf072015-02-26 19:00:29 +0000464 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 */
482static 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 Kumar1dea4322017-04-25 15:57:10 +0530487 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Javi Merinoc36cf072015-02-26 19:00:29 +0000488
Viresh Kumarcb1b6312017-04-25 15:57:23 +0530489 /* Request state should be less than max_level */
490 if (WARN_ON(state > cpufreq_cdev->max_level))
491 return -EINVAL;
492
Viresh Kumarba76dd92017-04-25 15:57:18 +0530493 num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus);
Javi Merinoc36cf072015-02-26 19:00:29 +0000494
Viresh Kumar349d39d2017-04-25 15:57:19 +0530495 freq = cpufreq_cdev->freq_table[state].frequency;
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530496 *power = cpu_freq_to_power(cpufreq_cdev, freq) * num_cpus;
Javi Merinoc36cf072015-02-26 19:00:29 +0000497
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530498 return 0;
Javi Merinoc36cf072015-02-26 19:00:29 +0000499}
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 */
521static int cpufreq_power2state(struct thermal_cooling_device *cdev,
522 struct thermal_zone_device *tz, u32 power,
523 unsigned long *state)
524{
Shaokun Zhange0fda732019-02-18 14:22:30 +0800525 unsigned int target_freq;
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530526 u32 last_load, normalised_power;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530527 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Viresh Kumarba76dd92017-04-25 15:57:18 +0530528 struct cpufreq_policy *policy = cpufreq_cdev->policy;
Javi Merinoc36cf072015-02-26 19:00:29 +0000529
Viresh Kumar1dea4322017-04-25 15:57:10 +0530530 last_load = cpufreq_cdev->last_load ?: 1;
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530531 normalised_power = (power * 100) / last_load;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530532 target_freq = cpu_power_to_freq(cpufreq_cdev, normalised_power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000533
Viresh Kumar3e08b2d2017-04-25 15:57:12 +0530534 *state = get_level(cpufreq_cdev, target_freq);
Viresh Kumarba76dd92017-04-25 15:57:18 +0530535 trace_thermal_power_cpu_limit(policy->related_cpus, target_freq, *state,
536 power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000537 return 0;
538}
539
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530540/* Bind cpufreq callbacks to thermal cooling device ops */
Brendan Jackmana305a432016-08-17 16:14:59 +0100541
Javi Merinoc36cf072015-02-26 19:00:29 +0000542static struct thermal_cooling_device_ops cpufreq_cooling_ops = {
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530543 .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 Jackmana305a432016-08-17 16:14:59 +0100548static 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 Kachhap02361412012-08-16 17:11:40 +0530557/* Notifier for cpufreq policy change */
558static struct notifier_block thermal_cpufreq_notifier_block = {
559 .notifier_call = cpufreq_thermal_notifier,
560};
561
Viresh Kumarf6859012014-12-04 09:42:06 +0530562static 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 Kachhap02361412012-08-16 17:11:40 +0530576/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400577 * __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 Kumar4d753aa2017-04-25 15:57:14 +0530579 * @policy: cpufreq policy
Viresh Kumar405fb822014-12-04 09:41:55 +0530580 * Normally this should be same as cpufreq policy->related_cpus.
Javi Merinoc36cf072015-02-26 19:00:29 +0000581 * @capacitance: dynamic power coefficient for these cpus
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000582 *
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 Valentin39d99cf2013-09-12 19:26:45 -0400585 * 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 Valentin12cb08b2013-04-17 17:12:15 +0000587 *
588 * Return: a valid struct thermal_cooling_device pointer on success,
589 * on failure, it returns a corresponding ERR_PTR().
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530590 */
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400591static struct thermal_cooling_device *
592__cpufreq_cooling_register(struct device_node *np,
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530593 struct cpufreq_policy *policy, u32 capacitance)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530594{
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530595 struct thermal_cooling_device *cdev;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530596 struct cpufreq_cooling_device *cpufreq_cdev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530597 char dev_name[THERMAL_NAME_LENGTH];
Javi Merinoc36cf072015-02-26 19:00:29 +0000598 unsigned int freq, i, num_cpus;
Viresh Kumar405fb822014-12-04 09:41:55 +0530599 int ret;
Brendan Jackmana305a432016-08-17 16:14:59 +0100600 struct thermal_cooling_device_ops *cooling_ops;
Matthew Wilcox088db932017-03-10 18:33:28 +0000601 bool first;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530602
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530603 if (IS_ERR_OR_NULL(policy)) {
Arvind Yadavb2fd7082017-10-24 13:20:39 +0530604 pr_err("%s: cpufreq policy isn't valid: %p\n", __func__, policy);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530605 return ERR_PTR(-EINVAL);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530606 }
Eduardo Valentin0f1be512014-12-04 09:41:43 +0530607
Viresh Kumar55d85292017-04-25 15:57:15 +0530608 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 Kumar4d753aa2017-04-25 15:57:14 +0530612 return ERR_PTR(-ENODEV);
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530613 }
614
Viresh Kumar1dea4322017-04-25 15:57:10 +0530615 cpufreq_cdev = kzalloc(sizeof(*cpufreq_cdev), GFP_KERNEL);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530616 if (!cpufreq_cdev)
617 return ERR_PTR(-ENOMEM);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530618
Viresh Kumarb12b6512017-04-25 15:57:16 +0530619 cpufreq_cdev->policy = policy;
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530620 num_cpus = cpumask_weight(policy->related_cpus);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530621 cpufreq_cdev->idle_time = kcalloc(num_cpus,
622 sizeof(*cpufreq_cdev->idle_time),
623 GFP_KERNEL);
624 if (!cpufreq_cdev->idle_time) {
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530625 cdev = ERR_PTR(-ENOMEM);
Javi Merinoc36cf072015-02-26 19:00:29 +0000626 goto free_cdev;
627 }
628
Viresh Kumar55d85292017-04-25 15:57:15 +0530629 /* max_level is an index, not a counter */
630 cpufreq_cdev->max_level = i - 1;
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530631
Viresh Kumarf19b1a12017-05-23 12:33:06 +0530632 cpufreq_cdev->freq_table = kmalloc_array(i,
633 sizeof(*cpufreq_cdev->freq_table),
634 GFP_KERNEL);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530635 if (!cpufreq_cdev->freq_table) {
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530636 cdev = ERR_PTR(-ENOMEM);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530637 goto free_idle_time;
Viresh Kumarf6859012014-12-04 09:42:06 +0530638 }
639
Matthew Wilcoxae606082016-12-21 09:47:05 -0800640 ret = ida_simple_get(&cpufreq_ida, 0, 0, GFP_KERNEL);
641 if (ret < 0) {
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530642 cdev = ERR_PTR(ret);
Viresh Kumar349d39d2017-04-25 15:57:19 +0530643 goto free_table;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530644 }
Viresh Kumar1dea4322017-04-25 15:57:10 +0530645 cpufreq_cdev->id = ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530646
Viresh Kumar349d39d2017-04-25 15:57:19 +0530647 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
648 cpufreq_cdev->id);
649
Viresh Kumarf6859012014-12-04 09:42:06 +0530650 /* Fill freq-table in descending order of frequencies */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530651 for (i = 0, freq = -1; i <= cpufreq_cdev->max_level; i++) {
Viresh Kumar55d85292017-04-25 15:57:15 +0530652 freq = find_next_max(policy->freq_table, freq);
Viresh Kumar349d39d2017-04-25 15:57:19 +0530653 cpufreq_cdev->freq_table[i].frequency = freq;
Viresh Kumarf6859012014-12-04 09:42:06 +0530654
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 Kachhap02361412012-08-16 17:11:40 +0530660 }
Viresh Kumarf6859012014-12-04 09:42:06 +0530661
Viresh Kumar349d39d2017-04-25 15:57:19 +0530662 if (capacitance) {
Viresh Kumar349d39d2017-04-25 15:57:19 +0530663 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 Lubaf840ab12016-05-31 11:32:02 +0100673
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530674 cdev = thermal_of_cooling_device_register(np, dev_name, cpufreq_cdev,
675 cooling_ops);
676 if (IS_ERR(cdev))
Matthew Wilcoxae606082016-12-21 09:47:05 -0800677 goto remove_ida;
Lukasz Lubaf840ab12016-05-31 11:32:02 +0100678
Viresh Kumar349d39d2017-04-25 15:57:19 +0530679 cpufreq_cdev->clipped_freq = cpufreq_cdev->freq_table[0].frequency;
Viresh Kumar92e615e2014-12-04 09:41:51 +0530680
Russell King02373d72015-08-12 15:22:16 +0530681 mutex_lock(&cooling_list_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530682 /* Register the notifier for first cpufreq cooling device */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530683 first = list_empty(&cpufreq_cdev_list);
684 list_add(&cpufreq_cdev->node, &cpufreq_cdev_list);
Matthew Wilcox088db932017-03-10 18:33:28 +0000685 mutex_unlock(&cooling_list_lock);
686
687 if (first)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530688 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000689 CPUFREQ_POLICY_NOTIFIER);
Eduardo Valentin79491e52013-04-17 17:11:59 +0000690
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530691 return cdev;
Viresh Kumar730abe02014-12-04 09:41:58 +0530692
Matthew Wilcoxae606082016-12-21 09:47:05 -0800693remove_ida:
Viresh Kumar1dea4322017-04-25 15:57:10 +0530694 ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id);
Viresh Kumarf6859012014-12-04 09:42:06 +0530695free_table:
Viresh Kumar1dea4322017-04-25 15:57:10 +0530696 kfree(cpufreq_cdev->freq_table);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530697free_idle_time:
698 kfree(cpufreq_cdev->idle_time);
Viresh Kumar730abe02014-12-04 09:41:58 +0530699free_cdev:
Viresh Kumar1dea4322017-04-25 15:57:10 +0530700 kfree(cpufreq_cdev);
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530701 return cdev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530702}
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400703
704/**
705 * cpufreq_cooling_register - function to create cpufreq cooling device.
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530706 * @policy: cpufreq policy
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400707 *
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 */
715struct thermal_cooling_device *
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530716cpufreq_cooling_register(struct cpufreq_policy *policy)
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400717{
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530718 return __cpufreq_cooling_register(NULL, policy, 0);
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400719}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000720EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530721
722/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400723 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530724 * @policy: cpufreq policy
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400725 *
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 Merinoc36cf072015-02-26 19:00:29 +0000731 * 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 Kumarf5f263f2017-12-05 11:02:43 +0530735 * It also takes into account, if property present in policy CPU node, the
736 * static power consumed by the cpu.
Javi Merinoc36cf072015-02-26 19:00:29 +0000737 *
738 * Return: a valid struct thermal_cooling_device pointer on success,
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530739 * and NULL on failure.
Javi Merinoc36cf072015-02-26 19:00:29 +0000740 */
741struct thermal_cooling_device *
Viresh Kumar3ebb62f2017-12-05 11:02:45 +0530742of_cpufreq_cooling_register(struct cpufreq_policy *policy)
Javi Merinoc36cf072015-02-26 19:00:29 +0000743{
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530744 struct device_node *np = of_get_cpu_node(policy->cpu, NULL);
745 struct thermal_cooling_device *cdev = NULL;
746 u32 capacitance = 0;
Javi Merinoc36cf072015-02-26 19:00:29 +0000747
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530748 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 Kumar84fe2ca2017-12-05 11:02:46 +0530758 cdev = __cpufreq_cooling_register(np, policy, capacitance);
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530759 if (IS_ERR(cdev)) {
Amit Kucheriabf78f132019-01-21 15:12:23 +0530760 pr_err("cpu_cooling: cpu%d failed to register as cooling device: %ld\n",
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530761 policy->cpu, PTR_ERR(cdev));
762 cdev = NULL;
763 }
764 }
765
766 of_node_put(np);
767 return cdev;
Javi Merinoc36cf072015-02-26 19:00:29 +0000768}
Viresh Kumar3ebb62f2017-12-05 11:02:45 +0530769EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
Javi Merinoc36cf072015-02-26 19:00:29 +0000770
771/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530772 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
773 * @cdev: thermal cooling device pointer.
Eduardo Valentin135266b2013-04-17 17:12:16 +0000774 *
775 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530776 */
777void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
778{
Viresh Kumar1dea4322017-04-25 15:57:10 +0530779 struct cpufreq_cooling_device *cpufreq_cdev;
Matthew Wilcox088db932017-03-10 18:33:28 +0000780 bool last;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530781
Eduardo Valentin50e66c72013-08-15 10:54:46 -0400782 if (!cdev)
783 return;
784
Viresh Kumar1dea4322017-04-25 15:57:10 +0530785 cpufreq_cdev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530786
Matthew Wilcoxae606082016-12-21 09:47:05 -0800787 mutex_lock(&cooling_list_lock);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530788 list_del(&cpufreq_cdev->node);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530789 /* Unregister the notifier for the last cpufreq cooling device */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530790 last = list_empty(&cpufreq_cdev_list);
Matthew Wilcox088db932017-03-10 18:33:28 +0000791 mutex_unlock(&cooling_list_lock);
792
793 if (last)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530794 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000795 CPUFREQ_POLICY_NOTIFIER);
Russell King02373d72015-08-12 15:22:16 +0530796
Daniel Lezcano72554a72019-04-28 11:51:05 +0200797 thermal_cooling_device_unregister(cdev);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530798 ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530799 kfree(cpufreq_cdev->idle_time);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530800 kfree(cpufreq_cdev->freq_table);
801 kfree(cpufreq_cdev);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530802}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000803EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);