1 // SPDX-License-Identifier: GPL-2.0-only
3 * fair_share.c - A simple weight based Thermal governor
5 * Copyright (C) 2012 Intel Corp
6 * Copyright (C) 2012 Durgadoss R <durgadoss.r@intel.com>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13 #include <linux/thermal.h>
14 #include "thermal_trace.h"
16 #include "thermal_core.h"
18 static int get_trip_level(struct thermal_zone_device *tz)
20 const struct thermal_trip *trip, *level_trip = NULL;
23 for_each_trip(tz, trip) {
24 if (trip->temperature >= tz->temperature)
30 /* Bail out if the temperature is not greater than any trips. */
34 trip_level = thermal_zone_trip_id(tz, level_trip);
36 trace_thermal_zone_trip(tz, trip_level, level_trip->type);
41 static long get_target_state(struct thermal_zone_device *tz,
42 struct thermal_cooling_device *cdev, int percentage, int level)
44 return (long)(percentage * level * cdev->max_state) / (100 * tz->num_trips);
48 * fair_share_throttle - throttles devices associated with the given zone
49 * @tz: thermal_zone_device
52 * Throttling Logic: This uses three parameters to calculate the new
53 * throttle state of the cooling devices associated with the given zone.
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
66 static int fair_share_throttle(struct thermal_zone_device *tz,
67 const struct thermal_trip *trip)
69 struct thermal_instance *instance;
71 int total_instance = 0;
72 int cur_trip_level = get_trip_level(tz);
74 lockdep_assert_held(&tz->lock);
76 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
77 if (instance->trip != trip)
80 total_weight += instance->weight;
84 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
86 struct thermal_cooling_device *cdev = instance->cdev;
88 if (instance->trip != trip)
92 percentage = 100 / total_instance;
94 percentage = (instance->weight * 100) / total_weight;
96 instance->target = get_target_state(tz, cdev, percentage,
99 mutex_lock(&cdev->lock);
100 __thermal_cdev_update(cdev);
101 mutex_unlock(&cdev->lock);
107 static struct thermal_governor thermal_gov_fair_share = {
108 .name = "fair_share",
109 .throttle = fair_share_throttle,
111 THERMAL_GOVERNOR_DECLARE(thermal_gov_fair_share);