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