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 #define pr_fmt(fmt) "cpuidle cooling: " fmt
9
10 #include <linux/cpu.h>
11 #include <linux/cpu_cooling.h>
12 #include <linux/cpuidle.h>
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/idle_inject.h>
16 #include <linux/of.h>
17 #include <linux/slab.h>
18 #include <linux/thermal.h>
19
20 /**
21 * struct cpuidle_cooling_device - data for the idle cooling device
22 * @ii_dev: an atomic to keep track of the last task exiting the idle cycle
23 * @state: a normalized integer giving the state of the cooling device
24 */
25 struct cpuidle_cooling_device {
26 struct idle_inject_device *ii_dev;
27 unsigned long state;
28 };
29
30 /**
31 * cpuidle_cooling_runtime - Running time computation
32 * @idle_duration_us: CPU idle time to inject in microseconds
33 * @state: a percentile based number
34 *
35 * The running duration is computed from the idle injection duration
36 * which is fixed. If we reach 100% of idle injection ratio, that
37 * means the running duration is zero. If we have a 50% ratio
38 * injection, that means we have equal duration for idle and for
39 * running duration.
40 *
41 * The formula is deduced as follows:
42 *
43 * running = idle x ((100 / ratio) - 1)
44 *
45 * For precision purpose for integer math, we use the following:
46 *
47 * running = (idle x 100) / ratio - idle
48 *
49 * For example, if we have an injected duration of 50%, then we end up
50 * with 10ms of idle injection and 10ms of running duration.
51 *
52 * Return: An unsigned int for a usec based runtime duration.
53 */
cpuidle_cooling_runtime(unsigned int idle_duration_us,unsigned long state)54 static unsigned int cpuidle_cooling_runtime(unsigned int idle_duration_us,
55 unsigned long state)
56 {
57 if (!state)
58 return 0;
59
60 return ((idle_duration_us * 100) / state) - idle_duration_us;
61 }
62
63 /**
64 * cpuidle_cooling_get_max_state - Get the maximum state
65 * @cdev : the thermal cooling device
66 * @state : a pointer to the state variable to be filled
67 *
68 * The function always returns 100 as the injection ratio. It is
69 * percentile based for consistency across different platforms.
70 *
71 * Return: The function can not fail, it is always zero
72 */
cpuidle_cooling_get_max_state(struct thermal_cooling_device * cdev,unsigned long * state)73 static int cpuidle_cooling_get_max_state(struct thermal_cooling_device *cdev,
74 unsigned long *state)
75 {
76 /*
77 * Depending on the configuration or the hardware, the running
78 * cycle and the idle cycle could be different. We want to
79 * unify that to an 0..100 interval, so the set state
80 * interface will be the same whatever the platform is.
81 *
82 * The state 100% will make the cluster 100% ... idle. A 0%
83 * injection ratio means no idle injection at all and 50%
84 * means for 10ms of idle injection, we have 10ms of running
85 * time.
86 */
87 *state = 100;
88
89 return 0;
90 }
91
92 /**
93 * cpuidle_cooling_get_cur_state - Get the current cooling state
94 * @cdev: the thermal cooling device
95 * @state: a pointer to the state
96 *
97 * The function just copies the state value from the private thermal
98 * cooling device structure, the mapping is 1 <-> 1.
99 *
100 * Return: The function can not fail, it is always zero
101 */
cpuidle_cooling_get_cur_state(struct thermal_cooling_device * cdev,unsigned long * state)102 static int cpuidle_cooling_get_cur_state(struct thermal_cooling_device *cdev,
103 unsigned long *state)
104 {
105 struct cpuidle_cooling_device *idle_cdev = cdev->devdata;
106
107 *state = idle_cdev->state;
108
109 return 0;
110 }
111
112 /**
113 * cpuidle_cooling_set_cur_state - Set the current cooling state
114 * @cdev: the thermal cooling device
115 * @state: the target state
116 *
117 * The function checks first if we are initiating the mitigation which
118 * in turn wakes up all the idle injection tasks belonging to the idle
119 * cooling device. In any case, it updates the internal state for the
120 * cooling device.
121 *
122 * Return: The function can not fail, it is always zero
123 */
cpuidle_cooling_set_cur_state(struct thermal_cooling_device * cdev,unsigned long state)124 static int cpuidle_cooling_set_cur_state(struct thermal_cooling_device *cdev,
125 unsigned long state)
126 {
127 struct cpuidle_cooling_device *idle_cdev = cdev->devdata;
128 struct idle_inject_device *ii_dev = idle_cdev->ii_dev;
129 unsigned long current_state = idle_cdev->state;
130 unsigned int runtime_us, idle_duration_us;
131
132 idle_cdev->state = state;
133
134 idle_inject_get_duration(ii_dev, &runtime_us, &idle_duration_us);
135
136 runtime_us = cpuidle_cooling_runtime(idle_duration_us, state);
137
138 idle_inject_set_duration(ii_dev, runtime_us, idle_duration_us);
139
140 if (current_state == 0 && state > 0) {
141 idle_inject_start(ii_dev);
142 } else if (current_state > 0 && !state) {
143 idle_inject_stop(ii_dev);
144 }
145
146 return 0;
147 }
148
149 /*
150 * cpuidle_cooling_ops - thermal cooling device ops
151 */
152 static struct thermal_cooling_device_ops cpuidle_cooling_ops = {
153 .get_max_state = cpuidle_cooling_get_max_state,
154 .get_cur_state = cpuidle_cooling_get_cur_state,
155 .set_cur_state = cpuidle_cooling_set_cur_state,
156 };
157
158 /**
159 * __cpuidle_cooling_register: register the cooling device
160 * @drv: a cpuidle driver structure pointer
161 * @np: a device node structure pointer used for the thermal binding
162 *
163 * This function is in charge of allocating the cpuidle cooling device
164 * structure, the idle injection, initialize them and register the
165 * cooling device to the thermal framework.
166 *
167 * Return: zero on success, a negative value returned by one of the
168 * underlying subsystem in case of error
169 */
__cpuidle_cooling_register(struct device_node * np,struct cpuidle_driver * drv)170 static int __cpuidle_cooling_register(struct device_node *np,
171 struct cpuidle_driver *drv)
172 {
173 struct idle_inject_device *ii_dev;
174 struct cpuidle_cooling_device *idle_cdev;
175 struct thermal_cooling_device *cdev;
176 struct device *dev;
177 unsigned int idle_duration_us = TICK_USEC;
178 unsigned int latency_us = UINT_MAX;
179 char *name;
180 int ret;
181
182 idle_cdev = kzalloc(sizeof(*idle_cdev), GFP_KERNEL);
183 if (!idle_cdev) {
184 ret = -ENOMEM;
185 goto out;
186 }
187
188 ii_dev = idle_inject_register(drv->cpumask);
189 if (!ii_dev) {
190 ret = -EINVAL;
191 goto out_kfree;
192 }
193
194 of_property_read_u32(np, "duration-us", &idle_duration_us);
195 of_property_read_u32(np, "exit-latency-us", &latency_us);
196
197 idle_inject_set_duration(ii_dev, TICK_USEC, idle_duration_us);
198 idle_inject_set_latency(ii_dev, latency_us);
199
200 idle_cdev->ii_dev = ii_dev;
201
202 dev = get_cpu_device(cpumask_first(drv->cpumask));
203
204 name = kasprintf(GFP_KERNEL, "idle-%s", dev_name(dev));
205 if (!name) {
206 ret = -ENOMEM;
207 goto out_unregister;
208 }
209
210 cdev = thermal_of_cooling_device_register(np, name, idle_cdev,
211 &cpuidle_cooling_ops);
212 if (IS_ERR(cdev)) {
213 ret = PTR_ERR(cdev);
214 goto out_kfree_name;
215 }
216
217 pr_debug("%s: Idle injection set with idle duration=%u, latency=%u\n",
218 name, idle_duration_us, latency_us);
219
220 kfree(name);
221
222 return 0;
223
224 out_kfree_name:
225 kfree(name);
226 out_unregister:
227 idle_inject_unregister(ii_dev);
228 out_kfree:
229 kfree(idle_cdev);
230 out:
231 return ret;
232 }
233
234 /**
235 * cpuidle_cooling_register - Idle cooling device initialization function
236 * @drv: a cpuidle driver structure pointer
237 *
238 * This function is in charge of creating a cooling device per cpuidle
239 * driver and register it to the thermal framework.
240 */
cpuidle_cooling_register(struct cpuidle_driver * drv)241 void cpuidle_cooling_register(struct cpuidle_driver *drv)
242 {
243 struct device_node *cooling_node;
244 struct device_node *cpu_node;
245 int cpu, ret;
246
247 for_each_cpu(cpu, drv->cpumask) {
248
249 cpu_node = of_cpu_device_node_get(cpu);
250
251 cooling_node = of_get_child_by_name(cpu_node, "thermal-idle");
252
253 of_node_put(cpu_node);
254
255 if (!cooling_node) {
256 pr_debug("'thermal-idle' node not found for cpu%d\n", cpu);
257 continue;
258 }
259
260 ret = __cpuidle_cooling_register(cooling_node, drv);
261
262 of_node_put(cooling_node);
263
264 if (ret) {
265 pr_err("Failed to register the cpuidle cooling device" \
266 "for cpu%d: %d\n", cpu, ret);
267 break;
268 }
269 }
270 }
271