1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2019 Linaro Limited.
5 * Author: Daniel Lezcano <daniel.lezcano@linaro.org>
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>
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
21 struct cpuidle_cooling_device
{
22 struct idle_inject_device
*ii_dev
;
26 static DEFINE_IDA(cpuidle_ida
);
29 * cpuidle_cooling_runtime - Running time computation
30 * @idle_duration_us: the idle cooling device
31 * @state: a percentile based number
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
39 * The formula is deduced as follows:
41 * running = idle x ((100 / ratio) - 1)
43 * For precision purpose for integer math, we use the following:
45 * running = (idle x 100) / ratio - idle
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.
50 * Return: An unsigned int for a usec based runtime duration.
52 static unsigned int cpuidle_cooling_runtime(unsigned int idle_duration_us
,
58 return ((idle_duration_us
* 100) / state
) - idle_duration_us
;
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
66 * The function always returns 100 as the injection ratio. It is
67 * percentile based for consistency accross different platforms.
69 * Return: The function can not fail, it is always zero
71 static int cpuidle_cooling_get_max_state(struct thermal_cooling_device
*cdev
,
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.
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
91 * cpuidle_cooling_get_cur_state - Get the current cooling state
92 * @cdev: the thermal cooling device
93 * @state: a pointer to the state
95 * The function just copies the state value from the private thermal
96 * cooling device structure, the mapping is 1 <-> 1.
98 * Return: The function can not fail, it is always zero
100 static int cpuidle_cooling_get_cur_state(struct thermal_cooling_device
*cdev
,
101 unsigned long *state
)
103 struct cpuidle_cooling_device
*idle_cdev
= cdev
->devdata
;
105 *state
= idle_cdev
->state
;
111 * cpuidle_cooling_set_cur_state - Set the current cooling state
112 * @cdev: the thermal cooling device
113 * @state: the target state
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
120 * Return: The function can not fail, it is always zero
122 static int cpuidle_cooling_set_cur_state(struct thermal_cooling_device
*cdev
,
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
;
130 idle_cdev
->state
= state
;
132 idle_inject_get_duration(ii_dev
, &runtime_us
, &idle_duration_us
);
134 runtime_us
= cpuidle_cooling_runtime(idle_duration_us
, state
);
136 idle_inject_set_duration(ii_dev
, runtime_us
, idle_duration_us
);
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
);
148 * cpuidle_cooling_ops - thermal cooling device ops
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
,
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
161 * This function is in charge of creating a cooling device per cpuidle
162 * driver and register it to thermal framework.
164 * Return: zero on success, or negative value corresponding to the
165 * error detected in the underlying subsystems.
167 int cpuidle_of_cooling_register(struct device_node
*np
,
168 struct cpuidle_driver
*drv
)
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
];
176 idle_cdev
= kzalloc(sizeof(*idle_cdev
), GFP_KERNEL
);
182 id
= ida_simple_get(&cpuidle_ida
, 0, 0, GFP_KERNEL
);
188 ii_dev
= idle_inject_register(drv
->cpumask
);
194 idle_inject_set_duration(ii_dev
, TICK_USEC
, TICK_USEC
);
196 idle_cdev
->ii_dev
= ii_dev
;
198 snprintf(dev_name
, sizeof(dev_name
), "thermal-idle-%d", id
);
200 cdev
= thermal_of_cooling_device_register(np
, dev_name
, idle_cdev
,
201 &cpuidle_cooling_ops
);
210 idle_inject_unregister(ii_dev
);
212 ida_simple_remove(&cpuidle_ida
, id
);
220 * cpuidle_cooling_register - Idle cooling device initialization function
221 * @drv: a cpuidle driver structure pointer
223 * This function is in charge of creating a cooling device per cpuidle
224 * driver and register it to thermal framework.
226 * Return: zero on success, or negative value corresponding to the
227 * error detected in the underlying subsystems.
229 int cpuidle_cooling_register(struct cpuidle_driver
*drv
)
231 return cpuidle_of_cooling_register(NULL
, drv
);