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