Skip to content

Commit 2c7b4bf

Browse files
committed
thermal: core: Store trip pointer in struct thermal_instance
Replace the integer trip number stored in struct thermal_instance with a pointer to the relevant trip and adjust the code using the structure in question accordingly. The main reason for making this change is to allow the trip point to cooling device binding code more straightforward, as illustrated by subsequent modifications of the ACPI thermal driver, but it also helps to clarify the overall design and allows the governor code overhead to be reduced (through subsequent modifications). The only case in which it adds complexity is trip_point_show() that needs to walk the trips[] table to find the index of the given trip point, but this is not a critical path and the interface that trip_point_show() belongs to is problematic anyway (for instance, it doesn't cover the case when the same cooling devices is associated with multiple trip points). This is a preliminary change and the affected code will be refined by a series of subsequent modifications of thermal governors, the core and the ACPI thermal driver. The general functionality is not expected to be affected by this change. Signed-off-by: Rafael J. Wysocki <[email protected]> Reviewed-by: Daniel Lezcano <[email protected]>
1 parent a15ffa7 commit 2c7b4bf

File tree

9 files changed

+60
-37
lines changed

9 files changed

+60
-37
lines changed

drivers/thermal/gov_bang_bang.c

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,21 @@
1313

1414
#include "thermal_core.h"
1515

16-
static int thermal_zone_trip_update(struct thermal_zone_device *tz, int trip_id)
16+
static int thermal_zone_trip_update(struct thermal_zone_device *tz, int trip_index)
1717
{
18-
struct thermal_trip trip;
18+
const struct thermal_trip *trip = &tz->trips[trip_index];
1919
struct thermal_instance *instance;
20-
int ret;
21-
22-
ret = __thermal_zone_get_trip(tz, trip_id, &trip);
23-
if (ret) {
24-
pr_warn_once("Failed to retrieve trip point %d\n", trip_id);
25-
return ret;
26-
}
2720

28-
if (!trip.hysteresis)
21+
if (!trip->hysteresis)
2922
dev_info_once(&tz->device,
3023
"Zero hysteresis value for thermal zone %s\n", tz->type);
3124

3225
dev_dbg(&tz->device, "Trip%d[temp=%d]:temp=%d:hyst=%d\n",
33-
trip_id, trip.temperature, tz->temperature,
34-
trip.hysteresis);
26+
trip_index, trip->temperature, tz->temperature,
27+
trip->hysteresis);
3528

3629
list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
37-
if (instance->trip != trip_id)
30+
if (instance->trip != trip)
3831
continue;
3932

4033
/* in case fan is in initial state, switch the fan off */
@@ -52,10 +45,10 @@ static int thermal_zone_trip_update(struct thermal_zone_device *tz, int trip_id)
5245
* enable fan when temperature exceeds trip_temp and disable
5346
* the fan in case it falls below trip_temp minus hysteresis
5447
*/
55-
if (instance->target == 0 && tz->temperature >= trip.temperature)
48+
if (instance->target == 0 && tz->temperature >= trip->temperature)
5649
instance->target = 1;
5750
else if (instance->target == 1 &&
58-
tz->temperature <= trip.temperature - trip.hysteresis)
51+
tz->temperature <= trip->temperature - trip->hysteresis)
5952
instance->target = 0;
6053

6154
dev_dbg(&instance->cdev->device, "target=%d\n",

drivers/thermal/gov_fair_share.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static long get_target_state(struct thermal_zone_device *tz,
4949
/**
5050
* fair_share_throttle - throttles devices associated with the given zone
5151
* @tz: thermal_zone_device
52-
* @trip: trip point index
52+
* @trip_index: trip point index
5353
*
5454
* Throttling Logic: This uses three parameters to calculate the new
5555
* throttle state of the cooling devices associated with the given zone.
@@ -65,8 +65,9 @@ static long get_target_state(struct thermal_zone_device *tz,
6565
* (Heavily assumes the trip points are in ascending order)
6666
* new_state of cooling device = P3 * P2 * P1
6767
*/
68-
static int fair_share_throttle(struct thermal_zone_device *tz, int trip)
68+
static int fair_share_throttle(struct thermal_zone_device *tz, int trip_index)
6969
{
70+
const struct thermal_trip *trip = &tz->trips[trip_index];
7071
struct thermal_instance *instance;
7172
int total_weight = 0;
7273
int total_instance = 0;

drivers/thermal/gov_power_allocator.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,14 @@ static u32 estimate_sustainable_power(struct thermal_zone_device *tz)
9090
u32 sustainable_power = 0;
9191
struct thermal_instance *instance;
9292
struct power_allocator_params *params = tz->governor_data;
93+
const struct thermal_trip *trip_max_desired_temperature =
94+
&tz->trips[params->trip_max_desired_temperature];
9395

9496
list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
9597
struct thermal_cooling_device *cdev = instance->cdev;
9698
u32 min_power;
9799

98-
if (instance->trip != params->trip_max_desired_temperature)
100+
if (instance->trip != trip_max_desired_temperature)
99101
continue;
100102

101103
if (!cdev_is_power_actor(cdev))
@@ -383,12 +385,13 @@ static int allocate_power(struct thermal_zone_device *tz,
383385
{
384386
struct thermal_instance *instance;
385387
struct power_allocator_params *params = tz->governor_data;
388+
const struct thermal_trip *trip_max_desired_temperature =
389+
&tz->trips[params->trip_max_desired_temperature];
386390
u32 *req_power, *max_power, *granted_power, *extra_actor_power;
387391
u32 *weighted_req_power;
388392
u32 total_req_power, max_allocatable_power, total_weighted_req_power;
389393
u32 total_granted_power, power_range;
390394
int i, num_actors, total_weight, ret = 0;
391-
int trip_max_desired_temperature = params->trip_max_desired_temperature;
392395

393396
num_actors = 0;
394397
total_weight = 0;
@@ -564,12 +567,14 @@ static void allow_maximum_power(struct thermal_zone_device *tz, bool update)
564567
{
565568
struct thermal_instance *instance;
566569
struct power_allocator_params *params = tz->governor_data;
570+
const struct thermal_trip *trip_max_desired_temperature =
571+
&tz->trips[params->trip_max_desired_temperature];
567572
u32 req_power;
568573

569574
list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
570575
struct thermal_cooling_device *cdev = instance->cdev;
571576

572-
if ((instance->trip != params->trip_max_desired_temperature) ||
577+
if ((instance->trip != trip_max_desired_temperature) ||
573578
(!cdev_is_power_actor(instance->cdev)))
574579
continue;
575580

drivers/thermal/gov_step_wise.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,26 +81,24 @@ static void update_passive_instance(struct thermal_zone_device *tz,
8181

8282
static void thermal_zone_trip_update(struct thermal_zone_device *tz, int trip_id)
8383
{
84+
const struct thermal_trip *trip = &tz->trips[trip_id];
8485
enum thermal_trend trend;
8586
struct thermal_instance *instance;
86-
struct thermal_trip trip;
8787
bool throttle = false;
8888
int old_target;
8989

90-
__thermal_zone_get_trip(tz, trip_id, &trip);
91-
9290
trend = get_tz_trend(tz, trip_id);
9391

94-
if (tz->temperature >= trip.temperature) {
92+
if (tz->temperature >= trip->temperature) {
9593
throttle = true;
96-
trace_thermal_zone_trip(tz, trip_id, trip.type);
94+
trace_thermal_zone_trip(tz, trip_id, trip->type);
9795
}
9896

9997
dev_dbg(&tz->device, "Trip%d[type=%d,temp=%d]:trend=%d,throttle=%d\n",
100-
trip_id, trip.type, trip.temperature, trend, throttle);
98+
trip_id, trip->type, trip->temperature, trend, throttle);
10199

102100
list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
103-
if (instance->trip != trip_id)
101+
if (instance->trip != trip)
104102
continue;
105103

106104
old_target = instance->target;
@@ -114,11 +112,11 @@ static void thermal_zone_trip_update(struct thermal_zone_device *tz, int trip_id
114112
/* Activate a passive thermal instance */
115113
if (old_target == THERMAL_NO_TARGET &&
116114
instance->target != THERMAL_NO_TARGET)
117-
update_passive_instance(tz, trip.type, 1);
115+
update_passive_instance(tz, trip->type, 1);
118116
/* Deactivate a passive thermal instance */
119117
else if (old_target != THERMAL_NO_TARGET &&
120118
instance->target == THERMAL_NO_TARGET)
121-
update_passive_instance(tz, trip.type, -1);
119+
update_passive_instance(tz, trip->type, -1);
122120

123121
instance->initialized = true;
124122
mutex_lock(&instance->cdev->lock);

drivers/thermal/thermal_core.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ struct thermal_zone_device *thermal_zone_get_by_id(int id)
602602
/**
603603
* thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
604604
* @tz: pointer to struct thermal_zone_device
605-
* @trip: indicates which trip point the cooling devices is
605+
* @trip_index: indicates which trip point the cooling devices is
606606
* associated with in this thermal zone.
607607
* @cdev: pointer to struct thermal_cooling_device
608608
* @upper: the Maximum cooling state for this trip point.
@@ -622,7 +622,7 @@ struct thermal_zone_device *thermal_zone_get_by_id(int id)
622622
* Return: 0 on success, the proper error value otherwise.
623623
*/
624624
int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
625-
int trip,
625+
int trip_index,
626626
struct thermal_cooling_device *cdev,
627627
unsigned long upper, unsigned long lower,
628628
unsigned int weight)
@@ -631,12 +631,15 @@ int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
631631
struct thermal_instance *pos;
632632
struct thermal_zone_device *pos1;
633633
struct thermal_cooling_device *pos2;
634+
const struct thermal_trip *trip;
634635
bool upper_no_limit;
635636
int result;
636637

637-
if (trip >= tz->num_trips || trip < 0)
638+
if (trip_index >= tz->num_trips || trip_index < 0)
638639
return -EINVAL;
639640

641+
trip = &tz->trips[trip_index];
642+
640643
list_for_each_entry(pos1, &thermal_tz_list, node) {
641644
if (pos1 == tz)
642645
break;
@@ -739,7 +742,7 @@ EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
739742
* thermal_zone_unbind_cooling_device() - unbind a cooling device from a
740743
* thermal zone.
741744
* @tz: pointer to a struct thermal_zone_device.
742-
* @trip: indicates which trip point the cooling devices is
745+
* @trip_index: indicates which trip point the cooling devices is
743746
* associated with in this thermal zone.
744747
* @cdev: pointer to a struct thermal_cooling_device.
745748
*
@@ -750,13 +753,15 @@ EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
750753
* Return: 0 on success, the proper error value otherwise.
751754
*/
752755
int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
753-
int trip,
756+
int trip_index,
754757
struct thermal_cooling_device *cdev)
755758
{
756759
struct thermal_instance *pos, *next;
760+
const struct thermal_trip *trip;
757761

758762
mutex_lock(&tz->lock);
759763
mutex_lock(&cdev->lock);
764+
trip = &tz->trips[trip_index];
760765
list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
761766
if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
762767
list_del(&pos->tz_node);

drivers/thermal/thermal_core.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ struct thermal_instance {
8787
char name[THERMAL_NAME_LENGTH];
8888
struct thermal_zone_device *tz;
8989
struct thermal_cooling_device *cdev;
90-
int trip;
90+
const struct thermal_trip *trip;
9191
bool initialized;
9292
unsigned long upper; /* Highest cooling state for this trip point */
9393
unsigned long lower; /* Lowest cooling state for this trip point */
@@ -119,6 +119,8 @@ void __thermal_zone_device_update(struct thermal_zone_device *tz,
119119
void __thermal_zone_set_trips(struct thermal_zone_device *tz);
120120
int __thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
121121
struct thermal_trip *trip);
122+
int thermal_zone_trip_id(struct thermal_zone_device *tz,
123+
const struct thermal_trip *trip);
122124
int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp);
123125

124126
/* sysfs I/F */

drivers/thermal/thermal_helpers.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,17 @@ int get_tz_trend(struct thermal_zone_device *tz, int trip_index)
4242

4343
struct thermal_instance *
4444
get_thermal_instance(struct thermal_zone_device *tz,
45-
struct thermal_cooling_device *cdev, int trip)
45+
struct thermal_cooling_device *cdev, int trip_index)
4646
{
4747
struct thermal_instance *pos = NULL;
4848
struct thermal_instance *target_instance = NULL;
49+
const struct thermal_trip *trip;
4950

5051
mutex_lock(&tz->lock);
5152
mutex_lock(&cdev->lock);
5253

54+
trip = &tz->trips[trip_index];
55+
5356
list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
5457
if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
5558
target_instance = pos;

drivers/thermal/thermal_sysfs.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,8 @@ trip_point_show(struct device *dev, struct device_attribute *attr, char *buf)
943943
instance =
944944
container_of(attr, struct thermal_instance, attr);
945945

946-
return sprintf(buf, "%d\n", instance->trip);
946+
return sprintf(buf, "%d\n",
947+
thermal_zone_trip_id(instance->tz, instance->trip));
947948
}
948949

949950
ssize_t

drivers/thermal/thermal_trip.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,18 @@ int thermal_zone_set_trip(struct thermal_zone_device *tz, int trip_id,
157157

158158
return 0;
159159
}
160+
161+
int thermal_zone_trip_id(struct thermal_zone_device *tz,
162+
const struct thermal_trip *trip)
163+
{
164+
int i;
165+
166+
lockdep_assert_held(&tz->lock);
167+
168+
for (i = 0; i < tz->num_trips; i++) {
169+
if (&tz->trips[i] == trip)
170+
return i;
171+
}
172+
173+
return -ENODATA;
174+
}

0 commit comments

Comments
 (0)