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