1 // SPDX-License-Identifier: GPL-2.0-only
3 * step_wise.c - A step-by-step Thermal throttling governor
5 * Copyright (C) 2012 Intel Corp
6 * Copyright (C) 2012 Durgadoss R <durgadoss.r@intel.com>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13 #include <linux/thermal.h>
14 #include <linux/minmax.h>
15 #include <trace/events/thermal.h>
17 #include "thermal_core.h"
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
26 * d. if the trend is THERMAL_TREND_DROP_FULL, use lower limit
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
38 static unsigned long get_target_state(struct thermal_instance *instance,
39 enum thermal_trend trend, bool throttle)
41 struct thermal_cooling_device *cdev = instance->cdev;
42 unsigned long cur_state;
43 unsigned long next_target;
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.
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);
54 if (!instance->initialized) {
56 next_target = clamp((cur_state + 1), instance->lower, instance->upper);
58 next_target = THERMAL_NO_TARGET;
65 case THERMAL_TREND_RAISING:
67 next_target = clamp((cur_state + 1), instance->lower, instance->upper);
70 case THERMAL_TREND_DROPPING:
71 if (cur_state <= instance->lower) {
73 next_target = THERMAL_NO_TARGET;
76 next_target = clamp((cur_state - 1), instance->lower, instance->upper);
87 static void update_passive_instance(struct thermal_zone_device *tz,
88 enum thermal_trip_type type, int value)
91 * If value is +1, activate a passive instance.
92 * If value is -1, deactivate a passive instance.
94 if (type == THERMAL_TRIP_PASSIVE)
98 static void thermal_zone_trip_update(struct thermal_zone_device *tz, int trip)
101 enum thermal_trip_type trip_type;
102 enum thermal_trend trend;
103 struct thermal_instance *instance;
104 bool throttle = false;
107 tz->ops->get_trip_temp(tz, trip, &trip_temp);
108 tz->ops->get_trip_type(tz, trip, &trip_type);
110 trend = get_tz_trend(tz, trip);
112 if (tz->temperature >= trip_temp) {
114 trace_thermal_zone_trip(tz, trip, trip_type);
117 dev_dbg(&tz->device, "Trip%d[type=%d,temp=%d]:trend=%d,throttle=%d\n",
118 trip, trip_type, trip_temp, trend, throttle);
120 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
121 if (instance->trip != trip)
124 old_target = instance->target;
125 instance->target = get_target_state(instance, trend, throttle);
126 dev_dbg(&instance->cdev->device, "old_target=%d, target=%d\n",
127 old_target, (int)instance->target);
129 if (instance->initialized && old_target == instance->target)
132 /* Activate a passive thermal instance */
133 if (old_target == THERMAL_NO_TARGET &&
134 instance->target != THERMAL_NO_TARGET)
135 update_passive_instance(tz, trip_type, 1);
136 /* Deactivate a passive thermal instance */
137 else if (old_target != THERMAL_NO_TARGET &&
138 instance->target == THERMAL_NO_TARGET)
139 update_passive_instance(tz, trip_type, -1);
141 instance->initialized = true;
142 mutex_lock(&instance->cdev->lock);
143 instance->cdev->updated = false; /* cdev needs update */
144 mutex_unlock(&instance->cdev->lock);
149 * step_wise_throttle - throttles devices associated with the given zone
150 * @tz: thermal_zone_device
151 * @trip: trip point index
153 * Throttling Logic: This uses the trend of the thermal zone to throttle.
154 * If the thermal zone is 'heating up' this throttles all the cooling
155 * devices associated with the zone and its particular trip point, by one
156 * step. If the zone is 'cooling down' it brings back the performance of
157 * the devices by one step.
159 static int step_wise_throttle(struct thermal_zone_device *tz, int trip)
161 struct thermal_instance *instance;
163 lockdep_assert_held(&tz->lock);
165 thermal_zone_trip_update(tz, trip);
167 list_for_each_entry(instance, &tz->thermal_instances, tz_node)
168 thermal_cdev_update(instance->cdev);
173 static struct thermal_governor thermal_gov_step_wise = {
175 .throttle = step_wise_throttle,
177 THERMAL_GOVERNOR_DECLARE(thermal_gov_step_wise);