blob: afd99f668c65d51190547682a30c80b83c38119e [file] [log] [blame]
Thomas Gleixner873e65b2019-05-27 08:55:15 +02001// SPDX-License-Identifier: GPL-2.0-only
Durgadoss R4ccc57432012-09-18 11:05:01 +05302/*
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 *
Durgadoss R4ccc57432012-09-18 11:05:01 +053010 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 */
12
Durgadoss R4ccc57432012-09-18 11:05:01 +053013#include <linux/thermal.h>
Punit Agrawal208cd822014-07-29 11:50:50 +010014#include <trace/events/thermal.h>
Durgadoss R4ccc57432012-09-18 11:05:01 +053015
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{
24 int count = 0;
Sascha Hauer17e83512015-07-24 08:12:54 +020025 int trip_temp;
Punit Agrawal208cd822014-07-29 11:50:50 +010026 enum thermal_trip_type trip_type;
Durgadoss R4ccc57432012-09-18 11:05:01 +053027
28 if (tz->trips == 0 || !tz->ops->get_trip_temp)
29 return 0;
30
31 for (count = 0; count < tz->trips; count++) {
32 tz->ops->get_trip_temp(tz, count, &trip_temp);
33 if (tz->temperature < trip_temp)
34 break;
35 }
Punit Agrawal208cd822014-07-29 11:50:50 +010036
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
Durgadoss R4ccc57432012-09-18 11:05:01 +053046 return count;
47}
48
49static long get_target_state(struct thermal_zone_device *tz,
Javi Merinobcdcbbc2015-02-18 16:04:25 +000050 struct thermal_cooling_device *cdev, int percentage, int level)
Durgadoss R4ccc57432012-09-18 11:05:01 +053051{
52 unsigned long max_state;
53
54 cdev->ops->get_max_state(cdev, &max_state);
55
Javi Merinobcdcbbc2015-02-18 16:04:25 +000056 return (long)(percentage * level * max_state) / (100 * tz->trips);
Durgadoss R4ccc57432012-09-18 11:05:01 +053057}
58
59/**
Javi Merino80b89172015-02-18 16:04:23 +000060 * fair_share_throttle - throttles devices associated with the given zone
Durgadoss R4ccc57432012-09-18 11:05:01 +053061 * @tz - thermal_zone_device
Willy WOLFF0d76d6e12017-06-24 14:06:03 +010062 * @trip - trip point index
Durgadoss R4ccc57432012-09-18 11:05:01 +053063 *
64 * Throttling Logic: This uses three parameters to calculate the new
65 * throttle state of the cooling devices associated with the given zone.
66 *
67 * Parameters used for Throttling:
68 * P1. max_state: Maximum throttle state exposed by the cooling device.
Javi Merinobcdcbbc2015-02-18 16:04:25 +000069 * P2. percentage[i]/100:
Durgadoss R4ccc57432012-09-18 11:05:01 +053070 * How 'effective' the 'i'th device is, in cooling the given zone.
71 * P3. cur_trip_level/max_no_of_trips:
72 * This describes the extent to which the devices should be throttled.
73 * We do not want to throttle too much when we trip a lower temperature,
74 * whereas the throttling is at full swing if we trip critical levels.
75 * (Heavily assumes the trip points are in ascending order)
76 * new_state of cooling device = P3 * P2 * P1
77 */
Sachin Kamat62176932012-09-27 16:57:53 +053078static int fair_share_throttle(struct thermal_zone_device *tz, int trip)
Durgadoss R4ccc57432012-09-18 11:05:01 +053079{
Durgadoss R4ccc57432012-09-18 11:05:01 +053080 struct thermal_instance *instance;
Javi Merinobcdcbbc2015-02-18 16:04:25 +000081 int total_weight = 0;
82 int total_instance = 0;
Durgadoss R4ccc57432012-09-18 11:05:01 +053083 int cur_trip_level = get_trip_level(tz);
84
Javi Merino8b91e2c2015-02-18 16:04:22 +000085 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
Javi Merinobcdcbbc2015-02-18 16:04:25 +000086 if (instance->trip != trip)
87 continue;
88
89 total_weight += instance->weight;
90 total_instance++;
91 }
92
93 list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
94 int percentage;
Javi Merino8b91e2c2015-02-18 16:04:22 +000095 struct thermal_cooling_device *cdev = instance->cdev;
Durgadoss R4ccc57432012-09-18 11:05:01 +053096
Javi Merino8b91e2c2015-02-18 16:04:22 +000097 if (instance->trip != trip)
Durgadoss R4ccc57432012-09-18 11:05:01 +053098 continue;
99
Javi Merinobcdcbbc2015-02-18 16:04:25 +0000100 if (!total_weight)
101 percentage = 100 / total_instance;
102 else
103 percentage = (instance->weight * 100) / total_weight;
104
105 instance->target = get_target_state(tz, cdev, percentage,
106 cur_trip_level);
Durgadoss R4ccc57432012-09-18 11:05:01 +0530107
Michele Di Giorgiod0b73062016-06-02 15:25:31 +0100108 mutex_lock(&instance->cdev->lock);
Durgadoss R4ccc57432012-09-18 11:05:01 +0530109 instance->cdev->updated = false;
Michele Di Giorgiod0b73062016-06-02 15:25:31 +0100110 mutex_unlock(&instance->cdev->lock);
Durgadoss R4ccc57432012-09-18 11:05:01 +0530111 thermal_cdev_update(cdev);
112 }
113 return 0;
114}
115
Sachin Kamat62176932012-09-27 16:57:53 +0530116static struct thermal_governor thermal_gov_fair_share = {
Durgadoss R4ccc57432012-09-18 11:05:01 +0530117 .name = "fair_share",
118 .throttle = fair_share_throttle,
Durgadoss R4ccc57432012-09-18 11:05:01 +0530119};
Daniel Lezcano57c5b2e2019-06-12 22:13:25 +0200120THERMAL_GOVERNOR_DECLARE(thermal_gov_fair_share);