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