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
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_id)
100 enum thermal_trend trend;
101 struct thermal_instance *instance;
102 struct thermal_trip trip;
103 bool throttle = false;
106 __thermal_zone_get_trip(tz, trip_id, &trip);
108 trend = get_tz_trend(tz, trip_id);
110 if (tz->temperature >= trip.temperature) {
112 trace_thermal_zone_trip(tz, trip_id, trip.type);
115 dev_dbg(&tz->device, "Trip%d[type=%d,temp=%d]:trend=%d,throttle=%d\n",
116 trip_id, trip.type, trip.temperature, trend, throttle);
118 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
119 if (instance->trip != trip_id)
122 old_target = instance->target;
123 instance->target = get_target_state(instance, trend, throttle);
124 dev_dbg(&instance->cdev->device, "old_target=%d, target=%d\n",
125 old_target, (int)instance->target);
127 if (instance->initialized && old_target == instance->target)
130 /* Activate a passive thermal instance */
131 if (old_target == THERMAL_NO_TARGET &&
132 instance->target != THERMAL_NO_TARGET)
133 update_passive_instance(tz, trip.type, 1);
134 /* Deactivate a passive thermal instance */
135 else if (old_target != THERMAL_NO_TARGET &&
136 instance->target == THERMAL_NO_TARGET)
137 update_passive_instance(tz, trip.type, -1);
139 instance->initialized = true;
140 mutex_lock(&instance->cdev->lock);
141 instance->cdev->updated = false; /* cdev needs update */
142 mutex_unlock(&instance->cdev->lock);
147 * step_wise_throttle - throttles devices associated with the given zone
148 * @tz: thermal_zone_device
149 * @trip: trip point index
151 * Throttling Logic: This uses the trend of the thermal zone to throttle.
152 * If the thermal zone is 'heating up' this throttles all the cooling
153 * devices associated with the zone and its particular trip point, by one
154 * step. If the zone is 'cooling down' it brings back the performance of
155 * the devices by one step.
157 static int step_wise_throttle(struct thermal_zone_device *tz, int trip)
159 struct thermal_instance *instance;
161 lockdep_assert_held(&tz->lock);
163 thermal_zone_trip_update(tz, trip);
165 list_for_each_entry(instance, &tz->thermal_instances, tz_node)
166 thermal_cdev_update(instance->cdev);
171 static struct thermal_governor thermal_gov_step_wise = {
173 .throttle = step_wise_throttle,
175 THERMAL_GOVERNOR_DECLARE(thermal_gov_step_wise);