cpuidle_cooling.c 7.4 KB

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