Merge branch 'resizex' (patches from Maciej)
[linux-block.git] / drivers / thermal / devfreq_cooling.c
CommitLineData
0de967f2 1// SPDX-License-Identifier: GPL-2.0
a76caf55
ØE
2/*
3 * devfreq_cooling: Thermal cooling device implementation for devices using
4 * devfreq
5 *
6 * Copyright (C) 2014-2015 ARM Limited
7 *
a76caf55
ØE
8 * TODO:
9 * - If OPPs are added or removed after devfreq cooling has
10 * registered, the devfreq cooling won't react to it.
11 */
12
13#include <linux/devfreq.h>
14#include <linux/devfreq_cooling.h>
84e0d87c 15#include <linux/energy_model.h>
a76caf55
ØE
16#include <linux/export.h>
17#include <linux/slab.h>
18#include <linux/pm_opp.h>
04fa9c80 19#include <linux/pm_qos.h>
a76caf55
ØE
20#include <linux/thermal.h>
21
9876b1a4
JM
22#include <trace/events/thermal.h>
23
04fa9c80
MK
24#define HZ_PER_KHZ 1000
25#define SCALE_ERROR_MITIGATION 100
2be83da8 26
a76caf55
ØE
27/**
28 * struct devfreq_cooling_device - Devfreq cooling device
a76caf55
ØE
29 * devfreq_cooling_device registered.
30 * @cdev: Pointer to associated thermal cooling device.
31 * @devfreq: Pointer to associated devfreq device.
32 * @cooling_state: Current cooling state.
a76caf55
ØE
33 * @freq_table: Pointer to a table with the frequencies sorted in descending
34 * order. You can index the table by cooling device state
615510fe
LL
35 * @max_state: It is the last index, that is, one less than the number of the
36 * OPPs
37 * @power_ops: Pointer to devfreq_cooling_power, a more precised model.
2be83da8
LL
38 * @res_util: Resource utilization scaling factor for the power.
39 * It is multiplied by 100 to minimize the error. It is used
40 * for estimation of the power budget instead of using
615510fe
LL
41 * 'utilization' (which is 'busy_time' / 'total_time').
42 * The 'res_util' range is from 100 to power * 100 for the
43 * corresponding 'state'.
1b5cb957 44 * @capped_state: index to cooling state with in dynamic power budget
04fa9c80
MK
45 * @req_max_freq: PM QoS request for limiting the maximum frequency
46 * of the devfreq device.
4401117b 47 * @em_pd: Energy Model for the associated Devfreq device
a76caf55
ØE
48 */
49struct devfreq_cooling_device {
a76caf55
ØE
50 struct thermal_cooling_device *cdev;
51 struct devfreq *devfreq;
52 unsigned long cooling_state;
a76caf55 53 u32 *freq_table;
615510fe 54 size_t max_state;
a76caf55 55 struct devfreq_cooling_power *power_ops;
2be83da8
LL
56 u32 res_util;
57 int capped_state;
04fa9c80 58 struct dev_pm_qos_request req_max_freq;
4401117b 59 struct em_perf_domain *em_pd;
a76caf55
ØE
60};
61
a76caf55
ØE
62static int devfreq_cooling_get_max_state(struct thermal_cooling_device *cdev,
63 unsigned long *state)
64{
65 struct devfreq_cooling_device *dfc = cdev->devdata;
66
615510fe 67 *state = dfc->max_state;
a76caf55
ØE
68
69 return 0;
70}
71
72static int devfreq_cooling_get_cur_state(struct thermal_cooling_device *cdev,
73 unsigned long *state)
74{
75 struct devfreq_cooling_device *dfc = cdev->devdata;
76
77 *state = dfc->cooling_state;
78
79 return 0;
80}
81
82static int devfreq_cooling_set_cur_state(struct thermal_cooling_device *cdev,
83 unsigned long state)
84{
85 struct devfreq_cooling_device *dfc = cdev->devdata;
86 struct devfreq *df = dfc->devfreq;
87 struct device *dev = df->dev.parent;
04fa9c80 88 unsigned long freq;
615510fe 89 int perf_idx;
a76caf55
ØE
90
91 if (state == dfc->cooling_state)
92 return 0;
93
94 dev_dbg(dev, "Setting cooling state %lu\n", state);
95
615510fe 96 if (state > dfc->max_state)
a76caf55
ØE
97 return -EINVAL;
98
4401117b 99 if (dfc->em_pd) {
615510fe 100 perf_idx = dfc->max_state - state;
4401117b 101 freq = dfc->em_pd->table[perf_idx].frequency * 1000;
615510fe
LL
102 } else {
103 freq = dfc->freq_table[state];
104 }
04fa9c80
MK
105
106 dev_pm_qos_update_request(&dfc->req_max_freq,
107 DIV_ROUND_UP(freq, HZ_PER_KHZ));
a76caf55
ØE
108
109 dfc->cooling_state = state;
110
111 return 0;
112}
113
114/**
615510fe
LL
115 * get_perf_idx() - get the performance index corresponding to a frequency
116 * @em_pd: Pointer to device's Energy Model
117 * @freq: frequency in kHz
a76caf55 118 *
615510fe
LL
119 * Return: the performance index associated with the @freq, or
120 * -EINVAL if it wasn't found.
a76caf55 121 */
615510fe 122static int get_perf_idx(struct em_perf_domain *em_pd, unsigned long freq)
a76caf55
ØE
123{
124 int i;
125
615510fe
LL
126 for (i = 0; i < em_pd->nr_perf_states; i++) {
127 if (em_pd->table[i].frequency == freq)
a76caf55
ØE
128 return i;
129 }
130
615510fe 131 return -EINVAL;
a76caf55
ØE
132}
133
e34cab4c 134static unsigned long get_voltage(struct devfreq *df, unsigned long freq)
a76caf55 135{
a76caf55
ØE
136 struct device *dev = df->dev.parent;
137 unsigned long voltage;
138 struct dev_pm_opp *opp;
139
a76caf55 140 opp = dev_pm_opp_find_freq_exact(dev, freq, true);
a4e49c9b 141 if (PTR_ERR(opp) == -ERANGE)
a76caf55
ØE
142 opp = dev_pm_opp_find_freq_exact(dev, freq, false);
143
afd1f4e0
VK
144 if (IS_ERR(opp)) {
145 dev_err_ratelimited(dev, "Failed to find OPP for frequency %lu: %ld\n",
146 freq, PTR_ERR(opp));
147 return 0;
148 }
149
a76caf55 150 voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */
8a31d9d9 151 dev_pm_opp_put(opp);
a76caf55
ØE
152
153 if (voltage == 0) {
8327b830 154 dev_err_ratelimited(dev,
afd1f4e0
VK
155 "Failed to get voltage for frequency %lu\n",
156 freq);
a76caf55
ØE
157 }
158
e34cab4c
LL
159 return voltage;
160}
161
229794ee
LL
162static void _normalize_load(struct devfreq_dev_status *status)
163{
164 if (status->total_time > 0xfffff) {
165 status->total_time >>= 10;
166 status->busy_time >>= 10;
167 }
168
169 status->busy_time <<= 10;
170 status->busy_time /= status->total_time ? : 1;
171
172 status->busy_time = status->busy_time ? : 1;
173 status->total_time = 1024;
174}
2be83da8 175
a76caf55 176static int devfreq_cooling_get_requested_power(struct thermal_cooling_device *cdev,
a76caf55
ØE
177 u32 *power)
178{
179 struct devfreq_cooling_device *dfc = cdev->devdata;
180 struct devfreq *df = dfc->devfreq;
229794ee 181 struct devfreq_dev_status status;
a76caf55 182 unsigned long state;
229794ee 183 unsigned long freq;
2be83da8 184 unsigned long voltage;
615510fe 185 int res, perf_idx;
a76caf55 186
229794ee
LL
187 mutex_lock(&df->lock);
188 status = df->last_status;
189 mutex_unlock(&df->lock);
190
191 freq = status.current_frequency;
192
615510fe 193 if (dfc->power_ops && dfc->power_ops->get_real_power) {
2be83da8
LL
194 voltage = get_voltage(df, freq);
195 if (voltage == 0) {
196 res = -EINVAL;
197 goto fail;
198 }
a76caf55 199
2be83da8
LL
200 res = dfc->power_ops->get_real_power(df, power, freq, voltage);
201 if (!res) {
202 state = dfc->capped_state;
4401117b 203 dfc->res_util = dfc->em_pd->table[state].power;
2be83da8 204 dfc->res_util *= SCALE_ERROR_MITIGATION;
a76caf55 205
2be83da8
LL
206 if (*power > 1)
207 dfc->res_util /= *power;
208 } else {
209 goto fail;
210 }
211 } else {
615510fe 212 /* Energy Model frequencies are in kHz */
4401117b 213 perf_idx = get_perf_idx(dfc->em_pd, freq / 1000);
615510fe
LL
214 if (perf_idx < 0) {
215 res = -EAGAIN;
216 goto fail;
217 }
2be83da8 218
229794ee
LL
219 _normalize_load(&status);
220
615510fe 221 /* Scale power for utilization */
4401117b 222 *power = dfc->em_pd->table[perf_idx].power;
615510fe
LL
223 *power *= status.busy_time;
224 *power >>= 10;
2be83da8 225 }
a76caf55 226
229794ee 227 trace_thermal_power_devfreq_get_power(cdev, &status, freq, *power);
9876b1a4 228
a76caf55 229 return 0;
2be83da8
LL
230fail:
231 /* It is safe to set max in this case */
232 dfc->res_util = SCALE_ERROR_MITIGATION;
233 return res;
a76caf55
ØE
234}
235
236static int devfreq_cooling_state2power(struct thermal_cooling_device *cdev,
615510fe 237 unsigned long state, u32 *power)
a76caf55
ØE
238{
239 struct devfreq_cooling_device *dfc = cdev->devdata;
615510fe 240 int perf_idx;
a76caf55 241
615510fe 242 if (state > dfc->max_state)
a76caf55
ØE
243 return -EINVAL;
244
615510fe 245 perf_idx = dfc->max_state - state;
4401117b 246 *power = dfc->em_pd->table[perf_idx].power;
a76caf55 247
a76caf55
ØE
248 return 0;
249}
250
251static int devfreq_cooling_power2state(struct thermal_cooling_device *cdev,
a76caf55
ØE
252 u32 power, unsigned long *state)
253{
254 struct devfreq_cooling_device *dfc = cdev->devdata;
255 struct devfreq *df = dfc->devfreq;
229794ee
LL
256 struct devfreq_dev_status status;
257 unsigned long freq;
2be83da8 258 s32 est_power;
a76caf55
ØE
259 int i;
260
229794ee
LL
261 mutex_lock(&df->lock);
262 status = df->last_status;
263 mutex_unlock(&df->lock);
264
265 freq = status.current_frequency;
266
615510fe 267 if (dfc->power_ops && dfc->power_ops->get_real_power) {
2be83da8
LL
268 /* Scale for resource utilization */
269 est_power = power * dfc->res_util;
270 est_power /= SCALE_ERROR_MITIGATION;
271 } else {
2be83da8 272 /* Scale dynamic power for utilization */
229794ee 273 _normalize_load(&status);
615510fe
LL
274 est_power = power << 10;
275 est_power /= status.busy_time;
2be83da8 276 }
a76caf55
ØE
277
278 /*
279 * Find the first cooling state that is within the power
615510fe 280 * budget. The EM power table is sorted ascending.
a76caf55 281 */
615510fe 282 for (i = dfc->max_state; i > 0; i--)
4401117b 283 if (est_power >= dfc->em_pd->table[i].power)
a76caf55
ØE
284 break;
285
615510fe
LL
286 *state = dfc->max_state - i;
287 dfc->capped_state = *state;
288
9876b1a4 289 trace_thermal_power_devfreq_limit(cdev, freq, *state, power);
a76caf55
ØE
290 return 0;
291}
292
293static struct thermal_cooling_device_ops devfreq_cooling_ops = {
294 .get_max_state = devfreq_cooling_get_max_state,
295 .get_cur_state = devfreq_cooling_get_cur_state,
296 .set_cur_state = devfreq_cooling_set_cur_state,
297};
298
299/**
615510fe
LL
300 * devfreq_cooling_gen_tables() - Generate frequency table.
301 * @dfc: Pointer to devfreq cooling device.
302 * @num_opps: Number of OPPs
a76caf55 303 *
615510fe
LL
304 * Generate frequency table which holds the frequencies in descending
305 * order. That way its indexed by cooling device state. This is for
306 * compatibility with drivers which do not register Energy Model.
a76caf55
ØE
307 *
308 * Return: 0 on success, negative error code on failure.
309 */
615510fe
LL
310static int devfreq_cooling_gen_tables(struct devfreq_cooling_device *dfc,
311 int num_opps)
a76caf55
ØE
312{
313 struct devfreq *df = dfc->devfreq;
314 struct device *dev = df->dev.parent;
a76caf55 315 unsigned long freq;
a76caf55
ØE
316 int i;
317
615510fe 318 dfc->freq_table = kcalloc(num_opps, sizeof(*dfc->freq_table),
a76caf55 319 GFP_KERNEL);
615510fe
LL
320 if (!dfc->freq_table)
321 return -ENOMEM;
a76caf55
ØE
322
323 for (i = 0, freq = ULONG_MAX; i < num_opps; i++, freq--) {
a76caf55
ØE
324 struct dev_pm_opp *opp;
325
a76caf55
ØE
326 opp = dev_pm_opp_find_freq_floor(dev, &freq);
327 if (IS_ERR(opp)) {
615510fe
LL
328 kfree(dfc->freq_table);
329 return PTR_ERR(opp);
a76caf55
ØE
330 }
331
8a31d9d9 332 dev_pm_opp_put(opp);
615510fe 333 dfc->freq_table[i] = freq;
a76caf55
ØE
334 }
335
a76caf55 336 return 0;
a76caf55
ØE
337}
338
339/**
340 * of_devfreq_cooling_register_power() - Register devfreq cooling device,
341 * with OF and power information.
342 * @np: Pointer to OF device_node.
343 * @df: Pointer to devfreq device.
344 * @dfc_power: Pointer to devfreq_cooling_power.
345 *
346 * Register a devfreq cooling device. The available OPPs must be
347 * registered on the device.
348 *
349 * If @dfc_power is provided, the cooling device is registered with the
350 * power extensions. For the power extensions to work correctly,
351 * devfreq should use the simple_ondemand governor, other governors
352 * are not currently supported.
353 */
3c99c2ce 354struct thermal_cooling_device *
a76caf55
ØE
355of_devfreq_cooling_register_power(struct device_node *np, struct devfreq *df,
356 struct devfreq_cooling_power *dfc_power)
357{
358 struct thermal_cooling_device *cdev;
615510fe 359 struct device *dev = df->dev.parent;
a76caf55 360 struct devfreq_cooling_device *dfc;
f8d354e8 361 char *name;
615510fe 362 int err, num_opps;
a76caf55
ØE
363
364 dfc = kzalloc(sizeof(*dfc), GFP_KERNEL);
365 if (!dfc)
366 return ERR_PTR(-ENOMEM);
367
368 dfc->devfreq = df;
369
4401117b
LL
370 dfc->em_pd = em_pd_get(dev);
371 if (dfc->em_pd) {
a76caf55
ØE
372 devfreq_cooling_ops.get_requested_power =
373 devfreq_cooling_get_requested_power;
374 devfreq_cooling_ops.state2power = devfreq_cooling_state2power;
375 devfreq_cooling_ops.power2state = devfreq_cooling_power2state;
615510fe
LL
376
377 dfc->power_ops = dfc_power;
378
4401117b 379 num_opps = em_pd_nr_perf_states(dfc->em_pd);
615510fe
LL
380 } else {
381 /* Backward compatibility for drivers which do not use IPA */
382 dev_dbg(dev, "missing EM for cooling device\n");
383
384 num_opps = dev_pm_opp_get_opp_count(dev);
385
386 err = devfreq_cooling_gen_tables(dfc, num_opps);
387 if (err)
388 goto free_dfc;
a76caf55
ØE
389 }
390
615510fe
LL
391 if (num_opps <= 0) {
392 err = -EINVAL;
a76caf55 393 goto free_dfc;
615510fe
LL
394 }
395
396 /* max_state is an index, not a counter */
397 dfc->max_state = num_opps - 1;
a76caf55 398
615510fe 399 err = dev_pm_qos_add_request(dev, &dfc->req_max_freq,
04fa9c80
MK
400 DEV_PM_QOS_MAX_FREQUENCY,
401 PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE);
2f96c035 402 if (err < 0)
615510fe 403 goto free_table;
04fa9c80 404
9aa80ab2 405 err = -ENOMEM;
f8d354e8
DL
406 name = kasprintf(GFP_KERNEL, "devfreq-%s", dev_name(dev));
407 if (!name)
04fa9c80 408 goto remove_qos_req;
615510fe 409
f8d354e8 410 cdev = thermal_of_cooling_device_register(np, name, dfc,
a76caf55 411 &devfreq_cooling_ops);
f8d354e8
DL
412 kfree(name);
413
a76caf55
ØE
414 if (IS_ERR(cdev)) {
415 err = PTR_ERR(cdev);
615510fe 416 dev_err(dev,
a76caf55
ØE
417 "Failed to register devfreq cooling device (%d)\n",
418 err);
f8d354e8 419 goto remove_qos_req;
a76caf55
ØE
420 }
421
422 dfc->cdev = cdev;
423
3c99c2ce 424 return cdev;
a76caf55 425
04fa9c80
MK
426remove_qos_req:
427 dev_pm_qos_remove_request(&dfc->req_max_freq);
615510fe 428free_table:
a76caf55
ØE
429 kfree(dfc->freq_table);
430free_dfc:
431 kfree(dfc);
432
433 return ERR_PTR(err);
434}
435EXPORT_SYMBOL_GPL(of_devfreq_cooling_register_power);
436
437/**
438 * of_devfreq_cooling_register() - Register devfreq cooling device,
439 * with OF information.
440 * @np: Pointer to OF device_node.
441 * @df: Pointer to devfreq device.
442 */
3c99c2ce 443struct thermal_cooling_device *
a76caf55
ØE
444of_devfreq_cooling_register(struct device_node *np, struct devfreq *df)
445{
446 return of_devfreq_cooling_register_power(np, df, NULL);
447}
448EXPORT_SYMBOL_GPL(of_devfreq_cooling_register);
449
450/**
451 * devfreq_cooling_register() - Register devfreq cooling device.
452 * @df: Pointer to devfreq device.
453 */
3c99c2ce 454struct thermal_cooling_device *devfreq_cooling_register(struct devfreq *df)
a76caf55
ØE
455{
456 return of_devfreq_cooling_register(NULL, df);
457}
458EXPORT_SYMBOL_GPL(devfreq_cooling_register);
459
84e0d87c
LL
460/**
461 * devfreq_cooling_em_register_power() - Register devfreq cooling device with
462 * power information and automatically register Energy Model (EM)
463 * @df: Pointer to devfreq device.
464 * @dfc_power: Pointer to devfreq_cooling_power.
465 *
466 * Register a devfreq cooling device and automatically register EM. The
467 * available OPPs must be registered for the device.
468 *
469 * If @dfc_power is provided, the cooling device is registered with the
470 * power extensions. It is using the simple Energy Model which requires
471 * "dynamic-power-coefficient" a devicetree property. To not break drivers
472 * which miss that DT property, the function won't bail out when the EM
473 * registration failed. The cooling device will be registered if everything
474 * else is OK.
475 */
476struct thermal_cooling_device *
477devfreq_cooling_em_register(struct devfreq *df,
478 struct devfreq_cooling_power *dfc_power)
479{
480 struct thermal_cooling_device *cdev;
481 struct device *dev;
482 int ret;
483
484 if (IS_ERR_OR_NULL(df))
485 return ERR_PTR(-EINVAL);
486
487 dev = df->dev.parent;
488
489 ret = dev_pm_opp_of_register_em(dev, NULL);
490 if (ret)
491 dev_dbg(dev, "Unable to register EM for devfreq cooling device (%d)\n",
492 ret);
493
494 cdev = of_devfreq_cooling_register_power(dev->of_node, df, dfc_power);
495
496 if (IS_ERR_OR_NULL(cdev))
497 em_dev_unregister_perf_domain(dev);
498
499 return cdev;
500}
501EXPORT_SYMBOL_GPL(devfreq_cooling_em_register);
502
a76caf55
ØE
503/**
504 * devfreq_cooling_unregister() - Unregister devfreq cooling device.
1b5cb957 505 * @cdev: Pointer to devfreq cooling device to unregister.
84e0d87c
LL
506 *
507 * Unregisters devfreq cooling device and related Energy Model if it was
508 * present.
a76caf55 509 */
3c99c2ce 510void devfreq_cooling_unregister(struct thermal_cooling_device *cdev)
a76caf55 511{
3c99c2ce 512 struct devfreq_cooling_device *dfc;
84e0d87c 513 struct device *dev;
3c99c2ce 514
84e0d87c 515 if (IS_ERR_OR_NULL(cdev))
a76caf55
ØE
516 return;
517
3c99c2ce 518 dfc = cdev->devdata;
84e0d87c 519 dev = dfc->devfreq->dev.parent;
3c99c2ce 520
a76caf55 521 thermal_cooling_device_unregister(dfc->cdev);
04fa9c80 522 dev_pm_qos_remove_request(&dfc->req_max_freq);
84e0d87c
LL
523
524 em_dev_unregister_perf_domain(dev);
525
a76caf55 526 kfree(dfc->freq_table);
a76caf55
ØE
527 kfree(dfc);
528}
529EXPORT_SYMBOL_GPL(devfreq_cooling_unregister);