xref: /linux/drivers/thermal/gov_fair_share.c (revision 7255fcc80d4b525cc10cfaaf7f485830d4ed2000)
1 // SPDX-License-Identifier: GPL-2.0-only
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  *
10  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11  */
12 
13 #include <linux/thermal.h>
14 #include "thermal_trace.h"
15 
16 #include "thermal_core.h"
17 
18 static int get_trip_level(struct thermal_zone_device *tz)
19 {
20 	const struct thermal_trip *trip, *level_trip = NULL;
21 	int trip_level = -1;
22 
23 	for_each_trip(tz, trip) {
24 		if (trip->temperature >= tz->temperature)
25 			continue;
26 
27 		trip_level++;
28 
29 		if (!level_trip || trip->temperature > level_trip->temperature)
30 			level_trip = trip;
31 	}
32 
33 	/*  Bail out if the temperature is not greater than any trips. */
34 	if (trip_level < 0)
35 		return 0;
36 
37 	trace_thermal_zone_trip(tz, thermal_zone_trip_id(tz, level_trip),
38 				level_trip->type);
39 
40 	return trip_level;
41 }
42 
43 static long get_target_state(struct thermal_zone_device *tz,
44 		struct thermal_cooling_device *cdev, int percentage, int level)
45 {
46 	return (long)(percentage * level * cdev->max_state) / (100 * tz->num_trips);
47 }
48 
49 /**
50  * fair_share_throttle - throttles devices associated with the given zone
51  * @tz: thermal_zone_device
52  * @trip: trip point
53  *
54  * Throttling Logic: This uses three parameters to calculate the new
55  * throttle state of the cooling devices associated with the given zone.
56  *
57  * Parameters used for Throttling:
58  * P1. max_state: Maximum throttle state exposed by the cooling device.
59  * P2. percentage[i]/100:
60  *	How 'effective' the 'i'th device is, in cooling the given zone.
61  * P3. cur_trip_level/max_no_of_trips:
62  *	This describes the extent to which the devices should be throttled.
63  *	We do not want to throttle too much when we trip a lower temperature,
64  *	whereas the throttling is at full swing if we trip critical levels.
65  *	(Heavily assumes the trip points are in ascending order)
66  * new_state of cooling device = P3 * P2 * P1
67  */
68 static int fair_share_throttle(struct thermal_zone_device *tz,
69 			       const struct thermal_trip *trip)
70 {
71 	struct thermal_instance *instance;
72 	int total_weight = 0;
73 	int total_instance = 0;
74 	int cur_trip_level = get_trip_level(tz);
75 
76 	lockdep_assert_held(&tz->lock);
77 
78 	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
79 		if (instance->trip != trip)
80 			continue;
81 
82 		total_weight += instance->weight;
83 		total_instance++;
84 	}
85 
86 	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
87 		int percentage;
88 		struct thermal_cooling_device *cdev = instance->cdev;
89 
90 		if (instance->trip != trip)
91 			continue;
92 
93 		if (!total_weight)
94 			percentage = 100 / total_instance;
95 		else
96 			percentage = (instance->weight * 100) / total_weight;
97 
98 		instance->target = get_target_state(tz, cdev, percentage,
99 						    cur_trip_level);
100 
101 		mutex_lock(&cdev->lock);
102 		__thermal_cdev_update(cdev);
103 		mutex_unlock(&cdev->lock);
104 	}
105 
106 	return 0;
107 }
108 
109 static struct thermal_governor thermal_gov_fair_share = {
110 	.name		= "fair_share",
111 	.throttle	= fair_share_throttle,
112 };
113 THERMAL_GOVERNOR_DECLARE(thermal_gov_fair_share);
114