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