gov_step_wise.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * step_wise.c - A step-by-step Thermal throttling governor
  4. *
  5. * Copyright (C) 2012 Intel Corp
  6. * Copyright (C) 2012 Durgadoss R <durgadoss.r@intel.com>
  7. *
  8. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. *
  10. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11. */
  12. #include <linux/thermal.h>
  13. #include <trace/events/thermal.h>
  14. #include "thermal_core.h"
  15. /*
  16. * If the temperature is higher than a trip point,
  17. * a. if the trend is THERMAL_TREND_RAISING, use higher cooling
  18. * state for this trip point
  19. * b. if the trend is THERMAL_TREND_DROPPING, do nothing
  20. * c. if the trend is THERMAL_TREND_RAISE_FULL, use upper limit
  21. * for this trip point
  22. * d. if the trend is THERMAL_TREND_DROP_FULL, use lower limit
  23. * for this trip point
  24. * If the temperature is lower than a trip point,
  25. * a. if the trend is THERMAL_TREND_RAISING, do nothing
  26. * b. if the trend is THERMAL_TREND_DROPPING, use lower cooling
  27. * state for this trip point, if the cooling state already
  28. * equals lower limit, deactivate the thermal instance
  29. * c. if the trend is THERMAL_TREND_RAISE_FULL, do nothing
  30. * d. if the trend is THERMAL_TREND_DROP_FULL, use lower limit,
  31. * if the cooling state already equals lower limit,
  32. * deactivate the thermal instance
  33. */
  34. static unsigned long get_target_state(struct thermal_instance *instance,
  35. enum thermal_trend trend, bool throttle)
  36. {
  37. struct thermal_cooling_device *cdev = instance->cdev;
  38. unsigned long cur_state;
  39. unsigned long next_target;
  40. /*
  41. * We keep this instance the way it is by default.
  42. * Otherwise, we use the current state of the
  43. * cdev in use to determine the next_target.
  44. */
  45. cdev->ops->get_cur_state(cdev, &cur_state);
  46. next_target = instance->target;
  47. dev_dbg(&cdev->device, "cur_state=%ld\n", cur_state);
  48. if (!instance->initialized) {
  49. if (throttle) {
  50. next_target = (cur_state + 1) >= instance->upper ?
  51. instance->upper :
  52. ((cur_state + 1) < instance->lower ?
  53. instance->lower : (cur_state + 1));
  54. } else {
  55. next_target = THERMAL_NO_TARGET;
  56. }
  57. return next_target;
  58. }
  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. if (!throttle) {
  78. next_target = cur_state - 1;
  79. if (next_target > instance->upper)
  80. next_target = instance->upper;
  81. }
  82. }
  83. break;
  84. case THERMAL_TREND_DROP_FULL:
  85. if (cur_state == instance->lower) {
  86. if (!throttle)
  87. next_target = THERMAL_NO_TARGET;
  88. } else
  89. next_target = instance->lower;
  90. break;
  91. default:
  92. break;
  93. }
  94. return next_target;
  95. }
  96. static void update_passive_instance(struct thermal_zone_device *tz,
  97. enum thermal_trip_type type, int value)
  98. {
  99. /*
  100. * If value is +1, activate a passive instance.
  101. * If value is -1, deactivate a passive instance.
  102. */
  103. if (type == THERMAL_TRIP_PASSIVE || type == THERMAL_TRIPS_NONE)
  104. tz->passive += value;
  105. }
  106. static void thermal_zone_trip_update(struct thermal_zone_device *tz, int trip)
  107. {
  108. int trip_temp;
  109. enum thermal_trip_type trip_type;
  110. enum thermal_trend trend;
  111. struct thermal_instance *instance;
  112. bool throttle = false;
  113. int old_target;
  114. if (trip == THERMAL_TRIPS_NONE) {
  115. trip_temp = tz->forced_passive;
  116. trip_type = THERMAL_TRIPS_NONE;
  117. } else {
  118. tz->ops->get_trip_temp(tz, trip, &trip_temp);
  119. tz->ops->get_trip_type(tz, trip, &trip_type);
  120. }
  121. trend = get_tz_trend(tz, trip);
  122. if (tz->temperature >= trip_temp) {
  123. throttle = true;
  124. trace_thermal_zone_trip(tz, trip, trip_type);
  125. }
  126. dev_dbg(&tz->device, "Trip%d[type=%d,temp=%d]:trend=%d,throttle=%d\n",
  127. trip, trip_type, trip_temp, trend, throttle);
  128. mutex_lock(&tz->lock);
  129. list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  130. if (instance->trip != trip)
  131. continue;
  132. old_target = instance->target;
  133. instance->target = get_target_state(instance, trend, throttle);
  134. dev_dbg(&instance->cdev->device, "old_target=%d, target=%d\n",
  135. old_target, (int)instance->target);
  136. if (instance->initialized && old_target == instance->target)
  137. continue;
  138. /* Activate a passive thermal instance */
  139. if (old_target == THERMAL_NO_TARGET &&
  140. instance->target != THERMAL_NO_TARGET)
  141. update_passive_instance(tz, trip_type, 1);
  142. /* Deactivate a passive thermal instance */
  143. else if (old_target != THERMAL_NO_TARGET &&
  144. instance->target == THERMAL_NO_TARGET)
  145. update_passive_instance(tz, trip_type, -1);
  146. instance->initialized = true;
  147. mutex_lock(&instance->cdev->lock);
  148. instance->cdev->updated = false; /* cdev needs update */
  149. mutex_unlock(&instance->cdev->lock);
  150. }
  151. mutex_unlock(&tz->lock);
  152. }
  153. /**
  154. * step_wise_throttle - throttles devices associated with the given zone
  155. * @tz: thermal_zone_device
  156. * @trip: trip point index
  157. *
  158. * Throttling Logic: This uses the trend of the thermal zone to throttle.
  159. * If the thermal zone is 'heating up' this throttles all the cooling
  160. * devices associated with the zone and its particular trip point, by one
  161. * step. If the zone is 'cooling down' it brings back the performance of
  162. * the devices by one step.
  163. */
  164. static int step_wise_throttle(struct thermal_zone_device *tz, int trip)
  165. {
  166. struct thermal_instance *instance;
  167. thermal_zone_trip_update(tz, trip);
  168. if (tz->forced_passive)
  169. thermal_zone_trip_update(tz, THERMAL_TRIPS_NONE);
  170. mutex_lock(&tz->lock);
  171. list_for_each_entry(instance, &tz->thermal_instances, tz_node)
  172. thermal_cdev_update(instance->cdev);
  173. mutex_unlock(&tz->lock);
  174. return 0;
  175. }
  176. static struct thermal_governor thermal_gov_step_wise = {
  177. .name = "step_wise",
  178. .throttle = step_wise_throttle,
  179. };
  180. THERMAL_GOVERNOR_DECLARE(thermal_gov_step_wise);