Commit 698a1eb1 authored by Rafael J. Wysocki's avatar Rafael J. Wysocki

thermal: core: Store zone ops in struct thermal_zone_device

The current code requires thermal zone creators to pass pointers to
writable ops structures to thermal_zone_device_register_with_trips()
which needs to modify the target struct thermal_zone_device_ops object
if the "critical" operation in it is NULL.

Moreover, the callers of thermal_zone_device_register_with_trips() are
required to hold on to the struct thermal_zone_device_ops object passed
to it until the given thermal zone is unregistered.

Both of these requirements are quite inconvenient, so modify struct
thermal_zone_device to contain struct thermal_zone_device_ops as field and
make thermal_zone_device_register_with_trips() copy the contents of the
struct thermal_zone_device_ops passed to it via a pointer (which can be
const now) to that field.

Also adjust the code using thermal zone ops accordingly and modify
thermal_of_zone_register() to use a local ops variable during
thermal zone registration so ops do not need to be freed in
thermal_of_zone_unregister() any more.
Signed-off-by: default avatarRafael J. Wysocki <rafael.j.wysocki@intel.com>
Reviewed-by: default avatarStanislaw Gruszka <stanislaw.gruszka@linux.intel.com>
Reviewed-by: default avatarDaniel Lezcano <daniel.lezcano@linaro.org>
parent fcbf8780
...@@ -356,9 +356,9 @@ static void handle_critical_trips(struct thermal_zone_device *tz, ...@@ -356,9 +356,9 @@ static void handle_critical_trips(struct thermal_zone_device *tz,
trace_thermal_zone_trip(tz, thermal_zone_trip_id(tz, trip), trip->type); trace_thermal_zone_trip(tz, thermal_zone_trip_id(tz, trip), trip->type);
if (trip->type == THERMAL_TRIP_CRITICAL) if (trip->type == THERMAL_TRIP_CRITICAL)
tz->ops->critical(tz); tz->ops.critical(tz);
else if (tz->ops->hot) else if (tz->ops.hot)
tz->ops->hot(tz); tz->ops.hot(tz);
} }
static void handle_thermal_trip(struct thermal_zone_device *tz, static void handle_thermal_trip(struct thermal_zone_device *tz,
...@@ -493,8 +493,8 @@ static int thermal_zone_device_set_mode(struct thermal_zone_device *tz, ...@@ -493,8 +493,8 @@ static int thermal_zone_device_set_mode(struct thermal_zone_device *tz,
return ret; return ret;
} }
if (tz->ops->change_mode) if (tz->ops.change_mode)
ret = tz->ops->change_mode(tz, mode); ret = tz->ops.change_mode(tz, mode);
if (!ret) if (!ret)
tz->mode = mode; tz->mode = mode;
...@@ -867,8 +867,8 @@ static void bind_cdev(struct thermal_cooling_device *cdev) ...@@ -867,8 +867,8 @@ static void bind_cdev(struct thermal_cooling_device *cdev)
struct thermal_zone_device *pos = NULL; struct thermal_zone_device *pos = NULL;
list_for_each_entry(pos, &thermal_tz_list, node) { list_for_each_entry(pos, &thermal_tz_list, node) {
if (pos->ops->bind) { if (pos->ops.bind) {
ret = pos->ops->bind(pos, cdev); ret = pos->ops.bind(pos, cdev);
if (ret) if (ret)
print_bind_err_msg(pos, cdev, ret); print_bind_err_msg(pos, cdev, ret);
} }
...@@ -1184,8 +1184,8 @@ void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev) ...@@ -1184,8 +1184,8 @@ void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
/* Unbind all thermal zones associated with 'this' cdev */ /* Unbind all thermal zones associated with 'this' cdev */
list_for_each_entry(tz, &thermal_tz_list, node) { list_for_each_entry(tz, &thermal_tz_list, node) {
if (tz->ops->unbind) if (tz->ops.unbind)
tz->ops->unbind(tz, cdev); tz->ops.unbind(tz, cdev);
} }
mutex_unlock(&thermal_list_lock); mutex_unlock(&thermal_list_lock);
...@@ -1199,13 +1199,13 @@ static void bind_tz(struct thermal_zone_device *tz) ...@@ -1199,13 +1199,13 @@ static void bind_tz(struct thermal_zone_device *tz)
int ret; int ret;
struct thermal_cooling_device *pos = NULL; struct thermal_cooling_device *pos = NULL;
if (!tz->ops->bind) if (!tz->ops.bind)
return; return;
mutex_lock(&thermal_list_lock); mutex_lock(&thermal_list_lock);
list_for_each_entry(pos, &thermal_cdev_list, node) { list_for_each_entry(pos, &thermal_cdev_list, node) {
ret = tz->ops->bind(tz, pos); ret = tz->ops.bind(tz, pos);
if (ret) if (ret)
print_bind_err_msg(tz, pos, ret); print_bind_err_msg(tz, pos, ret);
} }
...@@ -1224,8 +1224,8 @@ int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp) ...@@ -1224,8 +1224,8 @@ int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp)
{ {
int i, ret = -EINVAL; int i, ret = -EINVAL;
if (tz->ops->get_crit_temp) if (tz->ops.get_crit_temp)
return tz->ops->get_crit_temp(tz, temp); return tz->ops.get_crit_temp(tz, temp);
mutex_lock(&tz->lock); mutex_lock(&tz->lock);
...@@ -1273,7 +1273,7 @@ thermal_zone_device_register_with_trips(const char *type, ...@@ -1273,7 +1273,7 @@ thermal_zone_device_register_with_trips(const char *type,
const struct thermal_trip *trips, const struct thermal_trip *trips,
int num_trips, int mask, int num_trips, int mask,
void *devdata, void *devdata,
struct thermal_zone_device_ops *ops, const struct thermal_zone_device_ops *ops,
const struct thermal_zone_params *tzp, const struct thermal_zone_params *tzp,
int passive_delay, int polling_delay) int passive_delay, int polling_delay)
{ {
...@@ -1348,10 +1348,10 @@ thermal_zone_device_register_with_trips(const char *type, ...@@ -1348,10 +1348,10 @@ thermal_zone_device_register_with_trips(const char *type,
tz->id = id; tz->id = id;
strscpy(tz->type, type, sizeof(tz->type)); strscpy(tz->type, type, sizeof(tz->type));
if (!ops->critical) tz->ops = *ops;
ops->critical = thermal_zone_device_critical; if (!tz->ops.critical)
tz->ops.critical = thermal_zone_device_critical;
tz->ops = ops;
tz->device.class = thermal_class; tz->device.class = thermal_class;
tz->devdata = devdata; tz->devdata = devdata;
memcpy(tz->trips, trips, num_trips * sizeof(*trips)); memcpy(tz->trips, trips, num_trips * sizeof(*trips));
...@@ -1437,7 +1437,7 @@ EXPORT_SYMBOL_GPL(thermal_zone_device_register_with_trips); ...@@ -1437,7 +1437,7 @@ EXPORT_SYMBOL_GPL(thermal_zone_device_register_with_trips);
struct thermal_zone_device *thermal_tripless_zone_device_register( struct thermal_zone_device *thermal_tripless_zone_device_register(
const char *type, const char *type,
void *devdata, void *devdata,
struct thermal_zone_device_ops *ops, const struct thermal_zone_device_ops *ops,
const struct thermal_zone_params *tzp) const struct thermal_zone_params *tzp)
{ {
return thermal_zone_device_register_with_trips(type, NULL, 0, 0, devdata, return thermal_zone_device_register_with_trips(type, NULL, 0, 0, devdata,
...@@ -1499,8 +1499,8 @@ void thermal_zone_device_unregister(struct thermal_zone_device *tz) ...@@ -1499,8 +1499,8 @@ void thermal_zone_device_unregister(struct thermal_zone_device *tz)
/* Unbind all cdevs associated with 'this' thermal zone */ /* Unbind all cdevs associated with 'this' thermal zone */
list_for_each_entry(cdev, &thermal_cdev_list, node) list_for_each_entry(cdev, &thermal_cdev_list, node)
if (tz->ops->unbind) if (tz->ops.unbind)
tz->ops->unbind(tz, cdev); tz->ops.unbind(tz, cdev);
mutex_unlock(&thermal_list_lock); mutex_unlock(&thermal_list_lock);
......
...@@ -26,8 +26,8 @@ int get_tz_trend(struct thermal_zone_device *tz, const struct thermal_trip *trip ...@@ -26,8 +26,8 @@ int get_tz_trend(struct thermal_zone_device *tz, const struct thermal_trip *trip
{ {
enum thermal_trend trend; enum thermal_trend trend;
if (tz->emul_temperature || !tz->ops->get_trend || if (tz->emul_temperature || !tz->ops.get_trend ||
tz->ops->get_trend(tz, trip, &trend)) { tz->ops.get_trend(tz, trip, &trend)) {
if (tz->temperature > tz->last_temperature) if (tz->temperature > tz->last_temperature)
trend = THERMAL_TREND_RAISING; trend = THERMAL_TREND_RAISING;
else if (tz->temperature < tz->last_temperature) else if (tz->temperature < tz->last_temperature)
...@@ -75,7 +75,7 @@ EXPORT_SYMBOL(get_thermal_instance); ...@@ -75,7 +75,7 @@ EXPORT_SYMBOL(get_thermal_instance);
* temperature and fill @temp. * temperature and fill @temp.
* *
* Both tz and tz->ops must be valid pointers when calling this function, * Both tz and tz->ops must be valid pointers when calling this function,
* and the tz->ops->get_temp callback must be provided. * and the tz->ops.get_temp callback must be provided.
* The function must be called under tz->lock. * The function must be called under tz->lock.
* *
* Return: On success returns 0, an error code otherwise * Return: On success returns 0, an error code otherwise
...@@ -88,7 +88,7 @@ int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp) ...@@ -88,7 +88,7 @@ int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
lockdep_assert_held(&tz->lock); lockdep_assert_held(&tz->lock);
ret = tz->ops->get_temp(tz, temp); ret = tz->ops.get_temp(tz, temp);
if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) { if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) {
for_each_trip(tz, trip) { for_each_trip(tz, trip) {
...@@ -132,7 +132,7 @@ int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp) ...@@ -132,7 +132,7 @@ int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
mutex_lock(&tz->lock); mutex_lock(&tz->lock);
if (!tz->ops->get_temp) { if (!tz->ops.get_temp) {
ret = -EINVAL; ret = -EINVAL;
goto unlock; goto unlock;
} }
......
...@@ -80,7 +80,7 @@ temp_crit_show(struct device *dev, struct device_attribute *attr, char *buf) ...@@ -80,7 +80,7 @@ temp_crit_show(struct device *dev, struct device_attribute *attr, char *buf)
mutex_lock(&tz->lock); mutex_lock(&tz->lock);
ret = tz->ops->get_crit_temp(tz, &temperature); ret = tz->ops.get_crit_temp(tz, &temperature);
mutex_unlock(&tz->lock); mutex_unlock(&tz->lock);
...@@ -132,7 +132,7 @@ thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon, ...@@ -132,7 +132,7 @@ thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
static bool thermal_zone_crit_temp_valid(struct thermal_zone_device *tz) static bool thermal_zone_crit_temp_valid(struct thermal_zone_device *tz)
{ {
int temp; int temp;
return tz->ops->get_crit_temp && !tz->ops->get_crit_temp(tz, &temp); return tz->ops.get_crit_temp && !tz->ops.get_crit_temp(tz, &temp);
} }
int thermal_add_hwmon_sysfs(struct thermal_zone_device *tz) int thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
......
...@@ -438,11 +438,8 @@ static int thermal_of_unbind(struct thermal_zone_device *tz, ...@@ -438,11 +438,8 @@ static int thermal_of_unbind(struct thermal_zone_device *tz,
*/ */
static void thermal_of_zone_unregister(struct thermal_zone_device *tz) static void thermal_of_zone_unregister(struct thermal_zone_device *tz)
{ {
struct thermal_zone_device_ops *ops = tz->ops;
thermal_zone_device_disable(tz); thermal_zone_device_disable(tz);
thermal_zone_device_unregister(tz); thermal_zone_device_unregister(tz);
kfree(ops);
} }
/** /**
...@@ -468,33 +465,27 @@ static void thermal_of_zone_unregister(struct thermal_zone_device *tz) ...@@ -468,33 +465,27 @@ static void thermal_of_zone_unregister(struct thermal_zone_device *tz)
static struct thermal_zone_device *thermal_of_zone_register(struct device_node *sensor, int id, void *data, static struct thermal_zone_device *thermal_of_zone_register(struct device_node *sensor, int id, void *data,
const struct thermal_zone_device_ops *ops) const struct thermal_zone_device_ops *ops)
{ {
struct thermal_zone_device_ops of_ops = *ops;
struct thermal_zone_device *tz; struct thermal_zone_device *tz;
struct thermal_trip *trips; struct thermal_trip *trips;
struct thermal_zone_params tzp = {}; struct thermal_zone_params tzp = {};
struct thermal_zone_device_ops *of_ops;
struct device_node *np; struct device_node *np;
const char *action; const char *action;
int delay, pdelay; int delay, pdelay;
int ntrips, mask; int ntrips, mask;
int ret; int ret;
of_ops = kmemdup(ops, sizeof(*ops), GFP_KERNEL);
if (!of_ops)
return ERR_PTR(-ENOMEM);
np = of_thermal_zone_find(sensor, id); np = of_thermal_zone_find(sensor, id);
if (IS_ERR(np)) { if (IS_ERR(np)) {
if (PTR_ERR(np) != -ENODEV) if (PTR_ERR(np) != -ENODEV)
pr_err("Failed to find thermal zone for %pOFn id=%d\n", sensor, id); pr_err("Failed to find thermal zone for %pOFn id=%d\n", sensor, id);
ret = PTR_ERR(np); return ERR_CAST(np);
goto out_kfree_of_ops;
} }
trips = thermal_of_trips_init(np, &ntrips); trips = thermal_of_trips_init(np, &ntrips);
if (IS_ERR(trips)) { if (IS_ERR(trips)) {
pr_err("Failed to find trip points for %pOFn id=%d\n", sensor, id); pr_err("Failed to find trip points for %pOFn id=%d\n", sensor, id);
ret = PTR_ERR(trips); return ERR_CAST(trips);
goto out_kfree_of_ops;
} }
ret = thermal_of_monitor_init(np, &delay, &pdelay); ret = thermal_of_monitor_init(np, &delay, &pdelay);
...@@ -505,18 +496,18 @@ static struct thermal_zone_device *thermal_of_zone_register(struct device_node * ...@@ -505,18 +496,18 @@ static struct thermal_zone_device *thermal_of_zone_register(struct device_node *
thermal_of_parameters_init(np, &tzp); thermal_of_parameters_init(np, &tzp);
of_ops->bind = thermal_of_bind; of_ops.bind = thermal_of_bind;
of_ops->unbind = thermal_of_unbind; of_ops.unbind = thermal_of_unbind;
mask = GENMASK_ULL((ntrips) - 1, 0); mask = GENMASK_ULL((ntrips) - 1, 0);
ret = of_property_read_string(np, "critical-action", &action); ret = of_property_read_string(np, "critical-action", &action);
if (!ret) if (!ret)
if (!of_ops->critical && !strcasecmp(action, "reboot")) if (!of_ops.critical && !strcasecmp(action, "reboot"))
of_ops->critical = thermal_zone_device_critical_reboot; of_ops.critical = thermal_zone_device_critical_reboot;
tz = thermal_zone_device_register_with_trips(np->name, trips, ntrips, tz = thermal_zone_device_register_with_trips(np->name, trips, ntrips,
mask, data, of_ops, &tzp, mask, data, &of_ops, &tzp,
pdelay, delay); pdelay, delay);
if (IS_ERR(tz)) { if (IS_ERR(tz)) {
ret = PTR_ERR(tz); ret = PTR_ERR(tz);
...@@ -538,8 +529,6 @@ static struct thermal_zone_device *thermal_of_zone_register(struct device_node * ...@@ -538,8 +529,6 @@ static struct thermal_zone_device *thermal_of_zone_register(struct device_node *
out_kfree_trips: out_kfree_trips:
kfree(trips); kfree(trips);
out_kfree_of_ops:
kfree(of_ops);
return ERR_PTR(ret); return ERR_PTR(ret);
} }
......
...@@ -123,8 +123,8 @@ trip_point_temp_store(struct device *dev, struct device_attribute *attr, ...@@ -123,8 +123,8 @@ trip_point_temp_store(struct device *dev, struct device_attribute *attr,
trip = &tz->trips[trip_id]; trip = &tz->trips[trip_id];
if (temp != trip->temperature) { if (temp != trip->temperature) {
if (tz->ops->set_trip_temp) { if (tz->ops.set_trip_temp) {
ret = tz->ops->set_trip_temp(tz, trip_id, temp); ret = tz->ops.set_trip_temp(tz, trip_id, temp);
if (ret) if (ret)
goto unlock; goto unlock;
} }
...@@ -174,8 +174,8 @@ trip_point_hyst_store(struct device *dev, struct device_attribute *attr, ...@@ -174,8 +174,8 @@ trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
trip = &tz->trips[trip_id]; trip = &tz->trips[trip_id];
if (hyst != trip->hysteresis) { if (hyst != trip->hysteresis) {
if (tz->ops->set_trip_hyst) { if (tz->ops.set_trip_hyst) {
ret = tz->ops->set_trip_hyst(tz, trip_id, hyst); ret = tz->ops.set_trip_hyst(tz, trip_id, hyst);
if (ret) if (ret)
goto unlock; goto unlock;
} }
...@@ -250,10 +250,10 @@ emul_temp_store(struct device *dev, struct device_attribute *attr, ...@@ -250,10 +250,10 @@ emul_temp_store(struct device *dev, struct device_attribute *attr,
mutex_lock(&tz->lock); mutex_lock(&tz->lock);
if (!tz->ops->set_emul_temp) if (!tz->ops.set_emul_temp)
tz->emul_temperature = temperature; tz->emul_temperature = temperature;
else else
ret = tz->ops->set_emul_temp(tz, temperature); ret = tz->ops.set_emul_temp(tz, temperature);
if (!ret) if (!ret)
__thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
...@@ -474,7 +474,7 @@ static int create_trip_attrs(struct thermal_zone_device *tz, int mask) ...@@ -474,7 +474,7 @@ static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
tz->trip_hyst_attrs[indx].name; tz->trip_hyst_attrs[indx].name;
tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO; tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show; tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
if (tz->ops->set_trip_hyst) { if (tz->ops.set_trip_hyst) {
tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR; tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
tz->trip_hyst_attrs[indx].attr.store = tz->trip_hyst_attrs[indx].attr.store =
trip_point_hyst_store; trip_point_hyst_store;
......
...@@ -70,7 +70,7 @@ void __thermal_zone_set_trips(struct thermal_zone_device *tz) ...@@ -70,7 +70,7 @@ void __thermal_zone_set_trips(struct thermal_zone_device *tz)
lockdep_assert_held(&tz->lock); lockdep_assert_held(&tz->lock);
if (!tz->ops->set_trips) if (!tz->ops.set_trips)
return; return;
for_each_trip(tz, trip) { for_each_trip(tz, trip) {
...@@ -114,7 +114,7 @@ void __thermal_zone_set_trips(struct thermal_zone_device *tz) ...@@ -114,7 +114,7 @@ void __thermal_zone_set_trips(struct thermal_zone_device *tz)
* Set a temperature window. When this window is left the driver * Set a temperature window. When this window is left the driver
* must inform the thermal core via thermal_zone_device_update. * must inform the thermal core via thermal_zone_device_update.
*/ */
ret = tz->ops->set_trips(tz, low, high); ret = tz->ops.set_trips(tz, low, high);
if (ret) if (ret)
dev_err(&tz->device, "Failed to set trips: %d\n", ret); dev_err(&tz->device, "Failed to set trips: %d\n", ret);
} }
......
...@@ -182,7 +182,7 @@ struct thermal_zone_device { ...@@ -182,7 +182,7 @@ struct thermal_zone_device {
int prev_low_trip; int prev_low_trip;
int prev_high_trip; int prev_high_trip;
atomic_t need_update; atomic_t need_update;
struct thermal_zone_device_ops *ops; struct thermal_zone_device_ops ops;
struct thermal_zone_params *tzp; struct thermal_zone_params *tzp;
struct thermal_governor *governor; struct thermal_governor *governor;
void *governor_data; void *governor_data;
...@@ -318,14 +318,14 @@ struct thermal_zone_device *thermal_zone_device_register_with_trips( ...@@ -318,14 +318,14 @@ struct thermal_zone_device *thermal_zone_device_register_with_trips(
const struct thermal_trip *trips, const struct thermal_trip *trips,
int num_trips, int mask, int num_trips, int mask,
void *devdata, void *devdata,
struct thermal_zone_device_ops *ops, const struct thermal_zone_device_ops *ops,
const struct thermal_zone_params *tzp, const struct thermal_zone_params *tzp,
int passive_delay, int polling_delay); int passive_delay, int polling_delay);
struct thermal_zone_device *thermal_tripless_zone_device_register( struct thermal_zone_device *thermal_tripless_zone_device_register(
const char *type, const char *type,
void *devdata, void *devdata,
struct thermal_zone_device_ops *ops, const struct thermal_zone_device_ops *ops,
const struct thermal_zone_params *tzp); const struct thermal_zone_params *tzp);
void thermal_zone_device_unregister(struct thermal_zone_device *tz); void thermal_zone_device_unregister(struct thermal_zone_device *tz);
...@@ -378,7 +378,7 @@ static inline struct thermal_zone_device *thermal_zone_device_register_with_trip ...@@ -378,7 +378,7 @@ static inline struct thermal_zone_device *thermal_zone_device_register_with_trip
const struct thermal_trip *trips, const struct thermal_trip *trips,
int num_trips, int mask, int num_trips, int mask,
void *devdata, void *devdata,
struct thermal_zone_device_ops *ops, const struct thermal_zone_device_ops *ops,
const struct thermal_zone_params *tzp, const struct thermal_zone_params *tzp,
int passive_delay, int polling_delay) int passive_delay, int polling_delay)
{ return ERR_PTR(-ENODEV); } { return ERR_PTR(-ENODEV); }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment