1 /*
2  *  step_wise.c - A step-by-step Thermal throttling governor
3  *
4  *  Copyright (C) 2012 Intel Corp
5  *  Copyright (C) 2012 Durgadoss R <durgadoss.r@intel.com>
6  *
7  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; version 2 of the License.
12  *
13  *  This program is distributed in the hope that it will be useful, but
14  *  WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *  General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License along
19  *  with this program; if not, write to the Free Software Foundation, Inc.,
20  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21  *
22  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23  */
24 
25 #include <linux/thermal.h>
26 #include <trace/events/thermal.h>
27 
28 #include "thermal_core.h"
29 
30 /*
31  * If the temperature is higher than a trip point,
32  *    a. if the trend is THERMAL_TREND_RAISING, use higher cooling
33  *       state for this trip point
34  *    b. if the trend is THERMAL_TREND_DROPPING, do nothing
35  *    c. if the trend is THERMAL_TREND_RAISE_FULL, use upper limit
36  *       for this trip point
37  *    d. if the trend is THERMAL_TREND_DROP_FULL, use lower limit
38  *       for this trip point
39  * If the temperature is lower than a trip point,
40  *    a. if the trend is THERMAL_TREND_RAISING, do nothing
41  *    b. if the trend is THERMAL_TREND_DROPPING, use lower cooling
42  *       state for this trip point, if the cooling state already
43  *       equals lower limit, deactivate the thermal instance
44  *    c. if the trend is THERMAL_TREND_RAISE_FULL, do nothing
45  *    d. if the trend is THERMAL_TREND_DROP_FULL, use lower limit,
46  *       if the cooling state already equals lower limit,
47  *       deactivate the thermal instance
48  */
get_target_state(struct thermal_instance * instance,enum thermal_trend trend,bool throttle)49 static unsigned long get_target_state(struct thermal_instance *instance,
50 				enum thermal_trend trend, bool throttle)
51 {
52 	struct thermal_cooling_device *cdev = instance->cdev;
53 	unsigned long cur_state;
54 	unsigned long next_target;
55 
56 	/*
57 	 * We keep this instance the way it is by default.
58 	 * Otherwise, we use the current state of the
59 	 * cdev in use to determine the next_target.
60 	 */
61 	cdev->ops->get_cur_state(cdev, &cur_state);
62 	next_target = instance->target;
63 	dev_dbg(&cdev->device, "cur_state=%ld\n", cur_state);
64 
65 	if (!instance->initialized) {
66 		if (throttle) {
67 			next_target = (cur_state + 1) >= instance->upper ?
68 					instance->upper :
69 					((cur_state + 1) < instance->lower ?
70 					instance->lower : (cur_state + 1));
71 		} else {
72 			next_target = THERMAL_NO_TARGET;
73 		}
74 
75 		return next_target;
76 	}
77 
78 	switch (trend) {
79 	case THERMAL_TREND_RAISING:
80 		if (throttle) {
81 			next_target = cur_state < instance->upper ?
82 				    (cur_state + 1) : instance->upper;
83 			if (next_target < instance->lower)
84 				next_target = instance->lower;
85 		}
86 		break;
87 	case THERMAL_TREND_RAISE_FULL:
88 		if (throttle)
89 			next_target = instance->upper;
90 		break;
91 	case THERMAL_TREND_DROPPING:
92 		if (cur_state <= instance->lower) {
93 			if (!throttle)
94 				next_target = THERMAL_NO_TARGET;
95 		} else {
96 			if (!throttle) {
97 				next_target = cur_state - 1;
98 				if (next_target > instance->upper)
99 					next_target = instance->upper;
100 			}
101 		}
102 		break;
103 	case THERMAL_TREND_DROP_FULL:
104 		if (cur_state == instance->lower) {
105 			if (!throttle)
106 				next_target = THERMAL_NO_TARGET;
107 		} else
108 			next_target = instance->lower;
109 		break;
110 	default:
111 		break;
112 	}
113 
114 	return next_target;
115 }
116 
update_passive_instance(struct thermal_zone_device * tz,enum thermal_trip_type type,int value)117 static void update_passive_instance(struct thermal_zone_device *tz,
118 				enum thermal_trip_type type, int value)
119 {
120 	/*
121 	 * If value is +1, activate a passive instance.
122 	 * If value is -1, deactivate a passive instance.
123 	 */
124 	if (type == THERMAL_TRIP_PASSIVE || type == THERMAL_TRIPS_NONE)
125 		tz->passive += value;
126 }
127 
thermal_zone_trip_update(struct thermal_zone_device * tz,int trip)128 static void thermal_zone_trip_update(struct thermal_zone_device *tz, int trip)
129 {
130 	int trip_temp;
131 	enum thermal_trip_type trip_type;
132 	enum thermal_trend trend;
133 	struct thermal_instance *instance;
134 	bool throttle = false;
135 	int old_target;
136 
137 	if (trip == THERMAL_TRIPS_NONE) {
138 		trip_temp = tz->forced_passive;
139 		trip_type = THERMAL_TRIPS_NONE;
140 	} else {
141 		tz->ops->get_trip_temp(tz, trip, &trip_temp);
142 		tz->ops->get_trip_type(tz, trip, &trip_type);
143 	}
144 
145 	trend = get_tz_trend(tz, trip);
146 
147 	if (tz->temperature >= trip_temp) {
148 		throttle = true;
149 		trace_thermal_zone_trip(tz, trip, trip_type);
150 	}
151 
152 	dev_dbg(&tz->device, "Trip%d[type=%d,temp=%d]:trend=%d,throttle=%d\n",
153 				trip, trip_type, trip_temp, trend, throttle);
154 
155 	mutex_lock(&tz->lock);
156 
157 	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
158 		if (instance->trip != trip)
159 			continue;
160 
161 		old_target = instance->target;
162 		instance->target = get_target_state(instance, trend, throttle);
163 		dev_dbg(&instance->cdev->device, "old_target=%d, target=%d\n",
164 					old_target, (int)instance->target);
165 
166 		if (instance->initialized && old_target == instance->target)
167 			continue;
168 
169 		/* Activate a passive thermal instance */
170 		if (old_target == THERMAL_NO_TARGET &&
171 			instance->target != THERMAL_NO_TARGET)
172 			update_passive_instance(tz, trip_type, 1);
173 		/* Deactivate a passive thermal instance */
174 		else if (old_target != THERMAL_NO_TARGET &&
175 			instance->target == THERMAL_NO_TARGET)
176 			update_passive_instance(tz, trip_type, -1);
177 
178 		instance->initialized = true;
179 		mutex_lock(&instance->cdev->lock);
180 		instance->cdev->updated = false; /* cdev needs update */
181 		mutex_unlock(&instance->cdev->lock);
182 	}
183 
184 	mutex_unlock(&tz->lock);
185 }
186 
187 /**
188  * step_wise_throttle - throttles devices associated with the given zone
189  * @tz - thermal_zone_device
190  * @trip - trip point index
191  *
192  * Throttling Logic: This uses the trend of the thermal zone to throttle.
193  * If the thermal zone is 'heating up' this throttles all the cooling
194  * devices associated with the zone and its particular trip point, by one
195  * step. If the zone is 'cooling down' it brings back the performance of
196  * the devices by one step.
197  */
step_wise_throttle(struct thermal_zone_device * tz,int trip)198 static int step_wise_throttle(struct thermal_zone_device *tz, int trip)
199 {
200 	struct thermal_instance *instance;
201 
202 	thermal_zone_trip_update(tz, trip);
203 
204 	if (tz->forced_passive)
205 		thermal_zone_trip_update(tz, THERMAL_TRIPS_NONE);
206 
207 	mutex_lock(&tz->lock);
208 
209 	list_for_each_entry(instance, &tz->thermal_instances, tz_node)
210 		thermal_cdev_update(instance->cdev);
211 
212 	mutex_unlock(&tz->lock);
213 
214 	return 0;
215 }
216 
217 static struct thermal_governor thermal_gov_step_wise = {
218 	.name		= "step_wise",
219 	.throttle	= step_wise_throttle,
220 };
221 
thermal_gov_step_wise_register(void)222 int thermal_gov_step_wise_register(void)
223 {
224 	return thermal_register_governor(&thermal_gov_step_wise);
225 }
226 
thermal_gov_step_wise_unregister(void)227 void thermal_gov_step_wise_unregister(void)
228 {
229 	thermal_unregister_governor(&thermal_gov_step_wise);
230 }
231