| 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, use a lower cooling state | 
|---|
| 24 | *       for this trip point, but keep the cooling state above the applicable | 
|---|
| 25 | *       minimum | 
|---|
| 26 | * If the temperature is lower than a trip point, | 
|---|
| 27 | *    a. if the trend is THERMAL_TREND_RAISING, do nothing | 
|---|
| 28 | *    b. if the trend is THERMAL_TREND_DROPPING, use the minimum applicable | 
|---|
| 29 | *       cooling state for this trip point, or if the cooling state already | 
|---|
| 30 | *       equals lower limit, deactivate the thermal instance | 
|---|
| 31 | */ | 
|---|
| 32 | static unsigned long get_target_state(struct thermal_instance *instance, | 
|---|
| 33 | enum thermal_trend trend, bool throttle) | 
|---|
| 34 | { | 
|---|
| 35 | struct thermal_cooling_device *cdev = instance->cdev; | 
|---|
| 36 | unsigned long cur_state; | 
|---|
| 37 |  | 
|---|
| 38 | /* | 
|---|
| 39 | * We keep this instance the way it is by default. | 
|---|
| 40 | * Otherwise, we use the current state of the | 
|---|
| 41 | * cdev in use to determine the next_target. | 
|---|
| 42 | */ | 
|---|
| 43 | cdev->ops->get_cur_state(cdev, &cur_state); | 
|---|
| 44 | dev_dbg(&cdev->device, "cur_state=%ld\n", cur_state); | 
|---|
| 45 |  | 
|---|
| 46 | if (!instance->initialized) { | 
|---|
| 47 | if (throttle) | 
|---|
| 48 | return clamp(cur_state + 1, instance->lower, instance->upper); | 
|---|
| 49 |  | 
|---|
| 50 | return THERMAL_NO_TARGET; | 
|---|
| 51 | } | 
|---|
| 52 |  | 
|---|
| 53 | if (throttle) { | 
|---|
| 54 | if (trend == THERMAL_TREND_RAISING) | 
|---|
| 55 | return clamp(cur_state + 1, instance->lower, instance->upper); | 
|---|
| 56 |  | 
|---|
| 57 | /* | 
|---|
| 58 | * If the zone temperature is falling, the cooling level can | 
|---|
| 59 | * be reduced, but it should still be above the lower state of | 
|---|
| 60 | * the given thermal instance to pull the temperature further | 
|---|
| 61 | * down. | 
|---|
| 62 | */ | 
|---|
| 63 | if (trend == THERMAL_TREND_DROPPING) | 
|---|
| 64 | return clamp(cur_state - 1, | 
|---|
| 65 | min(instance->lower + 1, instance->upper), | 
|---|
| 66 | instance->upper); | 
|---|
| 67 | } else if (trend == THERMAL_TREND_DROPPING) { | 
|---|
| 68 | if (cur_state <= instance->lower) | 
|---|
| 69 | return THERMAL_NO_TARGET; | 
|---|
| 70 |  | 
|---|
| 71 | /* | 
|---|
| 72 | * If 'throttle' is false, no mitigation is necessary, so | 
|---|
| 73 | * request the lower state for this instance. | 
|---|
| 74 | */ | 
|---|
| 75 | return instance->lower; | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | return instance->target; | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | static void thermal_zone_trip_update(struct thermal_zone_device *tz, | 
|---|
| 82 | const struct thermal_trip_desc *td, | 
|---|
| 83 | int trip_threshold) | 
|---|
| 84 | { | 
|---|
| 85 | bool throttle = tz->temperature >= trip_threshold; | 
|---|
| 86 | const struct thermal_trip *trip = &td->trip; | 
|---|
| 87 | enum thermal_trend trend = get_tz_trend(tz, trip); | 
|---|
| 88 | int trip_id = thermal_zone_trip_id(tz, trip); | 
|---|
| 89 | struct thermal_instance *instance; | 
|---|
| 90 |  | 
|---|
| 91 | if (throttle) | 
|---|
| 92 | trace_thermal_zone_trip(tz, trip: trip_id, trip_type: trip->type); | 
|---|
| 93 |  | 
|---|
| 94 | dev_dbg(&tz->device, "Trip%d[type=%d,temp=%d]:trend=%d,throttle=%d\n", | 
|---|
| 95 | trip_id, trip->type, trip_threshold, trend, throttle); | 
|---|
| 96 |  | 
|---|
| 97 | list_for_each_entry(instance, &td->thermal_instances, trip_node) { | 
|---|
| 98 | int old_target; | 
|---|
| 99 |  | 
|---|
| 100 | old_target = instance->target; | 
|---|
| 101 | instance->target = get_target_state(instance, trend, throttle); | 
|---|
| 102 |  | 
|---|
| 103 | dev_dbg(&instance->cdev->device, "old_target=%d, target=%ld\n", | 
|---|
| 104 | old_target, instance->target); | 
|---|
| 105 |  | 
|---|
| 106 | if (instance->initialized && old_target == instance->target) | 
|---|
| 107 | continue; | 
|---|
| 108 |  | 
|---|
| 109 | instance->initialized = true; | 
|---|
| 110 |  | 
|---|
| 111 | scoped_guard(cooling_dev, instance->cdev) { | 
|---|
| 112 | instance->cdev->updated = false; /* cdev needs update */ | 
|---|
| 113 | } | 
|---|
| 114 | } | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | static void step_wise_manage(struct thermal_zone_device *tz) | 
|---|
| 118 | { | 
|---|
| 119 | const struct thermal_trip_desc *td; | 
|---|
| 120 | struct thermal_instance *instance; | 
|---|
| 121 |  | 
|---|
| 122 | lockdep_assert_held(&tz->lock); | 
|---|
| 123 |  | 
|---|
| 124 | /* | 
|---|
| 125 | * Throttling Logic: Use the trend of the thermal zone to throttle. | 
|---|
| 126 | * If the thermal zone is 'heating up', throttle all of the cooling | 
|---|
| 127 | * devices associated with each trip point by one step. If the zone | 
|---|
| 128 | * is 'cooling down', it brings back the performance of the devices | 
|---|
| 129 | * by one step. | 
|---|
| 130 | */ | 
|---|
| 131 | for_each_trip_desc(tz, td) { | 
|---|
| 132 | const struct thermal_trip *trip = &td->trip; | 
|---|
| 133 |  | 
|---|
| 134 | if (trip->temperature == THERMAL_TEMP_INVALID || | 
|---|
| 135 | trip->type == THERMAL_TRIP_CRITICAL || | 
|---|
| 136 | trip->type == THERMAL_TRIP_HOT) | 
|---|
| 137 | continue; | 
|---|
| 138 |  | 
|---|
| 139 | thermal_zone_trip_update(tz, td, trip_threshold: td->threshold); | 
|---|
| 140 | } | 
|---|
| 141 |  | 
|---|
| 142 | for_each_trip_desc(tz, td) { | 
|---|
| 143 | list_for_each_entry(instance, &td->thermal_instances, trip_node) | 
|---|
| 144 | thermal_cdev_update(instance->cdev); | 
|---|
| 145 | } | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | static struct thermal_governor thermal_gov_step_wise = { | 
|---|
| 149 | .name	= "step_wise", | 
|---|
| 150 | .manage	= step_wise_manage, | 
|---|
| 151 | }; | 
|---|
| 152 | THERMAL_GOVERNOR_DECLARE(thermal_gov_step_wise); | 
|---|
| 153 |  | 
|---|