scsi: iscsi: Fix multiple iSCSI session unbind events sent to userspace
[platform/kernel/linux-rpi.git] / drivers / thermal / gov_fair_share.c
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 <trace/events/thermal.h>
15
16 #include "thermal_core.h"
17
18 /**
19  * get_trip_level: - obtains the current trip level for a zone
20  * @tz:         thermal zone device
21  */
22 static int get_trip_level(struct thermal_zone_device *tz)
23 {
24         int count = 0;
25         int trip_temp;
26         enum thermal_trip_type trip_type;
27
28         if (tz->num_trips == 0 || !tz->ops->get_trip_temp)
29                 return 0;
30
31         for (count = 0; count < tz->num_trips; count++) {
32                 tz->ops->get_trip_temp(tz, count, &trip_temp);
33                 if (tz->temperature < trip_temp)
34                         break;
35         }
36
37         /*
38          * count > 0 only if temperature is greater than first trip
39          * point, in which case, trip_point = count - 1
40          */
41         if (count > 0) {
42                 tz->ops->get_trip_type(tz, count - 1, &trip_type);
43                 trace_thermal_zone_trip(tz, count - 1, trip_type);
44         }
45
46         return count;
47 }
48
49 static long get_target_state(struct thermal_zone_device *tz,
50                 struct thermal_cooling_device *cdev, int percentage, int level)
51 {
52         return (long)(percentage * level * cdev->max_state) / (100 * tz->num_trips);
53 }
54
55 /**
56  * fair_share_throttle - throttles devices associated with the given zone
57  * @tz: thermal_zone_device
58  * @trip: trip point index
59  *
60  * Throttling Logic: This uses three parameters to calculate the new
61  * throttle state of the cooling devices associated with the given zone.
62  *
63  * Parameters used for Throttling:
64  * P1. max_state: Maximum throttle state exposed by the cooling device.
65  * P2. percentage[i]/100:
66  *      How 'effective' the 'i'th device is, in cooling the given zone.
67  * P3. cur_trip_level/max_no_of_trips:
68  *      This describes the extent to which the devices should be throttled.
69  *      We do not want to throttle too much when we trip a lower temperature,
70  *      whereas the throttling is at full swing if we trip critical levels.
71  *      (Heavily assumes the trip points are in ascending order)
72  * new_state of cooling device = P3 * P2 * P1
73  */
74 static int fair_share_throttle(struct thermal_zone_device *tz, int trip)
75 {
76         struct thermal_instance *instance;
77         int total_weight = 0;
78         int total_instance = 0;
79         int cur_trip_level = get_trip_level(tz);
80
81         mutex_lock(&tz->lock);
82
83         list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
84                 if (instance->trip != trip)
85                         continue;
86
87                 total_weight += instance->weight;
88                 total_instance++;
89         }
90
91         list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
92                 int percentage;
93                 struct thermal_cooling_device *cdev = instance->cdev;
94
95                 if (instance->trip != trip)
96                         continue;
97
98                 if (!total_weight)
99                         percentage = 100 / total_instance;
100                 else
101                         percentage = (instance->weight * 100) / total_weight;
102
103                 instance->target = get_target_state(tz, cdev, percentage,
104                                                     cur_trip_level);
105
106                 mutex_lock(&cdev->lock);
107                 __thermal_cdev_update(cdev);
108                 mutex_unlock(&cdev->lock);
109         }
110
111         mutex_unlock(&tz->lock);
112         return 0;
113 }
114
115 static struct thermal_governor thermal_gov_fair_share = {
116         .name           = "fair_share",
117         .throttle       = fair_share_throttle,
118 };
119 THERMAL_GOVERNOR_DECLARE(thermal_gov_fair_share);