cpuidle_cooling.c 7.4 KB

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