10015d9a2SAmit Kucheria // SPDX-License-Identifier: GPL-2.0-only
20015d9a2SAmit Kucheria /*
30015d9a2SAmit Kucheria  *  fair_share.c - A simple weight based Thermal governor
40015d9a2SAmit Kucheria  *
50015d9a2SAmit Kucheria  *  Copyright (C) 2012 Intel Corp
60015d9a2SAmit Kucheria  *  Copyright (C) 2012 Durgadoss R <durgadoss.r@intel.com>
70015d9a2SAmit Kucheria  *
80015d9a2SAmit Kucheria  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
90015d9a2SAmit Kucheria  *
100015d9a2SAmit Kucheria  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
110015d9a2SAmit Kucheria  */
120015d9a2SAmit Kucheria 
130015d9a2SAmit Kucheria #include <linux/thermal.h>
1432a7a021SDaniel Lezcano #include "thermal_trace.h"
150015d9a2SAmit Kucheria 
160015d9a2SAmit Kucheria #include "thermal_core.h"
170015d9a2SAmit Kucheria 
180015d9a2SAmit Kucheria /**
190015d9a2SAmit Kucheria  * get_trip_level: - obtains the current trip level for a zone
200015d9a2SAmit Kucheria  * @tz:		thermal zone device
210015d9a2SAmit Kucheria  */
get_trip_level(struct thermal_zone_device * tz)220015d9a2SAmit Kucheria static int get_trip_level(struct thermal_zone_device *tz)
230015d9a2SAmit Kucheria {
247f725a23SDaniel Lezcano 	struct thermal_trip trip;
257f725a23SDaniel Lezcano 	int count;
260015d9a2SAmit Kucheria 
27e5bfcd30SDaniel Lezcano 	for (count = 0; count < tz->num_trips; count++) {
287f725a23SDaniel Lezcano 		__thermal_zone_get_trip(tz, count, &trip);
297f725a23SDaniel Lezcano 		if (tz->temperature < trip.temperature)
300015d9a2SAmit Kucheria 			break;
310015d9a2SAmit Kucheria 	}
320015d9a2SAmit Kucheria 
330015d9a2SAmit Kucheria 	/*
340015d9a2SAmit Kucheria 	 * count > 0 only if temperature is greater than first trip
350015d9a2SAmit Kucheria 	 * point, in which case, trip_point = count - 1
360015d9a2SAmit Kucheria 	 */
377f725a23SDaniel Lezcano 	if (count > 0)
387f725a23SDaniel Lezcano 		trace_thermal_zone_trip(tz, count - 1, trip.type);
390015d9a2SAmit Kucheria 
400015d9a2SAmit Kucheria 	return count;
410015d9a2SAmit Kucheria }
420015d9a2SAmit Kucheria 
get_target_state(struct thermal_zone_device * tz,struct thermal_cooling_device * cdev,int percentage,int level)430015d9a2SAmit Kucheria static long get_target_state(struct thermal_zone_device *tz,
440015d9a2SAmit Kucheria 		struct thermal_cooling_device *cdev, int percentage, int level)
450015d9a2SAmit Kucheria {
46c408b3d1SViresh Kumar 	return (long)(percentage * level * cdev->max_state) / (100 * tz->num_trips);
470015d9a2SAmit Kucheria }
480015d9a2SAmit Kucheria 
490015d9a2SAmit Kucheria /**
500015d9a2SAmit Kucheria  * fair_share_throttle - throttles devices associated with the given zone
510015d9a2SAmit Kucheria  * @tz: thermal_zone_device
52*77451ef5SRafael J. Wysocki  * @trip_index: trip point index
530015d9a2SAmit Kucheria  *
540015d9a2SAmit Kucheria  * Throttling Logic: This uses three parameters to calculate the new
550015d9a2SAmit Kucheria  * throttle state of the cooling devices associated with the given zone.
560015d9a2SAmit Kucheria  *
570015d9a2SAmit Kucheria  * Parameters used for Throttling:
580015d9a2SAmit Kucheria  * P1. max_state: Maximum throttle state exposed by the cooling device.
590015d9a2SAmit Kucheria  * P2. percentage[i]/100:
600015d9a2SAmit Kucheria  *	How 'effective' the 'i'th device is, in cooling the given zone.
610015d9a2SAmit Kucheria  * P3. cur_trip_level/max_no_of_trips:
620015d9a2SAmit Kucheria  *	This describes the extent to which the devices should be throttled.
630015d9a2SAmit Kucheria  *	We do not want to throttle too much when we trip a lower temperature,
640015d9a2SAmit Kucheria  *	whereas the throttling is at full swing if we trip critical levels.
650015d9a2SAmit Kucheria  *	(Heavily assumes the trip points are in ascending order)
660015d9a2SAmit Kucheria  * new_state of cooling device = P3 * P2 * P1
670015d9a2SAmit Kucheria  */
fair_share_throttle(struct thermal_zone_device * tz,int trip_index)68*77451ef5SRafael J. Wysocki static int fair_share_throttle(struct thermal_zone_device *tz, int trip_index)
690015d9a2SAmit Kucheria {
70*77451ef5SRafael J. Wysocki 	const struct thermal_trip *trip = &tz->trips[trip_index];
710015d9a2SAmit Kucheria 	struct thermal_instance *instance;
720015d9a2SAmit Kucheria 	int total_weight = 0;
730015d9a2SAmit Kucheria 	int total_instance = 0;
740015d9a2SAmit Kucheria 	int cur_trip_level = get_trip_level(tz);
750015d9a2SAmit Kucheria 
76670a5e35SDaniel Lezcano 	lockdep_assert_held(&tz->lock);
77fef05776SLukasz Luba 
780015d9a2SAmit Kucheria 	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
790015d9a2SAmit Kucheria 		if (instance->trip != trip)
800015d9a2SAmit Kucheria 			continue;
810015d9a2SAmit Kucheria 
820015d9a2SAmit Kucheria 		total_weight += instance->weight;
830015d9a2SAmit Kucheria 		total_instance++;
840015d9a2SAmit Kucheria 	}
850015d9a2SAmit Kucheria 
860015d9a2SAmit Kucheria 	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
870015d9a2SAmit Kucheria 		int percentage;
880015d9a2SAmit Kucheria 		struct thermal_cooling_device *cdev = instance->cdev;
890015d9a2SAmit Kucheria 
900015d9a2SAmit Kucheria 		if (instance->trip != trip)
910015d9a2SAmit Kucheria 			continue;
920015d9a2SAmit Kucheria 
930015d9a2SAmit Kucheria 		if (!total_weight)
940015d9a2SAmit Kucheria 			percentage = 100 / total_instance;
950015d9a2SAmit Kucheria 		else
960015d9a2SAmit Kucheria 			percentage = (instance->weight * 100) / total_weight;
970015d9a2SAmit Kucheria 
980015d9a2SAmit Kucheria 		instance->target = get_target_state(tz, cdev, percentage,
990015d9a2SAmit Kucheria 						    cur_trip_level);
1000015d9a2SAmit Kucheria 
1011a933698SLukasz Luba 		mutex_lock(&cdev->lock);
1021a933698SLukasz Luba 		__thermal_cdev_update(cdev);
1031a933698SLukasz Luba 		mutex_unlock(&cdev->lock);
1040015d9a2SAmit Kucheria 	}
105fef05776SLukasz Luba 
1060015d9a2SAmit Kucheria 	return 0;
1070015d9a2SAmit Kucheria }
1080015d9a2SAmit Kucheria 
1090015d9a2SAmit Kucheria static struct thermal_governor thermal_gov_fair_share = {
1100015d9a2SAmit Kucheria 	.name		= "fair_share",
1110015d9a2SAmit Kucheria 	.throttle	= fair_share_throttle,
1120015d9a2SAmit Kucheria };
1130015d9a2SAmit Kucheria THERMAL_GOVERNOR_DECLARE(thermal_gov_fair_share);
114