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