gov_step_wise.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 <linux/minmax.h>
  14. #include "thermal_trace.h"
  15. #include "thermal_core.h"
  16. /*
  17. * If the temperature is higher than a trip point,
  18. * a. if the trend is THERMAL_TREND_RAISING, use higher cooling
  19. * state for this trip point
  20. * b. if the trend is THERMAL_TREND_DROPPING, use a lower cooling state
  21. * for this trip point, but keep the cooling state above the applicable
  22. * minimum
  23. * If the temperature is lower than a trip point,
  24. * a. if the trend is THERMAL_TREND_RAISING, do nothing
  25. * b. if the trend is THERMAL_TREND_DROPPING, use the minimum applicable
  26. * cooling state for this trip point, or if the cooling state already
  27. * equals lower limit, deactivate the thermal instance
  28. */
  29. static unsigned long get_target_state(struct thermal_instance *instance,
  30. enum thermal_trend trend, bool throttle)
  31. {
  32. struct thermal_cooling_device *cdev = instance->cdev;
  33. unsigned long cur_state;
  34. /*
  35. * We keep this instance the way it is by default.
  36. * Otherwise, we use the current state of the
  37. * cdev in use to determine the next_target.
  38. */
  39. cdev->ops->get_cur_state(cdev, &cur_state);
  40. dev_dbg(&cdev->device, "cur_state=%ld\n", cur_state);
  41. if (!instance->initialized) {
  42. if (throttle)
  43. return clamp(cur_state + 1, instance->lower, instance->upper);
  44. return THERMAL_NO_TARGET;
  45. }
  46. if (throttle) {
  47. if (trend == THERMAL_TREND_RAISING)
  48. return clamp(cur_state + 1, instance->lower, instance->upper);
  49. /*
  50. * If the zone temperature is falling, the cooling level can
  51. * be reduced, but it should still be above the lower state of
  52. * the given thermal instance to pull the temperature further
  53. * down.
  54. */
  55. if (trend == THERMAL_TREND_DROPPING)
  56. return clamp(cur_state - 1,
  57. min(instance->lower + 1, instance->upper),
  58. instance->upper);
  59. } else if (trend == THERMAL_TREND_DROPPING) {
  60. if (cur_state <= instance->lower)
  61. return THERMAL_NO_TARGET;
  62. /*
  63. * If 'throttle' is false, no mitigation is necessary, so
  64. * request the lower state for this instance.
  65. */
  66. return instance->lower;
  67. }
  68. return instance->target;
  69. }
  70. static void thermal_zone_trip_update(struct thermal_zone_device *tz,
  71. const struct thermal_trip_desc *td,
  72. int trip_threshold)
  73. {
  74. bool throttle = tz->temperature >= trip_threshold;
  75. const struct thermal_trip *trip = &td->trip;
  76. enum thermal_trend trend = get_tz_trend(tz, trip);
  77. int trip_id = thermal_zone_trip_id(tz, trip);
  78. struct thermal_instance *instance;
  79. if (throttle)
  80. trace_thermal_zone_trip(tz, trip_id, trip->type);
  81. dev_dbg(&tz->device, "Trip%d[type=%d,temp=%d]:trend=%d,throttle=%d\n",
  82. trip_id, trip->type, trip_threshold, trend, throttle);
  83. list_for_each_entry(instance, &td->thermal_instances, trip_node) {
  84. int old_target;
  85. old_target = instance->target;
  86. instance->target = get_target_state(instance, trend, throttle);
  87. dev_dbg(&instance->cdev->device, "old_target=%d, target=%ld\n",
  88. old_target, instance->target);
  89. if (instance->initialized && old_target == instance->target)
  90. continue;
  91. instance->initialized = true;
  92. scoped_guard(cooling_dev, instance->cdev) {
  93. instance->cdev->updated = false; /* cdev needs update */
  94. }
  95. }
  96. }
  97. static void step_wise_manage(struct thermal_zone_device *tz)
  98. {
  99. const struct thermal_trip_desc *td;
  100. struct thermal_instance *instance;
  101. lockdep_assert_held(&tz->lock);
  102. /*
  103. * Throttling Logic: Use the trend of the thermal zone to throttle.
  104. * If the thermal zone is 'heating up', throttle all of the cooling
  105. * devices associated with each trip point by one step. If the zone
  106. * is 'cooling down', it brings back the performance of the devices
  107. * by one step.
  108. */
  109. for_each_trip_desc(tz, td) {
  110. const struct thermal_trip *trip = &td->trip;
  111. if (trip->temperature == THERMAL_TEMP_INVALID ||
  112. trip->type == THERMAL_TRIP_CRITICAL ||
  113. trip->type == THERMAL_TRIP_HOT)
  114. continue;
  115. thermal_zone_trip_update(tz, td, td->threshold);
  116. }
  117. for_each_trip_desc(tz, td) {
  118. list_for_each_entry(instance, &td->thermal_instances, trip_node)
  119. thermal_cdev_update(instance->cdev);
  120. }
  121. }
  122. static struct thermal_governor thermal_gov_step_wise = {
  123. .name = "step_wise",
  124. .manage = step_wise_manage,
  125. };
  126. THERMAL_GOVERNOR_DECLARE(thermal_gov_step_wise);