blob: d3469fbc52072d755899e7e9b669f587f2e8af2e [file] [log] [blame]
Durgadoss R4ccc57432012-09-18 11:05:01 +05301/*
2 * fair_share.c - A simple weight based Thermal governor
3 *
4 * Copyright (C) 2012 Intel Corp
5 * Copyright (C) 2012 Durgadoss R <durgadoss.r@intel.com>
6 *
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
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.
12 *
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.
17 *
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.
21 *
22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 */
24
Durgadoss R4ccc57432012-09-18 11:05:01 +053025#include <linux/thermal.h>
Punit Agrawal208cd822014-07-29 11:50:50 +010026#include <trace/events/thermal.h>
Durgadoss R4ccc57432012-09-18 11:05:01 +053027
28#include "thermal_core.h"
29
30/**
31 * get_trip_level: - obtains the current trip level for a zone
32 * @tz: thermal zone device
33 */
34static int get_trip_level(struct thermal_zone_device *tz)
35{
36 int count = 0;
Sascha Hauer17e83512015-07-24 08:12:54 +020037 int trip_temp;
Punit Agrawal208cd822014-07-29 11:50:50 +010038 enum thermal_trip_type trip_type;
Durgadoss R4ccc57432012-09-18 11:05:01 +053039
40 if (tz->trips == 0 || !tz->ops->get_trip_temp)
41 return 0;
42
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;
47 }
Punit Agrawal208cd822014-07-29 11:50:50 +010048
49 /*
50 * count > 0 only if temperature is greater than first trip
51 * point, in which case, trip_point = count - 1
52 */
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);
56 }
57
Durgadoss R4ccc57432012-09-18 11:05:01 +053058 return count;
59}
60
61static long get_target_state(struct thermal_zone_device *tz,
Javi Merinobcdcbbc2015-02-18 16:04:25 +000062 struct thermal_cooling_device *cdev, int percentage, int level)
Durgadoss R4ccc57432012-09-18 11:05:01 +053063{
64 unsigned long max_state;
65
66 cdev->ops->get_max_state(cdev, &max_state);
67
Javi Merinobcdcbbc2015-02-18 16:04:25 +000068 return (long)(percentage * level * max_state) / (100 * tz->trips);
Durgadoss R4ccc57432012-09-18 11:05:01 +053069}
70
71/**
Javi Merino80b89172015-02-18 16:04:23 +000072 * fair_share_throttle - throttles devices associated with the given zone
Durgadoss R4ccc57432012-09-18 11:05:01 +053073 * @tz - thermal_zone_device
Willy WOLFF0d76d6e12017-06-24 14:06:03 +010074 * @trip - trip point index
Durgadoss R4ccc57432012-09-18 11:05:01 +053075 *
76 * Throttling Logic: This uses three parameters to calculate the new
77 * throttle state of the cooling devices associated with the given zone.
78 *
79 * Parameters used for Throttling:
80 * P1. max_state: Maximum throttle state exposed by the cooling device.
Javi Merinobcdcbbc2015-02-18 16:04:25 +000081 * P2. percentage[i]/100:
Durgadoss R4ccc57432012-09-18 11:05:01 +053082 * 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
89 */
Sachin Kamat62176932012-09-27 16:57:53 +053090static int fair_share_throttle(struct thermal_zone_device *tz, int trip)
Durgadoss R4ccc57432012-09-18 11:05:01 +053091{
Durgadoss R4ccc57432012-09-18 11:05:01 +053092 struct thermal_instance *instance;
Javi Merinobcdcbbc2015-02-18 16:04:25 +000093 int total_weight = 0;
94 int total_instance = 0;
Durgadoss R4ccc57432012-09-18 11:05:01 +053095 int cur_trip_level = get_trip_level(tz);
96
Javi Merino8b91e2c2015-02-18 16:04:22 +000097 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
Javi Merinobcdcbbc2015-02-18 16:04:25 +000098 if (instance->trip != trip)
99 continue;
100
101 total_weight += instance->weight;
102 total_instance++;
103 }
104
105 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
106 int percentage;
Javi Merino8b91e2c2015-02-18 16:04:22 +0000107 struct thermal_cooling_device *cdev = instance->cdev;
Durgadoss R4ccc57432012-09-18 11:05:01 +0530108
Javi Merino8b91e2c2015-02-18 16:04:22 +0000109 if (instance->trip != trip)
Durgadoss R4ccc57432012-09-18 11:05:01 +0530110 continue;
111
Javi Merinobcdcbbc2015-02-18 16:04:25 +0000112 if (!total_weight)
113 percentage = 100 / total_instance;
114 else
115 percentage = (instance->weight * 100) / total_weight;
116
117 instance->target = get_target_state(tz, cdev, percentage,
118 cur_trip_level);
Durgadoss R4ccc57432012-09-18 11:05:01 +0530119
Michele Di Giorgiod0b73062016-06-02 15:25:31 +0100120 mutex_lock(&instance->cdev->lock);
Durgadoss R4ccc57432012-09-18 11:05:01 +0530121 instance->cdev->updated = false;
Michele Di Giorgiod0b73062016-06-02 15:25:31 +0100122 mutex_unlock(&instance->cdev->lock);
Durgadoss R4ccc57432012-09-18 11:05:01 +0530123 thermal_cdev_update(cdev);
124 }
125 return 0;
126}
127
Sachin Kamat62176932012-09-27 16:57:53 +0530128static struct thermal_governor thermal_gov_fair_share = {
Durgadoss R4ccc57432012-09-18 11:05:01 +0530129 .name = "fair_share",
130 .throttle = fair_share_throttle,
Durgadoss R4ccc57432012-09-18 11:05:01 +0530131};
132
Zhang Rui80a26a52013-03-26 16:38:29 +0800133int thermal_gov_fair_share_register(void)
Durgadoss R4ccc57432012-09-18 11:05:01 +0530134{
135 return thermal_register_governor(&thermal_gov_fair_share);
136}
137
Zhang Rui80a26a52013-03-26 16:38:29 +0800138void thermal_gov_fair_share_unregister(void)
Durgadoss R4ccc57432012-09-18 11:05:01 +0530139{
140 thermal_unregister_governor(&thermal_gov_fair_share);
141}
142