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