xref: /linux/drivers/thermal/gov_step_wise.c (revision 7954c92ede882b0dfd52a5db90291a4151b44c1a)
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 "thermal_trace.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  * If the temperature is lower than a trip point,
25  *    a. if the trend is THERMAL_TREND_RAISING, do nothing
26  *    b. if the trend is THERMAL_TREND_DROPPING, use lower cooling
27  *       state for this trip point, if the cooling state already
28  *       equals lower limit, deactivate the thermal instance
29  */
30 static unsigned long get_target_state(struct thermal_instance *instance,
31 				enum thermal_trend trend, bool throttle)
32 {
33 	struct thermal_cooling_device *cdev = instance->cdev;
34 	unsigned long cur_state;
35 
36 	/*
37 	 * We keep this instance the way it is by default.
38 	 * Otherwise, we use the current state of the
39 	 * cdev in use to determine the next_target.
40 	 */
41 	cdev->ops->get_cur_state(cdev, &cur_state);
42 	dev_dbg(&cdev->device, "cur_state=%ld\n", cur_state);
43 
44 	if (!instance->initialized) {
45 		if (throttle)
46 			return clamp(cur_state + 1, instance->lower, instance->upper);
47 
48 		return THERMAL_NO_TARGET;
49 	}
50 
51 	if (throttle) {
52 		if (trend == THERMAL_TREND_RAISING)
53 			return clamp(cur_state + 1, instance->lower, instance->upper);
54 	} else if (trend == THERMAL_TREND_DROPPING) {
55 		if (cur_state <= instance->lower)
56 			return THERMAL_NO_TARGET;
57 
58 		return clamp(cur_state - 1, instance->lower, instance->upper);
59 	}
60 
61 	return instance->target;
62 }
63 
64 static void thermal_zone_trip_update(struct thermal_zone_device *tz,
65 				     const struct thermal_trip *trip)
66 {
67 	int trip_id = thermal_zone_trip_id(tz, trip);
68 	enum thermal_trend trend;
69 	struct thermal_instance *instance;
70 	bool throttle = false;
71 	int old_target;
72 
73 	trend = get_tz_trend(tz, trip);
74 
75 	if (tz->temperature >= trip->temperature) {
76 		throttle = true;
77 		trace_thermal_zone_trip(tz, trip_id, trip->type);
78 	}
79 
80 	dev_dbg(&tz->device, "Trip%d[type=%d,temp=%d]:trend=%d,throttle=%d\n",
81 		trip_id, trip->type, trip->temperature, trend, throttle);
82 
83 	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
84 		if (instance->trip != trip)
85 			continue;
86 
87 		old_target = instance->target;
88 		instance->target = get_target_state(instance, trend, throttle);
89 		dev_dbg(&instance->cdev->device, "old_target=%d, target=%d\n",
90 					old_target, (int)instance->target);
91 
92 		if (instance->initialized && old_target == instance->target)
93 			continue;
94 
95 		if (trip->type == THERMAL_TRIP_PASSIVE) {
96 			/* If needed, update the status of passive polling. */
97 			if (old_target == THERMAL_NO_TARGET &&
98 			    instance->target != THERMAL_NO_TARGET)
99 				tz->passive++;
100 			else if (old_target != THERMAL_NO_TARGET &&
101 				 instance->target == THERMAL_NO_TARGET)
102 				tz->passive--;
103 		}
104 
105 		instance->initialized = true;
106 		mutex_lock(&instance->cdev->lock);
107 		instance->cdev->updated = false; /* cdev needs update */
108 		mutex_unlock(&instance->cdev->lock);
109 	}
110 }
111 
112 /**
113  * step_wise_throttle - throttles devices associated with the given zone
114  * @tz: thermal_zone_device
115  * @trip: trip point
116  *
117  * Throttling Logic: This uses the trend of the thermal zone to throttle.
118  * If the thermal zone is 'heating up' this throttles all the cooling
119  * devices associated with the zone and its particular trip point, by one
120  * step. If the zone is 'cooling down' it brings back the performance of
121  * the devices by one step.
122  */
123 static int step_wise_throttle(struct thermal_zone_device *tz,
124 			      const struct thermal_trip *trip)
125 {
126 	struct thermal_instance *instance;
127 
128 	lockdep_assert_held(&tz->lock);
129 
130 	thermal_zone_trip_update(tz, trip);
131 
132 	list_for_each_entry(instance, &tz->thermal_instances, tz_node)
133 		thermal_cdev_update(instance->cdev);
134 
135 	return 0;
136 }
137 
138 static struct thermal_governor thermal_gov_step_wise = {
139 	.name		= "step_wise",
140 	.throttle	= step_wise_throttle,
141 };
142 THERMAL_GOVERNOR_DECLARE(thermal_gov_step_wise);
143