remoteproc/omap: Add watchdog functionality for remote processors
[sfrench/cifs-2.6.git] / drivers / thermal / cpuidle_cooling.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Copyright (C) 2019 Linaro Limited.
4  *
5  *  Author: Daniel Lezcano <daniel.lezcano@linaro.org>
6  *
7  */
8 #include <linux/cpu_cooling.h>
9 #include <linux/cpuidle.h>
10 #include <linux/err.h>
11 #include <linux/idle_inject.h>
12 #include <linux/idr.h>
13 #include <linux/slab.h>
14 #include <linux/thermal.h>
15
16 /**
17  * struct cpuidle_cooling_device - data for the idle cooling device
18  * @ii_dev: an atomic to keep track of the last task exiting the idle cycle
19  * @state: a normalized integer giving the state of the cooling device
20  */
21 struct cpuidle_cooling_device {
22         struct idle_inject_device *ii_dev;
23         unsigned long state;
24 };
25
26 static DEFINE_IDA(cpuidle_ida);
27
28 /**
29  * cpuidle_cooling_runtime - Running time computation
30  * @idle_duration_us: the idle cooling device
31  * @state: a percentile based number
32  *
33  * The running duration is computed from the idle injection duration
34  * which is fixed. If we reach 100% of idle injection ratio, that
35  * means the running duration is zero. If we have a 50% ratio
36  * injection, that means we have equal duration for idle and for
37  * running duration.
38  *
39  * The formula is deduced as follows:
40  *
41  *  running = idle x ((100 / ratio) - 1)
42  *
43  * For precision purpose for integer math, we use the following:
44  *
45  *  running = (idle x 100) / ratio - idle
46  *
47  * For example, if we have an injected duration of 50%, then we end up
48  * with 10ms of idle injection and 10ms of running duration.
49  *
50  * Return: An unsigned int for a usec based runtime duration.
51  */
52 static unsigned int cpuidle_cooling_runtime(unsigned int idle_duration_us,
53                                             unsigned long state)
54 {
55         if (!state)
56                 return 0;
57
58         return ((idle_duration_us * 100) / state) - idle_duration_us;
59 }
60
61 /**
62  * cpuidle_cooling_get_max_state - Get the maximum state
63  * @cdev  : the thermal cooling device
64  * @state : a pointer to the state variable to be filled
65  *
66  * The function always returns 100 as the injection ratio. It is
67  * percentile based for consistency accross different platforms.
68  *
69  * Return: The function can not fail, it is always zero
70  */
71 static int cpuidle_cooling_get_max_state(struct thermal_cooling_device *cdev,
72                                          unsigned long *state)
73 {
74         /*
75          * Depending on the configuration or the hardware, the running
76          * cycle and the idle cycle could be different. We want to
77          * unify that to an 0..100 interval, so the set state
78          * interface will be the same whatever the platform is.
79          *
80          * The state 100% will make the cluster 100% ... idle. A 0%
81          * injection ratio means no idle injection at all and 50%
82          * means for 10ms of idle injection, we have 10ms of running
83          * time.
84          */
85         *state = 100;
86
87         return 0;
88 }
89
90 /**
91  * cpuidle_cooling_get_cur_state - Get the current cooling state
92  * @cdev: the thermal cooling device
93  * @state: a pointer to the state
94  *
95  * The function just copies  the state value from the private thermal
96  * cooling device structure, the mapping is 1 <-> 1.
97  *
98  * Return: The function can not fail, it is always zero
99  */
100 static int cpuidle_cooling_get_cur_state(struct thermal_cooling_device *cdev,
101                                          unsigned long *state)
102 {
103         struct cpuidle_cooling_device *idle_cdev = cdev->devdata;
104
105         *state = idle_cdev->state;
106
107         return 0;
108 }
109
110 /**
111  * cpuidle_cooling_set_cur_state - Set the current cooling state
112  * @cdev: the thermal cooling device
113  * @state: the target state
114  *
115  * The function checks first if we are initiating the mitigation which
116  * in turn wakes up all the idle injection tasks belonging to the idle
117  * cooling device. In any case, it updates the internal state for the
118  * cooling device.
119  *
120  * Return: The function can not fail, it is always zero
121  */
122 static int cpuidle_cooling_set_cur_state(struct thermal_cooling_device *cdev,
123                                          unsigned long state)
124 {
125         struct cpuidle_cooling_device *idle_cdev = cdev->devdata;
126         struct idle_inject_device *ii_dev = idle_cdev->ii_dev;
127         unsigned long current_state = idle_cdev->state;
128         unsigned int runtime_us, idle_duration_us;
129
130         idle_cdev->state = state;
131
132         idle_inject_get_duration(ii_dev, &runtime_us, &idle_duration_us);
133
134         runtime_us = cpuidle_cooling_runtime(idle_duration_us, state);
135
136         idle_inject_set_duration(ii_dev, runtime_us, idle_duration_us);
137
138         if (current_state == 0 && state > 0) {
139                 idle_inject_start(ii_dev);
140         } else if (current_state > 0 && !state)  {
141                 idle_inject_stop(ii_dev);
142         }
143
144         return 0;
145 }
146
147 /**
148  * cpuidle_cooling_ops - thermal cooling device ops
149  */
150 static struct thermal_cooling_device_ops cpuidle_cooling_ops = {
151         .get_max_state = cpuidle_cooling_get_max_state,
152         .get_cur_state = cpuidle_cooling_get_cur_state,
153         .set_cur_state = cpuidle_cooling_set_cur_state,
154 };
155
156 /**
157  * cpuidle_of_cooling_register - Idle cooling device initialization function
158  * @drv: a cpuidle driver structure pointer
159  * @np: a node pointer to a device tree cooling device node
160  *
161  * This function is in charge of creating a cooling device per cpuidle
162  * driver and register it to thermal framework.
163  *
164  * Return: zero on success, or negative value corresponding to the
165  * error detected in the underlying subsystems.
166  */
167 int cpuidle_of_cooling_register(struct device_node *np,
168                                 struct cpuidle_driver *drv)
169 {
170         struct idle_inject_device *ii_dev;
171         struct cpuidle_cooling_device *idle_cdev;
172         struct thermal_cooling_device *cdev;
173         char dev_name[THERMAL_NAME_LENGTH];
174         int id, ret;
175
176         idle_cdev = kzalloc(sizeof(*idle_cdev), GFP_KERNEL);
177         if (!idle_cdev) {
178                 ret = -ENOMEM;
179                 goto out;
180         }
181
182         id = ida_simple_get(&cpuidle_ida, 0, 0, GFP_KERNEL);
183         if (id < 0) {
184                 ret = id;
185                 goto out_kfree;
186         }
187
188         ii_dev = idle_inject_register(drv->cpumask);
189         if (!ii_dev) {
190                 ret = -EINVAL;
191                 goto out_id;
192         }
193
194         idle_inject_set_duration(ii_dev, TICK_USEC, TICK_USEC);
195
196         idle_cdev->ii_dev = ii_dev;
197
198         snprintf(dev_name, sizeof(dev_name), "thermal-idle-%d", id);
199
200         cdev = thermal_of_cooling_device_register(np, dev_name, idle_cdev,
201                                                   &cpuidle_cooling_ops);
202         if (IS_ERR(cdev)) {
203                 ret = PTR_ERR(cdev);
204                 goto out_unregister;
205         }
206
207         return 0;
208
209 out_unregister:
210         idle_inject_unregister(ii_dev);
211 out_id:
212         ida_simple_remove(&cpuidle_ida, id);
213 out_kfree:
214         kfree(idle_cdev);
215 out:
216         return ret;
217 }
218
219 /**
220  * cpuidle_cooling_register - Idle cooling device initialization function
221  * @drv: a cpuidle driver structure pointer
222  *
223  * This function is in charge of creating a cooling device per cpuidle
224  * driver and register it to thermal framework.
225  *
226  * Return: zero on success, or negative value corresponding to the
227  * error detected in the underlying subsystems.
228  */
229 int cpuidle_cooling_register(struct cpuidle_driver *drv)
230 {
231         return cpuidle_of_cooling_register(NULL, drv);
232 }