Blame view

drivers/thermal/fair_share.c 4.07 KB
4ccc5743a   Durgadoss R   Thermal: Introduc...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  /*
   *  fair_share.c - A simple weight based Thermal governor
   *
   *  Copyright (C) 2012 Intel Corp
   *  Copyright (C) 2012 Durgadoss R <durgadoss.r@intel.com>
   *
   *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   *
   *  This program is free software; you can redistribute it and/or modify
   *  it under the terms of the GNU General Public License as published by
   *  the Free Software Foundation; version 2 of the License.
   *
   *  This program is distributed in the hope that it will be useful, but
   *  WITHOUT ANY WARRANTY; without even the implied warranty of
   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   *  General Public License for more details.
   *
   *  You should have received a copy of the GNU General Public License along
   *  with this program; if not, write to the Free Software Foundation, Inc.,
   *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
   *
   * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   */
4ccc5743a   Durgadoss R   Thermal: Introduc...
24
  #include <linux/thermal.h>
208cd822a   Punit Agrawal   thermal: trace: T...
25
  #include <trace/events/thermal.h>
4ccc5743a   Durgadoss R   Thermal: Introduc...
26
27
28
29
30
31
32
33
34
35
  
  #include "thermal_core.h"
  
  /**
   * get_trip_level: - obtains the current trip level for a zone
   * @tz:		thermal zone device
   */
  static int get_trip_level(struct thermal_zone_device *tz)
  {
  	int count = 0;
17e8351a7   Sascha Hauer   thermal: consiste...
36
  	int trip_temp;
208cd822a   Punit Agrawal   thermal: trace: T...
37
  	enum thermal_trip_type trip_type;
4ccc5743a   Durgadoss R   Thermal: Introduc...
38
39
40
41
42
43
44
45
46
  
  	if (tz->trips == 0 || !tz->ops->get_trip_temp)
  		return 0;
  
  	for (count = 0; count < tz->trips; count++) {
  		tz->ops->get_trip_temp(tz, count, &trip_temp);
  		if (tz->temperature < trip_temp)
  			break;
  	}
208cd822a   Punit Agrawal   thermal: trace: T...
47
48
49
50
51
52
53
54
55
  
  	/*
  	 * count > 0 only if temperature is greater than first trip
  	 * point, in which case, trip_point = count - 1
  	 */
  	if (count > 0) {
  		tz->ops->get_trip_type(tz, count - 1, &trip_type);
  		trace_thermal_zone_trip(tz, count - 1, trip_type);
  	}
4ccc5743a   Durgadoss R   Thermal: Introduc...
56
57
58
59
  	return count;
  }
  
  static long get_target_state(struct thermal_zone_device *tz,
bcdcbbc71   Javi Merino   thermal: fair_sha...
60
  		struct thermal_cooling_device *cdev, int percentage, int level)
4ccc5743a   Durgadoss R   Thermal: Introduc...
61
62
63
64
  {
  	unsigned long max_state;
  
  	cdev->ops->get_max_state(cdev, &max_state);
bcdcbbc71   Javi Merino   thermal: fair_sha...
65
  	return (long)(percentage * level * max_state) / (100 * tz->trips);
4ccc5743a   Durgadoss R   Thermal: Introduc...
66
67
68
  }
  
  /**
80b89172f   Javi Merino   thermal: fair_sha...
69
   * fair_share_throttle - throttles devices associated with the given zone
4ccc5743a   Durgadoss R   Thermal: Introduc...
70
   * @tz - thermal_zone_device
0d76d6e1e   Willy WOLFF   thermal: fix sour...
71
   * @trip - trip point index
4ccc5743a   Durgadoss R   Thermal: Introduc...
72
73
74
75
76
77
   *
   * Throttling Logic: This uses three parameters to calculate the new
   * throttle state of the cooling devices associated with the given zone.
   *
   * Parameters used for Throttling:
   * P1. max_state: Maximum throttle state exposed by the cooling device.
bcdcbbc71   Javi Merino   thermal: fair_sha...
78
   * P2. percentage[i]/100:
4ccc5743a   Durgadoss R   Thermal: Introduc...
79
80
81
82
83
84
85
86
   *	How 'effective' the 'i'th device is, in cooling the given zone.
   * P3. cur_trip_level/max_no_of_trips:
   *	This describes the extent to which the devices should be throttled.
   *	We do not want to throttle too much when we trip a lower temperature,
   *	whereas the throttling is at full swing if we trip critical levels.
   *	(Heavily assumes the trip points are in ascending order)
   * new_state of cooling device = P3 * P2 * P1
   */
621769334   Sachin Kamat   thermal: fair_sha...
87
  static int fair_share_throttle(struct thermal_zone_device *tz, int trip)
4ccc5743a   Durgadoss R   Thermal: Introduc...
88
  {
4ccc5743a   Durgadoss R   Thermal: Introduc...
89
  	struct thermal_instance *instance;
bcdcbbc71   Javi Merino   thermal: fair_sha...
90
91
  	int total_weight = 0;
  	int total_instance = 0;
4ccc5743a   Durgadoss R   Thermal: Introduc...
92
  	int cur_trip_level = get_trip_level(tz);
8b91e2cb2   Javi Merino   thermal: fair_sha...
93
  	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
bcdcbbc71   Javi Merino   thermal: fair_sha...
94
95
96
97
98
99
100
101
102
  		if (instance->trip != trip)
  			continue;
  
  		total_weight += instance->weight;
  		total_instance++;
  	}
  
  	list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
  		int percentage;
8b91e2cb2   Javi Merino   thermal: fair_sha...
103
  		struct thermal_cooling_device *cdev = instance->cdev;
4ccc5743a   Durgadoss R   Thermal: Introduc...
104

8b91e2cb2   Javi Merino   thermal: fair_sha...
105
  		if (instance->trip != trip)
4ccc5743a   Durgadoss R   Thermal: Introduc...
106
  			continue;
bcdcbbc71   Javi Merino   thermal: fair_sha...
107
108
109
110
111
112
113
  		if (!total_weight)
  			percentage = 100 / total_instance;
  		else
  			percentage = (instance->weight * 100) / total_weight;
  
  		instance->target = get_target_state(tz, cdev, percentage,
  						    cur_trip_level);
4ccc5743a   Durgadoss R   Thermal: Introduc...
114

d0b7306d2   Michele Di Giorgio   thermal: fix race...
115
  		mutex_lock(&instance->cdev->lock);
4ccc5743a   Durgadoss R   Thermal: Introduc...
116
  		instance->cdev->updated = false;
d0b7306d2   Michele Di Giorgio   thermal: fix race...
117
  		mutex_unlock(&instance->cdev->lock);
4ccc5743a   Durgadoss R   Thermal: Introduc...
118
119
120
121
  		thermal_cdev_update(cdev);
  	}
  	return 0;
  }
621769334   Sachin Kamat   thermal: fair_sha...
122
  static struct thermal_governor thermal_gov_fair_share = {
4ccc5743a   Durgadoss R   Thermal: Introduc...
123
124
  	.name		= "fair_share",
  	.throttle	= fair_share_throttle,
4ccc5743a   Durgadoss R   Thermal: Introduc...
125
  };
80a26a5c2   Zhang Rui   Thermal: build th...
126
  int thermal_gov_fair_share_register(void)
4ccc5743a   Durgadoss R   Thermal: Introduc...
127
128
129
  {
  	return thermal_register_governor(&thermal_gov_fair_share);
  }
80a26a5c2   Zhang Rui   Thermal: build th...
130
  void thermal_gov_fair_share_unregister(void)
4ccc5743a   Durgadoss R   Thermal: Introduc...
131
132
133
  {
  	thermal_unregister_governor(&thermal_gov_fair_share);
  }