Merge tag 'efi-next-for-v6.5' of git://git.kernel.org/pub/scm/linux/kernel/git/efi/efi
[linux-block.git] / drivers / thermal / gov_fair_share.c
CommitLineData
873e65bc 1// SPDX-License-Identifier: GPL-2.0-only
4ccc5743
D
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 *
4ccc5743
D
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 */
12
4ccc5743 13#include <linux/thermal.h>
32a7a021 14#include "thermal_trace.h"
4ccc5743
D
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 */
22static int get_trip_level(struct thermal_zone_device *tz)
23{
7f725a23
DL
24 struct thermal_trip trip;
25 int count;
4ccc5743 26
e5bfcd30 27 for (count = 0; count < tz->num_trips; count++) {
7f725a23
DL
28 __thermal_zone_get_trip(tz, count, &trip);
29 if (tz->temperature < trip.temperature)
4ccc5743
D
30 break;
31 }
208cd822
PA
32
33 /*
34 * count > 0 only if temperature is greater than first trip
35 * point, in which case, trip_point = count - 1
36 */
7f725a23
DL
37 if (count > 0)
38 trace_thermal_zone_trip(tz, count - 1, trip.type);
208cd822 39
4ccc5743
D
40 return count;
41}
42
43static long get_target_state(struct thermal_zone_device *tz,
bcdcbbc7 44 struct thermal_cooling_device *cdev, int percentage, int level)
4ccc5743 45{
c408b3d1 46 return (long)(percentage * level * cdev->max_state) / (100 * tz->num_trips);
4ccc5743
D
47}
48
49/**
80b89172 50 * fair_share_throttle - throttles devices associated with the given zone
53d256e7
AK
51 * @tz: thermal_zone_device
52 * @trip: trip point index
4ccc5743
D
53 *
54 * Throttling Logic: This uses three parameters to calculate the new
55 * throttle state of the cooling devices associated with the given zone.
56 *
57 * Parameters used for Throttling:
58 * P1. max_state: Maximum throttle state exposed by the cooling device.
bcdcbbc7 59 * P2. percentage[i]/100:
4ccc5743
D
60 * How 'effective' the 'i'th device is, in cooling the given zone.
61 * P3. cur_trip_level/max_no_of_trips:
62 * This describes the extent to which the devices should be throttled.
63 * We do not want to throttle too much when we trip a lower temperature,
64 * whereas the throttling is at full swing if we trip critical levels.
65 * (Heavily assumes the trip points are in ascending order)
66 * new_state of cooling device = P3 * P2 * P1
67 */
62176933 68static int fair_share_throttle(struct thermal_zone_device *tz, int trip)
4ccc5743 69{
4ccc5743 70 struct thermal_instance *instance;
bcdcbbc7
JM
71 int total_weight = 0;
72 int total_instance = 0;
4ccc5743
D
73 int cur_trip_level = get_trip_level(tz);
74
670a5e35 75 lockdep_assert_held(&tz->lock);
fef05776 76
8b91e2cb 77 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
bcdcbbc7
JM
78 if (instance->trip != trip)
79 continue;
80
81 total_weight += instance->weight;
82 total_instance++;
83 }
84
85 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
86 int percentage;
8b91e2cb 87 struct thermal_cooling_device *cdev = instance->cdev;
4ccc5743 88
8b91e2cb 89 if (instance->trip != trip)
4ccc5743
D
90 continue;
91
bcdcbbc7
JM
92 if (!total_weight)
93 percentage = 100 / total_instance;
94 else
95 percentage = (instance->weight * 100) / total_weight;
96
97 instance->target = get_target_state(tz, cdev, percentage,
98 cur_trip_level);
4ccc5743 99
1a933698
LL
100 mutex_lock(&cdev->lock);
101 __thermal_cdev_update(cdev);
102 mutex_unlock(&cdev->lock);
4ccc5743 103 }
fef05776 104
4ccc5743
D
105 return 0;
106}
107
62176933 108static struct thermal_governor thermal_gov_fair_share = {
4ccc5743
D
109 .name = "fair_share",
110 .throttle = fair_share_throttle,
4ccc5743 111};
57c5b2ec 112THERMAL_GOVERNOR_DECLARE(thermal_gov_fair_share);