LCOV - code coverage report
Current view: top level - drivers/thermal - step_wise.c (source / functions) Hit Total Coverage
Test: gcov_data_raspi2_real_modules_combined.info Lines: 0 69 0.0 %
Date: 2020-09-30 20:25:40 Functions: 0 3 0.0 %
Branches: 0 65 0.0 %

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

Generated by: LCOV version 1.14