Merge tag 'for-6.1-rc3-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave...
[platform/kernel/linux-starfive.git] / drivers / thermal / gov_step_wise.c
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 <trace/events/thermal.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, do nothing
24  *    c. if the trend is THERMAL_TREND_RAISE_FULL, use upper limit
25  *       for this trip point
26  *    d. if the trend is THERMAL_TREND_DROP_FULL, use lower limit
27  *       for this trip point
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
37  */
38 static unsigned long get_target_state(struct thermal_instance *instance,
39                                 enum thermal_trend trend, bool throttle)
40 {
41         struct thermal_cooling_device *cdev = instance->cdev;
42         unsigned long cur_state;
43         unsigned long next_target;
44
45         /*
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.
49          */
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);
53
54         if (!instance->initialized) {
55                 if (throttle) {
56                         next_target = clamp((cur_state + 1), instance->lower, instance->upper);
57                 } else {
58                         next_target = THERMAL_NO_TARGET;
59                 }
60
61                 return next_target;
62         }
63
64         switch (trend) {
65         case THERMAL_TREND_RAISING:
66                 if (throttle) {
67                         next_target = clamp((cur_state + 1), instance->lower, instance->upper);
68                 }
69                 break;
70         case THERMAL_TREND_DROPPING:
71                 if (cur_state <= instance->lower) {
72                         if (!throttle)
73                                 next_target = THERMAL_NO_TARGET;
74                 } else {
75                         if (!throttle) {
76                                 next_target = clamp((cur_state - 1), instance->lower, instance->upper);
77                         }
78                 }
79                 break;
80         default:
81                 break;
82         }
83
84         return next_target;
85 }
86
87 static void update_passive_instance(struct thermal_zone_device *tz,
88                                 enum thermal_trip_type type, int value)
89 {
90         /*
91          * If value is +1, activate a passive instance.
92          * If value is -1, deactivate a passive instance.
93          */
94         if (type == THERMAL_TRIP_PASSIVE)
95                 tz->passive += value;
96 }
97
98 static void thermal_zone_trip_update(struct thermal_zone_device *tz, int trip)
99 {
100         int trip_temp;
101         enum thermal_trip_type trip_type;
102         enum thermal_trend trend;
103         struct thermal_instance *instance;
104         bool throttle = false;
105         int old_target;
106
107         tz->ops->get_trip_temp(tz, trip, &trip_temp);
108         tz->ops->get_trip_type(tz, trip, &trip_type);
109
110         trend = get_tz_trend(tz, trip);
111
112         if (tz->temperature >= trip_temp) {
113                 throttle = true;
114                 trace_thermal_zone_trip(tz, trip, trip_type);
115         }
116
117         dev_dbg(&tz->device, "Trip%d[type=%d,temp=%d]:trend=%d,throttle=%d\n",
118                                 trip, trip_type, trip_temp, trend, throttle);
119
120         list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
121                 if (instance->trip != trip)
122                         continue;
123
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);
128
129                 if (instance->initialized && old_target == instance->target)
130                         continue;
131
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);
140
141                 instance->initialized = true;
142                 mutex_lock(&instance->cdev->lock);
143                 instance->cdev->updated = false; /* cdev needs update */
144                 mutex_unlock(&instance->cdev->lock);
145         }
146 }
147
148 /**
149  * step_wise_throttle - throttles devices associated with the given zone
150  * @tz: thermal_zone_device
151  * @trip: trip point index
152  *
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.
158  */
159 static int step_wise_throttle(struct thermal_zone_device *tz, int trip)
160 {
161         struct thermal_instance *instance;
162
163         lockdep_assert_held(&tz->lock);
164
165         thermal_zone_trip_update(tz, trip);
166
167         list_for_each_entry(instance, &tz->thermal_instances, tz_node)
168                 thermal_cdev_update(instance->cdev);
169
170         return 0;
171 }
172
173 static struct thermal_governor thermal_gov_step_wise = {
174         .name           = "step_wise",
175         .throttle       = step_wise_throttle,
176 };
177 THERMAL_GOVERNOR_DECLARE(thermal_gov_step_wise);