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