Thomas Gleixner | 873e65b | 2019-05-27 08:55:15 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Durgadoss R | e151a20 | 2012-09-21 12:06:04 +0530 | [diff] [blame] | 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 | * |
Durgadoss R | e151a20 | 2012-09-21 12:06:04 +0530 | [diff] [blame] | 10 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 11 | */ |
| 12 | |
Durgadoss R | e151a20 | 2012-09-21 12:06:04 +0530 | [diff] [blame] | 13 | #include <linux/thermal.h> |
Punit Agrawal | 208cd82 | 2014-07-29 11:50:50 +0100 | [diff] [blame] | 14 | #include <trace/events/thermal.h> |
Durgadoss R | e151a20 | 2012-09-21 12:06:04 +0530 | [diff] [blame] | 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 |
Daniel Lezcano | 07209fc | 2017-10-19 19:05:58 +0200 | [diff] [blame] | 22 | * b. if the trend is THERMAL_TREND_DROPPING, do nothing |
Zhang Rui | 3dbfff3 | 2012-11-19 16:10:20 +0800 | [diff] [blame] | 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, |
Brian Norris | 56b613e | 2015-01-29 09:57:21 -0800 | [diff] [blame] | 35 | * deactivate the thermal instance |
Durgadoss R | e151a20 | 2012-09-21 12:06:04 +0530 | [diff] [blame] | 36 | */ |
| 37 | static unsigned long get_target_state(struct thermal_instance *instance, |
Zhang Rui | 3dbfff3 | 2012-11-19 16:10:20 +0800 | [diff] [blame] | 38 | enum thermal_trend trend, bool throttle) |
Durgadoss R | e151a20 | 2012-09-21 12:06:04 +0530 | [diff] [blame] | 39 | { |
| 40 | struct thermal_cooling_device *cdev = instance->cdev; |
| 41 | unsigned long cur_state; |
Eduardo Valentin | ca56caa | 2013-06-17 21:24:24 +0800 | [diff] [blame] | 42 | unsigned long next_target; |
Durgadoss R | e151a20 | 2012-09-21 12:06:04 +0530 | [diff] [blame] | 43 | |
Eduardo Valentin | ca56caa | 2013-06-17 21:24:24 +0800 | [diff] [blame] | 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 | */ |
Durgadoss R | e151a20 | 2012-09-21 12:06:04 +0530 | [diff] [blame] | 49 | cdev->ops->get_cur_state(cdev, &cur_state); |
Eduardo Valentin | ca56caa | 2013-06-17 21:24:24 +0800 | [diff] [blame] | 50 | next_target = instance->target; |
Aaron Lu | 06475b5 | 2013-12-02 13:54:26 +0800 | [diff] [blame] | 51 | dev_dbg(&cdev->device, "cur_state=%ld\n", cur_state); |
Durgadoss R | e151a20 | 2012-09-21 12:06:04 +0530 | [diff] [blame] | 52 | |
Zhang Rui | bb431ba | 2015-10-30 16:31:47 +0800 | [diff] [blame] | 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 | |
Zhang Rui | 3dbfff3 | 2012-11-19 16:10:20 +0800 | [diff] [blame] | 66 | switch (trend) { |
| 67 | case THERMAL_TREND_RAISING: |
Andrew Bresticker | e79fe64 | 2013-04-09 21:59:47 +0000 | [diff] [blame] | 68 | if (throttle) { |
Eduardo Valentin | ca56caa | 2013-06-17 21:24:24 +0800 | [diff] [blame] | 69 | next_target = cur_state < instance->upper ? |
Zhang Rui | 3dbfff3 | 2012-11-19 16:10:20 +0800 | [diff] [blame] | 70 | (cur_state + 1) : instance->upper; |
Eduardo Valentin | ca56caa | 2013-06-17 21:24:24 +0800 | [diff] [blame] | 71 | if (next_target < instance->lower) |
| 72 | next_target = instance->lower; |
Andrew Bresticker | e79fe64 | 2013-04-09 21:59:47 +0000 | [diff] [blame] | 73 | } |
Zhang Rui | 3dbfff3 | 2012-11-19 16:10:20 +0800 | [diff] [blame] | 74 | break; |
| 75 | case THERMAL_TREND_RAISE_FULL: |
| 76 | if (throttle) |
Eduardo Valentin | ca56caa | 2013-06-17 21:24:24 +0800 | [diff] [blame] | 77 | next_target = instance->upper; |
Zhang Rui | 3dbfff3 | 2012-11-19 16:10:20 +0800 | [diff] [blame] | 78 | break; |
| 79 | case THERMAL_TREND_DROPPING: |
Lukasz Majewski | 26bb0e9 | 2014-09-24 10:27:10 +0200 | [diff] [blame] | 80 | if (cur_state <= instance->lower) { |
Zhang Rui | 3dbfff3 | 2012-11-19 16:10:20 +0800 | [diff] [blame] | 81 | if (!throttle) |
Eduardo Valentin | ca56caa | 2013-06-17 21:24:24 +0800 | [diff] [blame] | 82 | next_target = THERMAL_NO_TARGET; |
Andrew Bresticker | e79fe64 | 2013-04-09 21:59:47 +0000 | [diff] [blame] | 83 | } else { |
Daniel Lezcano | 07209fc | 2017-10-19 19:05:58 +0200 | [diff] [blame] | 84 | if (!throttle) { |
| 85 | next_target = cur_state - 1; |
| 86 | if (next_target > instance->upper) |
| 87 | next_target = instance->upper; |
| 88 | } |
Andrew Bresticker | e79fe64 | 2013-04-09 21:59:47 +0000 | [diff] [blame] | 89 | } |
Zhang Rui | 3dbfff3 | 2012-11-19 16:10:20 +0800 | [diff] [blame] | 90 | break; |
| 91 | case THERMAL_TREND_DROP_FULL: |
| 92 | if (cur_state == instance->lower) { |
| 93 | if (!throttle) |
Eduardo Valentin | ca56caa | 2013-06-17 21:24:24 +0800 | [diff] [blame] | 94 | next_target = THERMAL_NO_TARGET; |
Zhang Rui | 3dbfff3 | 2012-11-19 16:10:20 +0800 | [diff] [blame] | 95 | } else |
Eduardo Valentin | ca56caa | 2013-06-17 21:24:24 +0800 | [diff] [blame] | 96 | next_target = instance->lower; |
Zhang Rui | 3dbfff3 | 2012-11-19 16:10:20 +0800 | [diff] [blame] | 97 | break; |
| 98 | default: |
| 99 | break; |
Durgadoss R | e151a20 | 2012-09-21 12:06:04 +0530 | [diff] [blame] | 100 | } |
| 101 | |
Eduardo Valentin | ca56caa | 2013-06-17 21:24:24 +0800 | [diff] [blame] | 102 | return next_target; |
Durgadoss R | e151a20 | 2012-09-21 12:06:04 +0530 | [diff] [blame] | 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 | */ |
Daniel Lezcano | a7d6ba1 | 2020-12-15 00:38:04 +0100 | [diff] [blame^] | 112 | if (type == THERMAL_TRIP_PASSIVE) |
Durgadoss R | e151a20 | 2012-09-21 12:06:04 +0530 | [diff] [blame] | 113 | tz->passive += value; |
| 114 | } |
| 115 | |
Durgadoss R | e151a20 | 2012-09-21 12:06:04 +0530 | [diff] [blame] | 116 | static void thermal_zone_trip_update(struct thermal_zone_device *tz, int trip) |
| 117 | { |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 118 | int trip_temp; |
Durgadoss R | e151a20 | 2012-09-21 12:06:04 +0530 | [diff] [blame] | 119 | enum thermal_trip_type trip_type; |
| 120 | enum thermal_trend trend; |
Zhang Rui | b8bb6cb | 2012-11-22 15:45:02 +0800 | [diff] [blame] | 121 | struct thermal_instance *instance; |
| 122 | bool throttle = false; |
| 123 | int old_target; |
Durgadoss R | e151a20 | 2012-09-21 12:06:04 +0530 | [diff] [blame] | 124 | |
Daniel Lezcano | a7d6ba1 | 2020-12-15 00:38:04 +0100 | [diff] [blame^] | 125 | tz->ops->get_trip_temp(tz, trip, &trip_temp); |
| 126 | tz->ops->get_trip_type(tz, trip, &trip_type); |
Durgadoss R | e151a20 | 2012-09-21 12:06:04 +0530 | [diff] [blame] | 127 | |
| 128 | trend = get_tz_trend(tz, trip); |
| 129 | |
Punit Agrawal | 208cd82 | 2014-07-29 11:50:50 +0100 | [diff] [blame] | 130 | if (tz->temperature >= trip_temp) { |
Zhang Rui | b8bb6cb | 2012-11-22 15:45:02 +0800 | [diff] [blame] | 131 | throttle = true; |
Punit Agrawal | 208cd82 | 2014-07-29 11:50:50 +0100 | [diff] [blame] | 132 | trace_thermal_zone_trip(tz, trip, trip_type); |
| 133 | } |
Zhang Rui | b8bb6cb | 2012-11-22 15:45:02 +0800 | [diff] [blame] | 134 | |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 135 | dev_dbg(&tz->device, "Trip%d[type=%d,temp=%d]:trend=%d,throttle=%d\n", |
Aaron Lu | 06475b5 | 2013-12-02 13:54:26 +0800 | [diff] [blame] | 136 | trip, trip_type, trip_temp, trend, throttle); |
| 137 | |
Durgadoss R | e151a20 | 2012-09-21 12:06:04 +0530 | [diff] [blame] | 138 | mutex_lock(&tz->lock); |
| 139 | |
Zhang Rui | b8bb6cb | 2012-11-22 15:45:02 +0800 | [diff] [blame] | 140 | list_for_each_entry(instance, &tz->thermal_instances, tz_node) { |
| 141 | if (instance->trip != trip) |
| 142 | continue; |
| 143 | |
| 144 | old_target = instance->target; |
| 145 | instance->target = get_target_state(instance, trend, throttle); |
Aaron Lu | 06475b5 | 2013-12-02 13:54:26 +0800 | [diff] [blame] | 146 | dev_dbg(&instance->cdev->device, "old_target=%d, target=%d\n", |
| 147 | old_target, (int)instance->target); |
Zhang Rui | b8bb6cb | 2012-11-22 15:45:02 +0800 | [diff] [blame] | 148 | |
Zhang Rui | bb431ba | 2015-10-30 16:31:47 +0800 | [diff] [blame] | 149 | if (instance->initialized && old_target == instance->target) |
Shawn Guo | 178c249 | 2013-06-17 21:24:23 +0800 | [diff] [blame] | 150 | continue; |
| 151 | |
Zhang Rui | b8bb6cb | 2012-11-22 15:45:02 +0800 | [diff] [blame] | 152 | /* Activate a passive thermal instance */ |
| 153 | if (old_target == THERMAL_NO_TARGET && |
| 154 | instance->target != THERMAL_NO_TARGET) |
| 155 | update_passive_instance(tz, trip_type, 1); |
| 156 | /* Deactivate a passive thermal instance */ |
| 157 | else if (old_target != THERMAL_NO_TARGET && |
| 158 | instance->target == THERMAL_NO_TARGET) |
| 159 | update_passive_instance(tz, trip_type, -1); |
| 160 | |
Zhang Rui | bb431ba | 2015-10-30 16:31:47 +0800 | [diff] [blame] | 161 | instance->initialized = true; |
Michele Di Giorgio | d0b7306 | 2016-06-02 15:25:31 +0100 | [diff] [blame] | 162 | mutex_lock(&instance->cdev->lock); |
Zhang Rui | b8bb6cb | 2012-11-22 15:45:02 +0800 | [diff] [blame] | 163 | instance->cdev->updated = false; /* cdev needs update */ |
Michele Di Giorgio | d0b7306 | 2016-06-02 15:25:31 +0100 | [diff] [blame] | 164 | mutex_unlock(&instance->cdev->lock); |
Zhang Rui | b8bb6cb | 2012-11-22 15:45:02 +0800 | [diff] [blame] | 165 | } |
Durgadoss R | e151a20 | 2012-09-21 12:06:04 +0530 | [diff] [blame] | 166 | |
| 167 | mutex_unlock(&tz->lock); |
| 168 | } |
| 169 | |
| 170 | /** |
Brian Norris | 56b613e | 2015-01-29 09:57:21 -0800 | [diff] [blame] | 171 | * step_wise_throttle - throttles devices associated with the given zone |
Amit Kucheria | 53d256e | 2019-11-20 21:15:12 +0530 | [diff] [blame] | 172 | * @tz: thermal_zone_device |
| 173 | * @trip: trip point index |
Durgadoss R | e151a20 | 2012-09-21 12:06:04 +0530 | [diff] [blame] | 174 | * |
| 175 | * Throttling Logic: This uses the trend of the thermal zone to throttle. |
| 176 | * If the thermal zone is 'heating up' this throttles all the cooling |
| 177 | * devices associated with the zone and its particular trip point, by one |
| 178 | * step. If the zone is 'cooling down' it brings back the performance of |
| 179 | * the devices by one step. |
| 180 | */ |
Sachin Kamat | b88a497 | 2012-09-27 16:28:12 +0530 | [diff] [blame] | 181 | static int step_wise_throttle(struct thermal_zone_device *tz, int trip) |
Durgadoss R | e151a20 | 2012-09-21 12:06:04 +0530 | [diff] [blame] | 182 | { |
| 183 | struct thermal_instance *instance; |
| 184 | |
| 185 | thermal_zone_trip_update(tz, trip); |
| 186 | |
Durgadoss R | e151a20 | 2012-09-21 12:06:04 +0530 | [diff] [blame] | 187 | mutex_lock(&tz->lock); |
| 188 | |
| 189 | list_for_each_entry(instance, &tz->thermal_instances, tz_node) |
| 190 | thermal_cdev_update(instance->cdev); |
| 191 | |
| 192 | mutex_unlock(&tz->lock); |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
Sachin Kamat | b88a497 | 2012-09-27 16:28:12 +0530 | [diff] [blame] | 197 | static struct thermal_governor thermal_gov_step_wise = { |
Zhang Rui | 1f53ef1 | 2012-12-12 15:31:37 +0800 | [diff] [blame] | 198 | .name = "step_wise", |
Durgadoss R | e151a20 | 2012-09-21 12:06:04 +0530 | [diff] [blame] | 199 | .throttle = step_wise_throttle, |
Durgadoss R | e151a20 | 2012-09-21 12:06:04 +0530 | [diff] [blame] | 200 | }; |
Daniel Lezcano | 57c5b2e | 2019-06-12 22:13:25 +0200 | [diff] [blame] | 201 | THERMAL_GOVERNOR_DECLARE(thermal_gov_step_wise); |