step_wise.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * step_wise.c - A step-by-step Thermal throttling governor
  3. *
  4. * Copyright (C) 2012 Intel Corp
  5. * Copyright (C) 2012 Durgadoss R <durgadoss.r@intel.com>
  6. *
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  21. *
  22. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23. */
  24. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  25. #include <linux/module.h>
  26. #include <linux/thermal.h>
  27. #include "thermal_core.h"
  28. /*
  29. * If the temperature is higher than a trip point,
  30. * a. if the trend is THERMAL_TREND_RAISING, use higher cooling
  31. * state for this trip point
  32. * b. if the trend is THERMAL_TREND_DROPPING, use lower cooling
  33. * state for this trip point
  34. * c. if the trend is THERMAL_TREND_RAISE_FULL, use upper limit
  35. * for this trip point
  36. * d. if the trend is THERMAL_TREND_DROP_FULL, use lower limit
  37. * for this trip point
  38. * If the temperature is lower than a trip point,
  39. * a. if the trend is THERMAL_TREND_RAISING, do nothing
  40. * b. if the trend is THERMAL_TREND_DROPPING, use lower cooling
  41. * state for this trip point, if the cooling state already
  42. * equals lower limit, deactivate the thermal instance
  43. * c. if the trend is THERMAL_TREND_RAISE_FULL, do nothing
  44. * d. if the trend is THERMAL_TREND_DROP_FULL, use lower limit,
  45. * if the cooling state already equals lower limit,
  46. * deactive the thermal instance
  47. */
  48. static unsigned long get_target_state(struct thermal_instance *instance,
  49. enum thermal_trend trend, bool throttle)
  50. {
  51. struct thermal_cooling_device *cdev = instance->cdev;
  52. unsigned long cur_state;
  53. cdev->ops->get_cur_state(cdev, &cur_state);
  54. switch (trend) {
  55. case THERMAL_TREND_RAISING:
  56. if (throttle)
  57. cur_state = cur_state < instance->upper ?
  58. (cur_state + 1) : instance->upper;
  59. break;
  60. case THERMAL_TREND_RAISE_FULL:
  61. if (throttle)
  62. cur_state = instance->upper;
  63. break;
  64. case THERMAL_TREND_DROPPING:
  65. if (cur_state == instance->lower) {
  66. if (!throttle)
  67. cur_state = -1;
  68. } else
  69. cur_state -= 1;
  70. break;
  71. case THERMAL_TREND_DROP_FULL:
  72. if (cur_state == instance->lower) {
  73. if (!throttle)
  74. cur_state = -1;
  75. } else
  76. cur_state = instance->lower;
  77. break;
  78. default:
  79. break;
  80. }
  81. return cur_state;
  82. }
  83. static void update_passive_instance(struct thermal_zone_device *tz,
  84. enum thermal_trip_type type, int value)
  85. {
  86. /*
  87. * If value is +1, activate a passive instance.
  88. * If value is -1, deactivate a passive instance.
  89. */
  90. if (type == THERMAL_TRIP_PASSIVE || type == THERMAL_TRIPS_NONE)
  91. tz->passive += value;
  92. }
  93. static void update_instance_for_throttle(struct thermal_zone_device *tz,
  94. int trip, enum thermal_trip_type trip_type,
  95. enum thermal_trend trend)
  96. {
  97. struct thermal_instance *instance;
  98. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  99. if (instance->trip != trip)
  100. continue;
  101. instance->target = get_target_state(instance, trend, true);
  102. /* Activate a passive thermal instance */
  103. if (instance->target == THERMAL_NO_TARGET)
  104. update_passive_instance(tz, trip_type, 1);
  105. instance->cdev->updated = false; /* cdev needs update */
  106. }
  107. }
  108. static void update_instance_for_dethrottle(struct thermal_zone_device *tz,
  109. int trip, enum thermal_trip_type trip_type,
  110. enum thermal_trend trend)
  111. {
  112. struct thermal_instance *instance;
  113. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  114. if (instance->trip != trip ||
  115. instance->target == THERMAL_NO_TARGET)
  116. continue;
  117. instance->target = get_target_state(instance, trend, false);
  118. /* Deactivate a passive thermal instance */
  119. if (instance->target == THERMAL_NO_TARGET)
  120. update_passive_instance(tz, trip_type, -1);
  121. instance->cdev->updated = false; /* cdev needs update */
  122. }
  123. }
  124. static void thermal_zone_trip_update(struct thermal_zone_device *tz, int trip)
  125. {
  126. long trip_temp;
  127. enum thermal_trip_type trip_type;
  128. enum thermal_trend trend;
  129. if (trip == THERMAL_TRIPS_NONE) {
  130. trip_temp = tz->forced_passive;
  131. trip_type = THERMAL_TRIPS_NONE;
  132. } else {
  133. tz->ops->get_trip_temp(tz, trip, &trip_temp);
  134. tz->ops->get_trip_type(tz, trip, &trip_type);
  135. }
  136. trend = get_tz_trend(tz, trip);
  137. mutex_lock(&tz->lock);
  138. if (tz->temperature >= trip_temp)
  139. update_instance_for_throttle(tz, trip, trip_type, trend);
  140. else
  141. update_instance_for_dethrottle(tz, trip, trip_type, trend);
  142. mutex_unlock(&tz->lock);
  143. }
  144. /**
  145. * step_wise_throttle - throttles devices asscciated with the given zone
  146. * @tz - thermal_zone_device
  147. * @trip - the trip point
  148. * @trip_type - type of the trip point
  149. *
  150. * Throttling Logic: This uses the trend of the thermal zone to throttle.
  151. * If the thermal zone is 'heating up' this throttles all the cooling
  152. * devices associated with the zone and its particular trip point, by one
  153. * step. If the zone is 'cooling down' it brings back the performance of
  154. * the devices by one step.
  155. */
  156. static int step_wise_throttle(struct thermal_zone_device *tz, int trip)
  157. {
  158. struct thermal_instance *instance;
  159. thermal_zone_trip_update(tz, trip);
  160. if (tz->forced_passive)
  161. thermal_zone_trip_update(tz, THERMAL_TRIPS_NONE);
  162. mutex_lock(&tz->lock);
  163. list_for_each_entry(instance, &tz->thermal_instances, tz_node)
  164. thermal_cdev_update(instance->cdev);
  165. mutex_unlock(&tz->lock);
  166. return 0;
  167. }
  168. static struct thermal_governor thermal_gov_step_wise = {
  169. .name = "step_wise",
  170. .throttle = step_wise_throttle,
  171. .owner = THIS_MODULE,
  172. };
  173. static int __init thermal_gov_step_wise_init(void)
  174. {
  175. return thermal_register_governor(&thermal_gov_step_wise);
  176. }
  177. static void __exit thermal_gov_step_wise_exit(void)
  178. {
  179. thermal_unregister_governor(&thermal_gov_step_wise);
  180. }
  181. /* This should load after thermal framework */
  182. fs_initcall(thermal_gov_step_wise_init);
  183. module_exit(thermal_gov_step_wise_exit);
  184. MODULE_AUTHOR("Durgadoss R");
  185. MODULE_DESCRIPTION("A step-by-step thermal throttling governor");
  186. MODULE_LICENSE("GPL");