1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * fair_share.c - A simple weight based Thermal governor
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2012 Intel Corp
6*4882a593Smuzhiyun * Copyright (C) 2012 Durgadoss R <durgadoss.r@intel.com>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11*4882a593Smuzhiyun */
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #include <linux/thermal.h>
14*4882a593Smuzhiyun #include <trace/events/thermal.h>
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #include "thermal_core.h"
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun /**
19*4882a593Smuzhiyun * get_trip_level: - obtains the current trip level for a zone
20*4882a593Smuzhiyun * @tz: thermal zone device
21*4882a593Smuzhiyun */
get_trip_level(struct thermal_zone_device * tz)22*4882a593Smuzhiyun static int get_trip_level(struct thermal_zone_device *tz)
23*4882a593Smuzhiyun {
24*4882a593Smuzhiyun int count = 0;
25*4882a593Smuzhiyun int trip_temp;
26*4882a593Smuzhiyun enum thermal_trip_type trip_type;
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun if (tz->trips == 0 || !tz->ops->get_trip_temp)
29*4882a593Smuzhiyun return 0;
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun for (count = 0; count < tz->trips; count++) {
32*4882a593Smuzhiyun tz->ops->get_trip_temp(tz, count, &trip_temp);
33*4882a593Smuzhiyun if (tz->temperature < trip_temp)
34*4882a593Smuzhiyun break;
35*4882a593Smuzhiyun }
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun /*
38*4882a593Smuzhiyun * count > 0 only if temperature is greater than first trip
39*4882a593Smuzhiyun * point, in which case, trip_point = count - 1
40*4882a593Smuzhiyun */
41*4882a593Smuzhiyun if (count > 0) {
42*4882a593Smuzhiyun tz->ops->get_trip_type(tz, count - 1, &trip_type);
43*4882a593Smuzhiyun trace_thermal_zone_trip(tz, count - 1, trip_type);
44*4882a593Smuzhiyun }
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun return count;
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun
get_target_state(struct thermal_zone_device * tz,struct thermal_cooling_device * cdev,int percentage,int level)49*4882a593Smuzhiyun static long get_target_state(struct thermal_zone_device *tz,
50*4882a593Smuzhiyun struct thermal_cooling_device *cdev, int percentage, int level)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun unsigned long max_state;
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun cdev->ops->get_max_state(cdev, &max_state);
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun return (long)(percentage * level * max_state) / (100 * tz->trips);
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun /**
60*4882a593Smuzhiyun * fair_share_throttle - throttles devices associated with the given zone
61*4882a593Smuzhiyun * @tz: thermal_zone_device
62*4882a593Smuzhiyun * @trip: trip point index
63*4882a593Smuzhiyun *
64*4882a593Smuzhiyun * Throttling Logic: This uses three parameters to calculate the new
65*4882a593Smuzhiyun * throttle state of the cooling devices associated with the given zone.
66*4882a593Smuzhiyun *
67*4882a593Smuzhiyun * Parameters used for Throttling:
68*4882a593Smuzhiyun * P1. max_state: Maximum throttle state exposed by the cooling device.
69*4882a593Smuzhiyun * P2. percentage[i]/100:
70*4882a593Smuzhiyun * How 'effective' the 'i'th device is, in cooling the given zone.
71*4882a593Smuzhiyun * P3. cur_trip_level/max_no_of_trips:
72*4882a593Smuzhiyun * This describes the extent to which the devices should be throttled.
73*4882a593Smuzhiyun * We do not want to throttle too much when we trip a lower temperature,
74*4882a593Smuzhiyun * whereas the throttling is at full swing if we trip critical levels.
75*4882a593Smuzhiyun * (Heavily assumes the trip points are in ascending order)
76*4882a593Smuzhiyun * new_state of cooling device = P3 * P2 * P1
77*4882a593Smuzhiyun */
fair_share_throttle(struct thermal_zone_device * tz,int trip)78*4882a593Smuzhiyun static int fair_share_throttle(struct thermal_zone_device *tz, int trip)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun struct thermal_instance *instance;
81*4882a593Smuzhiyun int total_weight = 0;
82*4882a593Smuzhiyun int total_instance = 0;
83*4882a593Smuzhiyun int cur_trip_level = get_trip_level(tz);
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun mutex_lock(&tz->lock);
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
88*4882a593Smuzhiyun if (instance->trip != trip)
89*4882a593Smuzhiyun continue;
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun total_weight += instance->weight;
92*4882a593Smuzhiyun total_instance++;
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
96*4882a593Smuzhiyun int percentage;
97*4882a593Smuzhiyun struct thermal_cooling_device *cdev = instance->cdev;
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun if (instance->trip != trip)
100*4882a593Smuzhiyun continue;
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun if (!total_weight)
103*4882a593Smuzhiyun percentage = 100 / total_instance;
104*4882a593Smuzhiyun else
105*4882a593Smuzhiyun percentage = (instance->weight * 100) / total_weight;
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun instance->target = get_target_state(tz, cdev, percentage,
108*4882a593Smuzhiyun cur_trip_level);
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun mutex_lock(&instance->cdev->lock);
111*4882a593Smuzhiyun instance->cdev->updated = false;
112*4882a593Smuzhiyun mutex_unlock(&instance->cdev->lock);
113*4882a593Smuzhiyun thermal_cdev_update(cdev);
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun mutex_unlock(&tz->lock);
117*4882a593Smuzhiyun return 0;
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun static struct thermal_governor thermal_gov_fair_share = {
121*4882a593Smuzhiyun .name = "fair_share",
122*4882a593Smuzhiyun .throttle = fair_share_throttle,
123*4882a593Smuzhiyun };
124*4882a593Smuzhiyun THERMAL_GOVERNOR_DECLARE(thermal_gov_fair_share);
125