Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  fair_share.c - A simple weight based Thermal governor
0004  *
0005  *  Copyright (C) 2012 Intel Corp
0006  *  Copyright (C) 2012 Durgadoss R <durgadoss.r@intel.com>
0007  *
0008  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0009  *
0010  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0011  */
0012 
0013 #include <linux/thermal.h>
0014 #include <trace/events/thermal.h>
0015 
0016 #include "thermal_core.h"
0017 
0018 /**
0019  * get_trip_level: - obtains the current trip level for a zone
0020  * @tz:     thermal zone device
0021  */
0022 static int get_trip_level(struct thermal_zone_device *tz)
0023 {
0024     int count = 0;
0025     int trip_temp;
0026     enum thermal_trip_type trip_type;
0027 
0028     if (tz->num_trips == 0 || !tz->ops->get_trip_temp)
0029         return 0;
0030 
0031     for (count = 0; count < tz->num_trips; count++) {
0032         tz->ops->get_trip_temp(tz, count, &trip_temp);
0033         if (tz->temperature < trip_temp)
0034             break;
0035     }
0036 
0037     /*
0038      * count > 0 only if temperature is greater than first trip
0039      * point, in which case, trip_point = count - 1
0040      */
0041     if (count > 0) {
0042         tz->ops->get_trip_type(tz, count - 1, &trip_type);
0043         trace_thermal_zone_trip(tz, count - 1, trip_type);
0044     }
0045 
0046     return count;
0047 }
0048 
0049 static long get_target_state(struct thermal_zone_device *tz,
0050         struct thermal_cooling_device *cdev, int percentage, int level)
0051 {
0052     unsigned long max_state;
0053 
0054     cdev->ops->get_max_state(cdev, &max_state);
0055 
0056     return (long)(percentage * level * max_state) / (100 * tz->num_trips);
0057 }
0058 
0059 /**
0060  * fair_share_throttle - throttles devices associated with the given zone
0061  * @tz: thermal_zone_device
0062  * @trip: trip point index
0063  *
0064  * Throttling Logic: This uses three parameters to calculate the new
0065  * throttle state of the cooling devices associated with the given zone.
0066  *
0067  * Parameters used for Throttling:
0068  * P1. max_state: Maximum throttle state exposed by the cooling device.
0069  * P2. percentage[i]/100:
0070  *  How 'effective' the 'i'th device is, in cooling the given zone.
0071  * P3. cur_trip_level/max_no_of_trips:
0072  *  This describes the extent to which the devices should be throttled.
0073  *  We do not want to throttle too much when we trip a lower temperature,
0074  *  whereas the throttling is at full swing if we trip critical levels.
0075  *  (Heavily assumes the trip points are in ascending order)
0076  * new_state of cooling device = P3 * P2 * P1
0077  */
0078 static int fair_share_throttle(struct thermal_zone_device *tz, int trip)
0079 {
0080     struct thermal_instance *instance;
0081     int total_weight = 0;
0082     int total_instance = 0;
0083     int cur_trip_level = get_trip_level(tz);
0084 
0085     mutex_lock(&tz->lock);
0086 
0087     list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
0088         if (instance->trip != trip)
0089             continue;
0090 
0091         total_weight += instance->weight;
0092         total_instance++;
0093     }
0094 
0095     list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
0096         int percentage;
0097         struct thermal_cooling_device *cdev = instance->cdev;
0098 
0099         if (instance->trip != trip)
0100             continue;
0101 
0102         if (!total_weight)
0103             percentage = 100 / total_instance;
0104         else
0105             percentage = (instance->weight * 100) / total_weight;
0106 
0107         instance->target = get_target_state(tz, cdev, percentage,
0108                             cur_trip_level);
0109 
0110         mutex_lock(&cdev->lock);
0111         __thermal_cdev_update(cdev);
0112         mutex_unlock(&cdev->lock);
0113     }
0114 
0115     mutex_unlock(&tz->lock);
0116     return 0;
0117 }
0118 
0119 static struct thermal_governor thermal_gov_fair_share = {
0120     .name       = "fair_share",
0121     .throttle   = fair_share_throttle,
0122 };
0123 THERMAL_GOVERNOR_DECLARE(thermal_gov_fair_share);