Durgadoss R | 4ccc5743 | 2012-09-18 11:05:01 +0530 | [diff] [blame] | 1 | /* |
| 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 R | 4ccc5743 | 2012-09-18 11:05:01 +0530 | [diff] [blame] | 25 | #include <linux/thermal.h> |
Punit Agrawal | 208cd82 | 2014-07-29 11:50:50 +0100 | [diff] [blame] | 26 | #include <trace/events/thermal.h> |
Durgadoss R | 4ccc5743 | 2012-09-18 11:05:01 +0530 | [diff] [blame] | 27 | |
| 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 | */ |
| 34 | static int get_trip_level(struct thermal_zone_device *tz) |
| 35 | { |
| 36 | int count = 0; |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 37 | int trip_temp; |
Punit Agrawal | 208cd82 | 2014-07-29 11:50:50 +0100 | [diff] [blame] | 38 | enum thermal_trip_type trip_type; |
Durgadoss R | 4ccc5743 | 2012-09-18 11:05:01 +0530 | [diff] [blame] | 39 | |
| 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 Agrawal | 208cd82 | 2014-07-29 11:50:50 +0100 | [diff] [blame] | 48 | |
| 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 R | 4ccc5743 | 2012-09-18 11:05:01 +0530 | [diff] [blame] | 58 | return count; |
| 59 | } |
| 60 | |
| 61 | static long get_target_state(struct thermal_zone_device *tz, |
Javi Merino | bcdcbbc | 2015-02-18 16:04:25 +0000 | [diff] [blame] | 62 | struct thermal_cooling_device *cdev, int percentage, int level) |
Durgadoss R | 4ccc5743 | 2012-09-18 11:05:01 +0530 | [diff] [blame] | 63 | { |
| 64 | unsigned long max_state; |
| 65 | |
| 66 | cdev->ops->get_max_state(cdev, &max_state); |
| 67 | |
Javi Merino | bcdcbbc | 2015-02-18 16:04:25 +0000 | [diff] [blame] | 68 | return (long)(percentage * level * max_state) / (100 * tz->trips); |
Durgadoss R | 4ccc5743 | 2012-09-18 11:05:01 +0530 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
Javi Merino | 80b8917 | 2015-02-18 16:04:23 +0000 | [diff] [blame] | 72 | * fair_share_throttle - throttles devices associated with the given zone |
Durgadoss R | 4ccc5743 | 2012-09-18 11:05:01 +0530 | [diff] [blame] | 73 | * @tz - thermal_zone_device |
| 74 | * |
| 75 | * Throttling Logic: This uses three parameters to calculate the new |
| 76 | * throttle state of the cooling devices associated with the given zone. |
| 77 | * |
| 78 | * Parameters used for Throttling: |
| 79 | * P1. max_state: Maximum throttle state exposed by the cooling device. |
Javi Merino | bcdcbbc | 2015-02-18 16:04:25 +0000 | [diff] [blame] | 80 | * P2. percentage[i]/100: |
Durgadoss R | 4ccc5743 | 2012-09-18 11:05:01 +0530 | [diff] [blame] | 81 | * How 'effective' the 'i'th device is, in cooling the given zone. |
| 82 | * P3. cur_trip_level/max_no_of_trips: |
| 83 | * This describes the extent to which the devices should be throttled. |
| 84 | * We do not want to throttle too much when we trip a lower temperature, |
| 85 | * whereas the throttling is at full swing if we trip critical levels. |
| 86 | * (Heavily assumes the trip points are in ascending order) |
| 87 | * new_state of cooling device = P3 * P2 * P1 |
| 88 | */ |
Sachin Kamat | 6217693 | 2012-09-27 16:57:53 +0530 | [diff] [blame] | 89 | static int fair_share_throttle(struct thermal_zone_device *tz, int trip) |
Durgadoss R | 4ccc5743 | 2012-09-18 11:05:01 +0530 | [diff] [blame] | 90 | { |
Durgadoss R | 4ccc5743 | 2012-09-18 11:05:01 +0530 | [diff] [blame] | 91 | struct thermal_instance *instance; |
Javi Merino | bcdcbbc | 2015-02-18 16:04:25 +0000 | [diff] [blame] | 92 | int total_weight = 0; |
| 93 | int total_instance = 0; |
Durgadoss R | 4ccc5743 | 2012-09-18 11:05:01 +0530 | [diff] [blame] | 94 | int cur_trip_level = get_trip_level(tz); |
| 95 | |
Javi Merino | 8b91e2c | 2015-02-18 16:04:22 +0000 | [diff] [blame] | 96 | list_for_each_entry(instance, &tz->thermal_instances, tz_node) { |
Javi Merino | bcdcbbc | 2015-02-18 16:04:25 +0000 | [diff] [blame] | 97 | if (instance->trip != trip) |
| 98 | continue; |
| 99 | |
| 100 | total_weight += instance->weight; |
| 101 | total_instance++; |
| 102 | } |
| 103 | |
| 104 | list_for_each_entry(instance, &tz->thermal_instances, tz_node) { |
| 105 | int percentage; |
Javi Merino | 8b91e2c | 2015-02-18 16:04:22 +0000 | [diff] [blame] | 106 | struct thermal_cooling_device *cdev = instance->cdev; |
Durgadoss R | 4ccc5743 | 2012-09-18 11:05:01 +0530 | [diff] [blame] | 107 | |
Javi Merino | 8b91e2c | 2015-02-18 16:04:22 +0000 | [diff] [blame] | 108 | if (instance->trip != trip) |
Durgadoss R | 4ccc5743 | 2012-09-18 11:05:01 +0530 | [diff] [blame] | 109 | continue; |
| 110 | |
Javi Merino | bcdcbbc | 2015-02-18 16:04:25 +0000 | [diff] [blame] | 111 | if (!total_weight) |
| 112 | percentage = 100 / total_instance; |
| 113 | else |
| 114 | percentage = (instance->weight * 100) / total_weight; |
| 115 | |
| 116 | instance->target = get_target_state(tz, cdev, percentage, |
| 117 | cur_trip_level); |
Durgadoss R | 4ccc5743 | 2012-09-18 11:05:01 +0530 | [diff] [blame] | 118 | |
| 119 | instance->cdev->updated = false; |
| 120 | thermal_cdev_update(cdev); |
| 121 | } |
| 122 | return 0; |
| 123 | } |
| 124 | |
Sachin Kamat | 6217693 | 2012-09-27 16:57:53 +0530 | [diff] [blame] | 125 | static struct thermal_governor thermal_gov_fair_share = { |
Durgadoss R | 4ccc5743 | 2012-09-18 11:05:01 +0530 | [diff] [blame] | 126 | .name = "fair_share", |
| 127 | .throttle = fair_share_throttle, |
Durgadoss R | 4ccc5743 | 2012-09-18 11:05:01 +0530 | [diff] [blame] | 128 | }; |
| 129 | |
Zhang Rui | 80a26a5 | 2013-03-26 16:38:29 +0800 | [diff] [blame] | 130 | int thermal_gov_fair_share_register(void) |
Durgadoss R | 4ccc5743 | 2012-09-18 11:05:01 +0530 | [diff] [blame] | 131 | { |
| 132 | return thermal_register_governor(&thermal_gov_fair_share); |
| 133 | } |
| 134 | |
Zhang Rui | 80a26a5 | 2013-03-26 16:38:29 +0800 | [diff] [blame] | 135 | void thermal_gov_fair_share_unregister(void) |
Durgadoss R | 4ccc5743 | 2012-09-18 11:05:01 +0530 | [diff] [blame] | 136 | { |
| 137 | thermal_unregister_governor(&thermal_gov_fair_share); |
| 138 | } |
| 139 | |