Commit | Line | Data |
---|---|---|
873e65bc | 1 | // SPDX-License-Identifier: GPL-2.0-only |
4ccc5743 D |
2 | /* |
3 | * fair_share.c - A simple weight based Thermal governor | |
4 | * | |
5 | * Copyright (C) 2012 Intel Corp | |
6 | * Copyright (C) 2012 Durgadoss R <durgadoss.r@intel.com> | |
7 | * | |
8 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
9 | * | |
4ccc5743 D |
10 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
11 | */ | |
12 | ||
4ccc5743 | 13 | #include <linux/thermal.h> |
208cd822 | 14 | #include <trace/events/thermal.h> |
4ccc5743 D |
15 | |
16 | #include "thermal_core.h" | |
17 | ||
18 | /** | |
19 | * get_trip_level: - obtains the current trip level for a zone | |
20 | * @tz: thermal zone device | |
21 | */ | |
22 | static int get_trip_level(struct thermal_zone_device *tz) | |
23 | { | |
24 | int count = 0; | |
17e8351a | 25 | int trip_temp; |
208cd822 | 26 | enum thermal_trip_type trip_type; |
4ccc5743 D |
27 | |
28 | if (tz->trips == 0 || !tz->ops->get_trip_temp) | |
29 | return 0; | |
30 | ||
31 | for (count = 0; count < tz->trips; count++) { | |
32 | tz->ops->get_trip_temp(tz, count, &trip_temp); | |
33 | if (tz->temperature < trip_temp) | |
34 | break; | |
35 | } | |
208cd822 PA |
36 | |
37 | /* | |
38 | * count > 0 only if temperature is greater than first trip | |
39 | * point, in which case, trip_point = count - 1 | |
40 | */ | |
41 | if (count > 0) { | |
42 | tz->ops->get_trip_type(tz, count - 1, &trip_type); | |
43 | trace_thermal_zone_trip(tz, count - 1, trip_type); | |
44 | } | |
45 | ||
4ccc5743 D |
46 | return count; |
47 | } | |
48 | ||
49 | static long get_target_state(struct thermal_zone_device *tz, | |
bcdcbbc7 | 50 | struct thermal_cooling_device *cdev, int percentage, int level) |
4ccc5743 D |
51 | { |
52 | unsigned long max_state; | |
53 | ||
54 | cdev->ops->get_max_state(cdev, &max_state); | |
55 | ||
bcdcbbc7 | 56 | return (long)(percentage * level * max_state) / (100 * tz->trips); |
4ccc5743 D |
57 | } |
58 | ||
59 | /** | |
80b89172 | 60 | * fair_share_throttle - throttles devices associated with the given zone |
53d256e7 AK |
61 | * @tz: thermal_zone_device |
62 | * @trip: trip point index | |
4ccc5743 D |
63 | * |
64 | * Throttling Logic: This uses three parameters to calculate the new | |
65 | * throttle state of the cooling devices associated with the given zone. | |
66 | * | |
67 | * Parameters used for Throttling: | |
68 | * P1. max_state: Maximum throttle state exposed by the cooling device. | |
bcdcbbc7 | 69 | * P2. percentage[i]/100: |
4ccc5743 D |
70 | * How 'effective' the 'i'th device is, in cooling the given zone. |
71 | * P3. cur_trip_level/max_no_of_trips: | |
72 | * This describes the extent to which the devices should be throttled. | |
73 | * We do not want to throttle too much when we trip a lower temperature, | |
74 | * whereas the throttling is at full swing if we trip critical levels. | |
75 | * (Heavily assumes the trip points are in ascending order) | |
76 | * new_state of cooling device = P3 * P2 * P1 | |
77 | */ | |
62176933 | 78 | static int fair_share_throttle(struct thermal_zone_device *tz, int trip) |
4ccc5743 | 79 | { |
4ccc5743 | 80 | struct thermal_instance *instance; |
bcdcbbc7 JM |
81 | int total_weight = 0; |
82 | int total_instance = 0; | |
4ccc5743 D |
83 | int cur_trip_level = get_trip_level(tz); |
84 | ||
fef05776 LL |
85 | mutex_lock(&tz->lock); |
86 | ||
8b91e2cb | 87 | list_for_each_entry(instance, &tz->thermal_instances, tz_node) { |
bcdcbbc7 JM |
88 | if (instance->trip != trip) |
89 | continue; | |
90 | ||
91 | total_weight += instance->weight; | |
92 | total_instance++; | |
93 | } | |
94 | ||
95 | list_for_each_entry(instance, &tz->thermal_instances, tz_node) { | |
96 | int percentage; | |
8b91e2cb | 97 | struct thermal_cooling_device *cdev = instance->cdev; |
4ccc5743 | 98 | |
8b91e2cb | 99 | if (instance->trip != trip) |
4ccc5743 D |
100 | continue; |
101 | ||
bcdcbbc7 JM |
102 | if (!total_weight) |
103 | percentage = 100 / total_instance; | |
104 | else | |
105 | percentage = (instance->weight * 100) / total_weight; | |
106 | ||
107 | instance->target = get_target_state(tz, cdev, percentage, | |
108 | cur_trip_level); | |
4ccc5743 | 109 | |
1a933698 LL |
110 | mutex_lock(&cdev->lock); |
111 | __thermal_cdev_update(cdev); | |
112 | mutex_unlock(&cdev->lock); | |
4ccc5743 | 113 | } |
fef05776 LL |
114 | |
115 | mutex_unlock(&tz->lock); | |
4ccc5743 D |
116 | return 0; |
117 | } | |
118 | ||
62176933 | 119 | static struct thermal_governor thermal_gov_fair_share = { |
4ccc5743 D |
120 | .name = "fair_share", |
121 | .throttle = fair_share_throttle, | |
4ccc5743 | 122 | }; |
57c5b2ec | 123 | THERMAL_GOVERNOR_DECLARE(thermal_gov_fair_share); |