OSDN Git Service

powerpc/configs/6s: Drop obsolete crypto ALGs
[tomoyo/tomoyo-test1.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_id)
99 {
100         enum thermal_trend trend;
101         struct thermal_instance *instance;
102         struct thermal_trip trip;
103         bool throttle = false;
104         int old_target;
105
106         __thermal_zone_get_trip(tz, trip_id, &trip);
107
108         trend = get_tz_trend(tz, trip_id);
109
110         if (tz->temperature >= trip.temperature) {
111                 throttle = true;
112                 trace_thermal_zone_trip(tz, trip_id, trip.type);
113         }
114
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);
117
118         list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
119                 if (instance->trip != trip_id)
120                         continue;
121
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);
126
127                 if (instance->initialized && old_target == instance->target)
128                         continue;
129
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);
138
139                 instance->initialized = true;
140                 mutex_lock(&instance->cdev->lock);
141                 instance->cdev->updated = false; /* cdev needs update */
142                 mutex_unlock(&instance->cdev->lock);
143         }
144 }
145
146 /**
147  * step_wise_throttle - throttles devices associated with the given zone
148  * @tz: thermal_zone_device
149  * @trip: trip point index
150  *
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.
156  */
157 static int step_wise_throttle(struct thermal_zone_device *tz, int trip)
158 {
159         struct thermal_instance *instance;
160
161         lockdep_assert_held(&tz->lock);
162
163         thermal_zone_trip_update(tz, trip);
164
165         list_for_each_entry(instance, &tz->thermal_instances, tz_node)
166                 thermal_cdev_update(instance->cdev);
167
168         return 0;
169 }
170
171 static struct thermal_governor thermal_gov_step_wise = {
172         .name           = "step_wise",
173         .throttle       = step_wise_throttle,
174 };
175 THERMAL_GOVERNOR_DECLARE(thermal_gov_step_wise);