X-Git-Url: https://git.karo-electronics.de/?a=blobdiff_plain;f=drivers%2Fthermal%2Fthermal_sys.c;h=9ee42ca4d289754cb99b374a32eb8bdc59371e46;hb=29b19e250434c6193c8b8e4c34c9c6284dd4f101;hp=2ab31e4f02cc741ee5d7b1c603a9158b34f335c4;hpb=5419575e838cd2cd11a1b30449eb58decdf51f9f;p=karo-tx-linux.git diff --git a/drivers/thermal/thermal_sys.c b/drivers/thermal/thermal_sys.c index 2ab31e4f02cc..9ee42ca4d289 100644 --- a/drivers/thermal/thermal_sys.c +++ b/drivers/thermal/thermal_sys.c @@ -41,15 +41,25 @@ MODULE_AUTHOR("Zhang Rui"); MODULE_DESCRIPTION("Generic thermal management sysfs support"); MODULE_LICENSE("GPL"); -struct thermal_cooling_device_instance { +#define THERMAL_NO_TARGET -1UL +/* + * This structure is used to describe the behavior of + * a certain cooling device on a certain trip point + * in a certain thermal zone + */ +struct thermal_instance { int id; char name[THERMAL_NAME_LENGTH]; struct thermal_zone_device *tz; struct thermal_cooling_device *cdev; int trip; + unsigned long upper; /* Highest cooling state for this trip point */ + unsigned long lower; /* Lowest cooling state for this trip point */ + unsigned long target; /* expected cooling state */ char attr_name[THERMAL_NAME_LENGTH]; struct device_attribute attr; - struct list_head node; + struct list_head tz_node; /* node in tz->thermal_instances */ + struct list_head cdev_node; /* node in cdev->thermal_instances */ }; static DEFINE_IDR(thermal_tz_idr); @@ -78,7 +88,7 @@ again: else if (unlikely(err)) return err; - *id = *id & MAX_ID_MASK; + *id = *id & MAX_IDR_MASK; return 0; } @@ -308,8 +318,9 @@ passive_store(struct device *dev, struct device_attribute *attr, if (!strncmp("Processor", cdev->type, sizeof("Processor"))) thermal_zone_bind_cooling_device(tz, - THERMAL_TRIPS_NONE, - cdev); + THERMAL_TRIPS_NONE, cdev, + THERMAL_NO_LIMIT, + THERMAL_NO_LIMIT); } mutex_unlock(&thermal_list_lock); if (!tz->passive_delay) @@ -327,9 +338,6 @@ passive_store(struct device *dev, struct device_attribute *attr, tz->passive_delay = 0; } - tz->tc1 = 1; - tz->tc2 = 1; - tz->forced_passive = state; thermal_zone_device_update(tz); @@ -425,10 +433,10 @@ static ssize_t thermal_cooling_device_trip_point_show(struct device *dev, struct device_attribute *attr, char *buf) { - struct thermal_cooling_device_instance *instance; + struct thermal_instance *instance; instance = - container_of(attr, struct thermal_cooling_device_instance, attr); + container_of(attr, struct thermal_instance, attr); if (instance->trip == THERMAL_TRIPS_NONE) return sprintf(buf, "-1\n"); @@ -590,7 +598,7 @@ thermal_add_hwmon_sysfs(struct thermal_zone_device *tz) temp->tz = tz; hwmon->count++; - snprintf(temp->temp_input.name, THERMAL_NAME_LENGTH, + snprintf(temp->temp_input.name, sizeof(temp->temp_input.name), "temp%d_input", hwmon->count); temp->temp_input.attr.attr.name = temp->temp_input.name; temp->temp_input.attr.attr.mode = 0444; @@ -603,7 +611,8 @@ thermal_add_hwmon_sysfs(struct thermal_zone_device *tz) if (tz->ops->get_crit_temp) { unsigned long temperature; if (!tz->ops->get_crit_temp(tz, &temperature)) { - snprintf(temp->temp_crit.name, THERMAL_NAME_LENGTH, + snprintf(temp->temp_crit.name, + sizeof(temp->temp_crit.name), "temp%d_crit", hwmon->count); temp->temp_crit.attr.attr.name = temp->temp_crit.name; temp->temp_crit.attr.attr.mode = 0444; @@ -694,85 +703,14 @@ thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz) static void thermal_zone_device_set_polling(struct thermal_zone_device *tz, int delay) { - cancel_delayed_work(&(tz->poll_queue)); - - if (!delay) - return; - if (delay > 1000) - queue_delayed_work(system_freezable_wq, &(tz->poll_queue), - round_jiffies(msecs_to_jiffies(delay))); + mod_delayed_work(system_freezable_wq, &tz->poll_queue, + round_jiffies(msecs_to_jiffies(delay))); + else if (delay) + mod_delayed_work(system_freezable_wq, &tz->poll_queue, + msecs_to_jiffies(delay)); else - queue_delayed_work(system_freezable_wq, &(tz->poll_queue), - msecs_to_jiffies(delay)); -} - -static void thermal_zone_device_passive(struct thermal_zone_device *tz, - int temp, int trip_temp, int trip) -{ - int trend = 0; - struct thermal_cooling_device_instance *instance; - struct thermal_cooling_device *cdev; - long state, max_state; - - /* - * Above Trip? - * ----------- - * Calculate the thermal trend (using the passive cooling equation) - * and modify the performance limit for all passive cooling devices - * accordingly. Note that we assume symmetry. - */ - if (temp >= trip_temp) { - tz->passive = true; - - trend = (tz->tc1 * (temp - tz->last_temperature)) + - (tz->tc2 * (temp - trip_temp)); - - /* Heating up? */ - if (trend > 0) { - list_for_each_entry(instance, &tz->cooling_devices, - node) { - if (instance->trip != trip) - continue; - cdev = instance->cdev; - cdev->ops->get_cur_state(cdev, &state); - cdev->ops->get_max_state(cdev, &max_state); - if (state++ < max_state) - cdev->ops->set_cur_state(cdev, state); - } - } else if (trend < 0) { /* Cooling off? */ - list_for_each_entry(instance, &tz->cooling_devices, - node) { - if (instance->trip != trip) - continue; - cdev = instance->cdev; - cdev->ops->get_cur_state(cdev, &state); - cdev->ops->get_max_state(cdev, &max_state); - if (state > 0) - cdev->ops->set_cur_state(cdev, --state); - } - } - return; - } - - /* - * Below Trip? - * ----------- - * Implement passive cooling hysteresis to slowly increase performance - * and avoid thrashing around the passive trip point. Note that we - * assume symmetry. - */ - list_for_each_entry(instance, &tz->cooling_devices, node) { - if (instance->trip != trip) - continue; - cdev = instance->cdev; - cdev->ops->get_cur_state(cdev, &state); - cdev->ops->get_max_state(cdev, &max_state); - if (state > 0) - cdev->ops->set_cur_state(cdev, --state); - if (state == 0) - tz->passive = false; - } + cancel_delayed_work(&tz->poll_queue); } static void thermal_zone_device_check(struct work_struct *work) @@ -794,12 +732,14 @@ static void thermal_zone_device_check(struct work_struct *work) */ int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz, int trip, - struct thermal_cooling_device *cdev) + struct thermal_cooling_device *cdev, + unsigned long upper, unsigned long lower) { - struct thermal_cooling_device_instance *dev; - struct thermal_cooling_device_instance *pos; + struct thermal_instance *dev; + struct thermal_instance *pos; struct thermal_zone_device *pos1; struct thermal_cooling_device *pos2; + unsigned long max_state; int result; if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE)) @@ -817,13 +757,26 @@ int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz, if (tz != pos1 || cdev != pos2) return -EINVAL; + cdev->ops->get_max_state(cdev, &max_state); + + /* lower default 0, upper default max_state */ + lower = lower == THERMAL_NO_LIMIT ? 0 : lower; + upper = upper == THERMAL_NO_LIMIT ? max_state : upper; + + if (lower > upper || upper > max_state) + return -EINVAL; + dev = - kzalloc(sizeof(struct thermal_cooling_device_instance), GFP_KERNEL); + kzalloc(sizeof(struct thermal_instance), GFP_KERNEL); if (!dev) return -ENOMEM; dev->tz = tz; dev->cdev = cdev; dev->trip = trip; + dev->upper = upper; + dev->lower = lower; + dev->target = THERMAL_NO_TARGET; + result = get_idr(&tz->idr, &tz->lock, &dev->id); if (result) goto free_mem; @@ -844,13 +797,17 @@ int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz, goto remove_symbol_link; mutex_lock(&tz->lock); - list_for_each_entry(pos, &tz->cooling_devices, node) + mutex_lock(&cdev->lock); + list_for_each_entry(pos, &tz->thermal_instances, tz_node) if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { result = -EEXIST; break; } - if (!result) - list_add_tail(&dev->node, &tz->cooling_devices); + if (!result) { + list_add_tail(&dev->tz_node, &tz->thermal_instances); + list_add_tail(&dev->cdev_node, &cdev->thermal_instances); + } + mutex_unlock(&cdev->lock); mutex_unlock(&tz->lock); if (!result) @@ -880,16 +837,20 @@ int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz, int trip, struct thermal_cooling_device *cdev) { - struct thermal_cooling_device_instance *pos, *next; + struct thermal_instance *pos, *next; mutex_lock(&tz->lock); - list_for_each_entry_safe(pos, next, &tz->cooling_devices, node) { + mutex_lock(&cdev->lock); + list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) { if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { - list_del(&pos->node); + list_del(&pos->tz_node); + list_del(&pos->cdev_node); + mutex_unlock(&cdev->lock); mutex_unlock(&tz->lock); goto unbind; } } + mutex_unlock(&cdev->lock); mutex_unlock(&tz->lock); return -ENODEV; @@ -937,7 +898,7 @@ thermal_cooling_device_register(char *type, void *devdata, struct thermal_zone_device *pos; int result; - if (strlen(type) >= THERMAL_NAME_LENGTH) + if (type && strlen(type) >= THERMAL_NAME_LENGTH) return ERR_PTR(-EINVAL); if (!ops || !ops->get_max_state || !ops->get_cur_state || @@ -954,8 +915,11 @@ thermal_cooling_device_register(char *type, void *devdata, return ERR_PTR(result); } - strcpy(cdev->type, type); + strcpy(cdev->type, type ? : ""); + mutex_init(&cdev->lock); + INIT_LIST_HEAD(&cdev->thermal_instances); cdev->ops = ops; + cdev->updated = true; cdev->device.class = &thermal_class; cdev->devdata = devdata; dev_set_name(&cdev->device, "cooling_device%d", cdev->id); @@ -1047,6 +1011,136 @@ void thermal_cooling_device_unregister(struct } EXPORT_SYMBOL(thermal_cooling_device_unregister); +static void thermal_cdev_do_update(struct thermal_cooling_device *cdev) +{ + struct thermal_instance *instance; + unsigned long target = 0; + + /* cooling device is updated*/ + if (cdev->updated) + return; + + mutex_lock(&cdev->lock); + /* Make sure cdev enters the deepest cooling state */ + list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) { + if (instance->target == THERMAL_NO_TARGET) + continue; + if (instance->target > target) + target = instance->target; + } + mutex_unlock(&cdev->lock); + cdev->ops->set_cur_state(cdev, target); + cdev->updated = true; +} + +static void thermal_zone_do_update(struct thermal_zone_device *tz) +{ + struct thermal_instance *instance; + + list_for_each_entry(instance, &tz->thermal_instances, tz_node) + thermal_cdev_do_update(instance->cdev); +} + +/* + * Cooling algorithm for both active and passive cooling + * + * 1. if the temperature is higher than a trip point, + * a. if the trend is THERMAL_TREND_RAISING, use higher cooling + * state for this trip point + * b. if the trend is THERMAL_TREND_DROPPING, use lower cooling + * state for this trip point + * + * 2. if the temperature is lower than a trip point, use lower + * cooling state for this trip point + * + * Note that this behaves the same as the previous passive cooling + * algorithm. + */ + +static void thermal_zone_trip_update(struct thermal_zone_device *tz, + int trip, long temp) +{ + struct thermal_instance *instance; + struct thermal_cooling_device *cdev = NULL; + unsigned long cur_state, max_state; + long trip_temp; + enum thermal_trip_type trip_type; + enum thermal_trend trend; + + if (trip == THERMAL_TRIPS_NONE) { + trip_temp = tz->forced_passive; + trip_type = THERMAL_TRIPS_NONE; + } else { + tz->ops->get_trip_temp(tz, trip, &trip_temp); + tz->ops->get_trip_type(tz, trip, &trip_type); + } + + if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) { + /* + * compare the current temperature and previous temperature + * to get the thermal trend, if no special requirement + */ + if (tz->temperature > tz->last_temperature) + trend = THERMAL_TREND_RAISING; + else if (tz->temperature < tz->last_temperature) + trend = THERMAL_TREND_DROPPING; + else + trend = THERMAL_TREND_STABLE; + } + + if (temp >= trip_temp) { + list_for_each_entry(instance, &tz->thermal_instances, tz_node) { + if (instance->trip != trip) + continue; + + cdev = instance->cdev; + + cdev->ops->get_cur_state(cdev, &cur_state); + cdev->ops->get_max_state(cdev, &max_state); + + if (trend == THERMAL_TREND_RAISING) { + cur_state = cur_state < instance->upper ? + (cur_state + 1) : instance->upper; + } else if (trend == THERMAL_TREND_DROPPING) { + cur_state = cur_state > instance->lower ? + (cur_state - 1) : instance->lower; + } + + /* activate a passive thermal instance */ + if ((trip_type == THERMAL_TRIP_PASSIVE || + trip_type == THERMAL_TRIPS_NONE) && + instance->target == THERMAL_NO_TARGET) + tz->passive++; + + instance->target = cur_state; + cdev->updated = false; /* cooling device needs update */ + } + } else { /* below trip */ + list_for_each_entry(instance, &tz->thermal_instances, tz_node) { + if (instance->trip != trip) + continue; + + /* Do not use the inactive thermal instance */ + if (instance->target == THERMAL_NO_TARGET) + continue; + cdev = instance->cdev; + cdev->ops->get_cur_state(cdev, &cur_state); + + cur_state = cur_state > instance->lower ? + (cur_state - 1) : THERMAL_NO_TARGET; + + /* deactivate a passive thermal instance */ + if ((trip_type == THERMAL_TRIP_PASSIVE || + trip_type == THERMAL_TRIPS_NONE) && + cur_state == THERMAL_NO_TARGET) + tz->passive--; + instance->target = cur_state; + cdev->updated = false; /* cooling device needs update */ + } + } + + return; +} /** * thermal_zone_device_update - force an update of a thermal zone's state * @ttz: the thermal zone to update @@ -1057,8 +1151,6 @@ void thermal_zone_device_update(struct thermal_zone_device *tz) int count, ret = 0; long temp, trip_temp; enum thermal_trip_type trip_type; - struct thermal_cooling_device_instance *instance; - struct thermal_cooling_device *cdev; mutex_lock(&tz->lock); @@ -1068,6 +1160,9 @@ void thermal_zone_device_update(struct thermal_zone_device *tz) goto leave; } + tz->last_temperature = tz->temperature; + tz->temperature = temp; + for (count = 0; count < tz->trips; count++) { tz->ops->get_trip_type(tz, count, &trip_type); tz->ops->get_trip_temp(tz, count, &trip_temp); @@ -1091,32 +1186,18 @@ void thermal_zone_device_update(struct thermal_zone_device *tz) tz->ops->notify(tz, count, trip_type); break; case THERMAL_TRIP_ACTIVE: - list_for_each_entry(instance, &tz->cooling_devices, - node) { - if (instance->trip != count) - continue; - - cdev = instance->cdev; - - if (temp >= trip_temp) - cdev->ops->set_cur_state(cdev, 1); - else - cdev->ops->set_cur_state(cdev, 0); - } + thermal_zone_trip_update(tz, count, temp); break; case THERMAL_TRIP_PASSIVE: if (temp >= trip_temp || tz->passive) - thermal_zone_device_passive(tz, temp, - trip_temp, count); + thermal_zone_trip_update(tz, count, temp); break; } } if (tz->forced_passive) - thermal_zone_device_passive(tz, temp, tz->forced_passive, - THERMAL_TRIPS_NONE); - - tz->last_temperature = temp; + thermal_zone_trip_update(tz, THERMAL_TRIPS_NONE, temp); + thermal_zone_do_update(tz); leave: if (tz->passive) @@ -1239,8 +1320,6 @@ static void remove_trip_attrs(struct thermal_zone_device *tz) * @mask: a bit string indicating the writeablility of trip points * @devdata: private device data * @ops: standard thermal zone device callbacks - * @tc1: thermal coefficient 1 for passive calculations - * @tc2: thermal coefficient 2 for passive calculations * @passive_delay: number of milliseconds to wait between polls when * performing passive cooling * @polling_delay: number of milliseconds to wait between polls when checking @@ -1248,13 +1327,12 @@ static void remove_trip_attrs(struct thermal_zone_device *tz) * driven systems) * * thermal_zone_device_unregister() must be called when the device is no - * longer needed. The passive cooling formula uses tc1 and tc2 as described in - * section 11.1.5.1 of the ACPI specification 3.0. + * longer needed. The passive cooling depends on the .get_trend() return value. */ struct thermal_zone_device *thermal_zone_device_register(const char *type, int trips, int mask, void *devdata, const struct thermal_zone_device_ops *ops, - int tc1, int tc2, int passive_delay, int polling_delay) + int passive_delay, int polling_delay) { struct thermal_zone_device *tz; struct thermal_cooling_device *pos; @@ -1263,7 +1341,7 @@ struct thermal_zone_device *thermal_zone_device_register(const char *type, int count; int passive = 0; - if (strlen(type) >= THERMAL_NAME_LENGTH) + if (type && strlen(type) >= THERMAL_NAME_LENGTH) return ERR_PTR(-EINVAL); if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips) @@ -1276,7 +1354,7 @@ struct thermal_zone_device *thermal_zone_device_register(const char *type, if (!tz) return ERR_PTR(-ENOMEM); - INIT_LIST_HEAD(&tz->cooling_devices); + INIT_LIST_HEAD(&tz->thermal_instances); idr_init(&tz->idr); mutex_init(&tz->lock); result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id); @@ -1285,13 +1363,11 @@ struct thermal_zone_device *thermal_zone_device_register(const char *type, return ERR_PTR(result); } - strcpy(tz->type, type); + strcpy(tz->type, type ? : ""); tz->ops = ops; tz->device.class = &thermal_class; tz->devdata = devdata; tz->trips = trips; - tz->tc1 = tc1; - tz->tc2 = tc2; tz->passive_delay = passive_delay; tz->polling_delay = polling_delay;