Linux 4.19.133
[linux/fpc-iii.git] / drivers / thermal / fair_share.c
blobd3469fbc52072d755899e7e9b669f587f2e8af2e
1 /*
2 * fair_share.c - A simple weight based Thermal governor
4 * Copyright (C) 2012 Intel Corp
5 * Copyright (C) 2012 Durgadoss R <durgadoss.r@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 #include <linux/thermal.h>
26 #include <trace/events/thermal.h>
28 #include "thermal_core.h"
30 /**
31 * get_trip_level: - obtains the current trip level for a zone
32 * @tz: thermal zone device
34 static int get_trip_level(struct thermal_zone_device *tz)
36 int count = 0;
37 int trip_temp;
38 enum thermal_trip_type trip_type;
40 if (tz->trips == 0 || !tz->ops->get_trip_temp)
41 return 0;
43 for (count = 0; count < tz->trips; count++) {
44 tz->ops->get_trip_temp(tz, count, &trip_temp);
45 if (tz->temperature < trip_temp)
46 break;
50 * count > 0 only if temperature is greater than first trip
51 * point, in which case, trip_point = count - 1
53 if (count > 0) {
54 tz->ops->get_trip_type(tz, count - 1, &trip_type);
55 trace_thermal_zone_trip(tz, count - 1, trip_type);
58 return count;
61 static long get_target_state(struct thermal_zone_device *tz,
62 struct thermal_cooling_device *cdev, int percentage, int level)
64 unsigned long max_state;
66 cdev->ops->get_max_state(cdev, &max_state);
68 return (long)(percentage * level * max_state) / (100 * tz->trips);
71 /**
72 * fair_share_throttle - throttles devices associated with the given zone
73 * @tz - thermal_zone_device
74 * @trip - trip point index
76 * Throttling Logic: This uses three parameters to calculate the new
77 * throttle state of the cooling devices associated with the given zone.
79 * Parameters used for Throttling:
80 * P1. max_state: Maximum throttle state exposed by the cooling device.
81 * P2. percentage[i]/100:
82 * How 'effective' the 'i'th device is, in cooling the given zone.
83 * P3. cur_trip_level/max_no_of_trips:
84 * This describes the extent to which the devices should be throttled.
85 * We do not want to throttle too much when we trip a lower temperature,
86 * whereas the throttling is at full swing if we trip critical levels.
87 * (Heavily assumes the trip points are in ascending order)
88 * new_state of cooling device = P3 * P2 * P1
90 static int fair_share_throttle(struct thermal_zone_device *tz, int trip)
92 struct thermal_instance *instance;
93 int total_weight = 0;
94 int total_instance = 0;
95 int cur_trip_level = get_trip_level(tz);
97 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
98 if (instance->trip != trip)
99 continue;
101 total_weight += instance->weight;
102 total_instance++;
105 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
106 int percentage;
107 struct thermal_cooling_device *cdev = instance->cdev;
109 if (instance->trip != trip)
110 continue;
112 if (!total_weight)
113 percentage = 100 / total_instance;
114 else
115 percentage = (instance->weight * 100) / total_weight;
117 instance->target = get_target_state(tz, cdev, percentage,
118 cur_trip_level);
120 mutex_lock(&instance->cdev->lock);
121 instance->cdev->updated = false;
122 mutex_unlock(&instance->cdev->lock);
123 thermal_cdev_update(cdev);
125 return 0;
128 static struct thermal_governor thermal_gov_fair_share = {
129 .name = "fair_share",
130 .throttle = fair_share_throttle,
133 int thermal_gov_fair_share_register(void)
135 return thermal_register_governor(&thermal_gov_fair_share);
138 void thermal_gov_fair_share_unregister(void)
140 thermal_unregister_governor(&thermal_gov_fair_share);