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