step_wise.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #include <linux/thermal.h>
  25. #include "thermal_core.h"
  26. /*
  27. * If the temperature is higher than a trip point,
  28. * a. if the trend is THERMAL_TREND_RAISING, use higher cooling
  29. * state for this trip point
  30. * b. if the trend is THERMAL_TREND_DROPPING, use lower cooling
  31. * state for this trip point
  32. * c. if the trend is THERMAL_TREND_RAISE_FULL, use upper limit
  33. * for this trip point
  34. * d. if the trend is THERMAL_TREND_DROP_FULL, use lower limit
  35. * for this trip point
  36. * If the temperature is lower than a trip point,
  37. * a. if the trend is THERMAL_TREND_RAISING, do nothing
  38. * b. if the trend is THERMAL_TREND_DROPPING, use lower cooling
  39. * state for this trip point, if the cooling state already
  40. * equals lower limit, deactivate the thermal instance
  41. * c. if the trend is THERMAL_TREND_RAISE_FULL, do nothing
  42. * d. if the trend is THERMAL_TREND_DROP_FULL, use lower limit,
  43. * if the cooling state already equals lower limit,
  44. * deactive the thermal instance
  45. */
  46. static unsigned long get_target_state(struct thermal_instance *instance,
  47. enum thermal_trend trend, bool throttle)
  48. {
  49. struct thermal_cooling_device *cdev = instance->cdev;
  50. unsigned long cur_state;
  51. unsigned long next_target;
  52. /*
  53. * We keep this instance the way it is by default.
  54. * Otherwise, we use the current state of the
  55. * cdev in use to determine the next_target.
  56. */
  57. cdev->ops->get_cur_state(cdev, &cur_state);
  58. next_target = instance->target;
  59. switch (trend) {
  60. case THERMAL_TREND_RAISING:
  61. if (throttle) {
  62. next_target = cur_state < instance->upper ?
  63. (cur_state + 1) : instance->upper;
  64. if (next_target < instance->lower)
  65. next_target = instance->lower;
  66. }
  67. break;
  68. case THERMAL_TREND_RAISE_FULL:
  69. if (throttle)
  70. next_target = instance->upper;
  71. break;
  72. case THERMAL_TREND_DROPPING:
  73. if (cur_state == instance->lower) {
  74. if (!throttle)
  75. next_target = THERMAL_NO_TARGET;
  76. } else {
  77. next_target = cur_state - 1;
  78. if (next_target > instance->upper)
  79. next_target = instance->upper;
  80. }
  81. break;
  82. case THERMAL_TREND_DROP_FULL:
  83. if (cur_state == instance->lower) {
  84. if (!throttle)
  85. next_target = THERMAL_NO_TARGET;
  86. } else
  87. next_target = instance->lower;
  88. break;
  89. default:
  90. break;
  91. }
  92. return next_target;
  93. }
  94. static void update_passive_instance(struct thermal_zone_device *tz,
  95. enum thermal_trip_type type, int value)
  96. {
  97. /*
  98. * If value is +1, activate a passive instance.
  99. * If value is -1, deactivate a passive instance.
  100. */
  101. if (type == THERMAL_TRIP_PASSIVE || type == THERMAL_TRIPS_NONE)
  102. tz->passive += value;
  103. }
  104. static void thermal_zone_trip_update(struct thermal_zone_device *tz, int trip)
  105. {
  106. long trip_temp;
  107. enum thermal_trip_type trip_type;
  108. enum thermal_trend trend;
  109. struct thermal_instance *instance;
  110. bool throttle = false;
  111. int old_target;
  112. if (trip == THERMAL_TRIPS_NONE) {
  113. trip_temp = tz->forced_passive;
  114. trip_type = THERMAL_TRIPS_NONE;
  115. } else {
  116. tz->ops->get_trip_temp(tz, trip, &trip_temp);
  117. tz->ops->get_trip_type(tz, trip, &trip_type);
  118. }
  119. trend = get_tz_trend(tz, trip);
  120. if (tz->temperature >= trip_temp)
  121. throttle = true;
  122. mutex_lock(&tz->lock);
  123. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  124. if (instance->trip != trip)
  125. continue;
  126. old_target = instance->target;
  127. instance->target = get_target_state(instance, trend, throttle);
  128. if (old_target == instance->target)
  129. continue;
  130. /* Activate a passive thermal instance */
  131. if (old_target == THERMAL_NO_TARGET &&
  132. instance->target != THERMAL_NO_TARGET)
  133. update_passive_instance(tz, trip_type, 1);
  134. /* Deactivate a passive thermal instance */
  135. else if (old_target != THERMAL_NO_TARGET &&
  136. instance->target == THERMAL_NO_TARGET)
  137. update_passive_instance(tz, trip_type, -1);
  138. instance->cdev->updated = false; /* cdev needs update */
  139. }
  140. mutex_unlock(&tz->lock);
  141. }
  142. /**
  143. * step_wise_throttle - throttles devices asscciated with the given zone
  144. * @tz - thermal_zone_device
  145. * @trip - the trip point
  146. * @trip_type - type of the trip point
  147. *
  148. * Throttling Logic: This uses the trend of the thermal zone to throttle.
  149. * If the thermal zone is 'heating up' this throttles all the cooling
  150. * devices associated with the zone and its particular trip point, by one
  151. * step. If the zone is 'cooling down' it brings back the performance of
  152. * the devices by one step.
  153. */
  154. static int step_wise_throttle(struct thermal_zone_device *tz, int trip)
  155. {
  156. struct thermal_instance *instance;
  157. thermal_zone_trip_update(tz, trip);
  158. if (tz->forced_passive)
  159. thermal_zone_trip_update(tz, THERMAL_TRIPS_NONE);
  160. mutex_lock(&tz->lock);
  161. list_for_each_entry(instance, &tz->thermal_instances, tz_node)
  162. thermal_cdev_update(instance->cdev);
  163. mutex_unlock(&tz->lock);
  164. return 0;
  165. }
  166. static struct thermal_governor thermal_gov_step_wise = {
  167. .name = "step_wise",
  168. .throttle = step_wise_throttle,
  169. };
  170. int thermal_gov_step_wise_register(void)
  171. {
  172. return thermal_register_governor(&thermal_gov_step_wise);
  173. }
  174. void thermal_gov_step_wise_unregister(void)
  175. {
  176. thermal_unregister_governor(&thermal_gov_step_wise);
  177. }