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