mbox series

[v7,00/29] Rework the trip points creation

Message ID 20220928210059.891387-1-daniel.lezcano@linaro.org
Headers show
Series Rework the trip points creation | expand

Message

Daniel Lezcano Sept. 28, 2022, 9 p.m. UTC
This work is the pre-requisite of handling correctly when the trip
point are crossed. For that we need to rework how the trip points are
declared and assigned to a thermal zone.

Even if it appears to be a common sense to have the trip points being
ordered, this no guarantee neither documentation telling that is the
case.

One solution could have been to create an ordered array of trips built
when registering the thermal zone by calling the different get_trip*
ops. However those ops receive a thermal zone pointer which is not
known as it is in the process of creating it.

This cyclic dependency shows we have to rework how we manage the trip
points.

Actually, all the trip points definition can be common to the backend
sensor drivers and we can factor out the thermal trip structure in all
of them.

Then, as we register the thermal trips array, they will be available
in the thermal zone structure and a core function can return the trip
given its id.

The get_trip_* ops won't be needed anymore and could be removed. The
resulting code will be another step forward to a self encapsulated
generic thermal framework.

Most of the drivers can be converted more or less easily. This series
does a first round with most of the drivers. Some remain and will be
converted but with a smaller set of changes as the conversion is a bit
more complex.

Changelog:
 v7:
    - Added missing return 0 in the x86_pkg_temp driver
 v6:
    - Improved the code for the get_crit_temp() function as suggested by Rafael
    - Removed inner parenthesis in the set_trip_temp() function and invert the
      conditions. Check the type of the trip point is unchanged
    - Folded patch 4 with 1
    - Add per thermal zone info message in the bang-bang governor
    - Folded the fix for an uninitialized variable in int340x_thermal_zone_add()
 v5:
    - Fixed a deadlock when calling thermal_zone_get_trip() while
      handling the thermal zone lock
    - Remove an extra line in the sysfs change
    - Collected tags
v4:
   - Remove extra lines on exynos changes as reported by Krzysztof Kozlowski
   - Collected tags
 v3:
   - Reorg the series to be git-bisect safe
   - Added the set_trip generic function
   - Added the get_crit_temp generic function
   - Removed more dead code in the thermal-of
   - Fixed the exynos changelog
   - Fixed the error check for the exynos drivers
   - Collected tags
 v2:
   - Added missing EXPORT_SYMBOL_GPL() for thermal_zone_get_trip()
   - Removed tab whitespace in the acerhdf driver
   - Collected tags

Cc: Raju Rangoju <rajur@chelsio.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Eric Dumazet <edumazet@google.com>
Cc: Jakub Kicinski <kuba@kernel.org>
Cc: Paolo Abeni <pabeni@redhat.com>
Cc: Peter Kaestle <peter@piie.net>
Cc: Hans de Goede <hdegoede@redhat.com>
Cc: Mark Gross <markgross@kernel.org>
Cc: Miquel Raynal <miquel.raynal@bootlin.com>
Cc: "Rafael J. Wysocki" <rafael@kernel.org>
Cc: Daniel Lezcano <daniel.lezcano@linaro.org>
Cc: Amit Kucheria <amitk@kernel.org>
Cc: Zhang Rui <rui.zhang@intel.com>
Cc: Nicolas Saenz Julienne <nsaenz@kernel.org>
Cc: Broadcom Kernel Team <bcm-kernel-feedback-list@broadcom.com>
Cc: Florian Fainelli <f.fainelli@gmail.com>
Cc: Ray Jui <rjui@broadcom.com>
Cc: Scott Branden <sbranden@broadcom.com>
Cc: Support Opensource <support.opensource@diasemi.com>
Cc: Lukasz Luba <lukasz.luba@arm.com>
Cc: Shawn Guo <shawnguo@kernel.org>
Cc: Sascha Hauer <s.hauer@pengutronix.de>
Cc: Pengutronix Kernel Team <kernel@pengutronix.de>
Cc: Fabio Estevam <festevam@gmail.com>
Cc: NXP Linux Team <linux-imx@nxp.com>
Cc: Thara Gopinath <thara.gopinath@linaro.org>
Cc: Andy Gross <agross@kernel.org>
Cc: Bjorn Andersson <bjorn.andersson@linaro.org>
Cc: "Niklas Söderlund" <niklas.soderlund@ragnatech.se>
Cc: Bartlomiej Zolnierkiewicz <bzolnier@gmail.com>
Cc: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
Cc: Alim Akhtar <alim.akhtar@samsung.com>
Cc: Thierry Reding <thierry.reding@gmail.com>
Cc: Jonathan Hunter <jonathanh@nvidia.com>
Cc: Eduardo Valentin <edubezval@gmail.com>
Cc: Keerthy <j-keerthy@ti.com>
Cc: Kunihiko Hayashi <hayashi.kunihiko@socionext.com>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Antoine Tenart <atenart@kernel.org>
Cc: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
Cc: Dmitry Osipenko <digetx@gmail.com>
Cc: netdev@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Cc: platform-driver-x86@vger.kernel.org
Cc: linux-pm@vger.kernel.org
Cc: linux-rpi-kernel@lists.infradead.org
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-arm-msm@vger.kernel.org
Cc: linux-renesas-soc@vger.kernel.org
Cc: linux-samsung-soc@vger.kernel.org
Cc: linux-tegra@vger.kernel.org
Cc: linux-omap@vger.kernel.org

Daniel Lezcano (29):
  thermal/core: Add a generic thermal_zone_get_trip() function
  thermal/sysfs: Always expose hysteresis attributes
  thermal/core: Add a generic thermal_zone_set_trip() function
  thermal/core/governors: Use thermal_zone_get_trip() instead of ops
    functions
  thermal/of: Use generic thermal_zone_get_trip() function
  thermal/of: Remove unused functions
  thermal/drivers/exynos: Use generic thermal_zone_get_trip() function
  thermal/drivers/exynos: of_thermal_get_ntrips()
  thermal/drivers/exynos: Replace of_thermal_is_trip_valid() by
    thermal_zone_get_trip()
  thermal/drivers/tegra: Use generic thermal_zone_get_trip() function
  thermal/drivers/uniphier: Use generic thermal_zone_get_trip() function
  thermal/drivers/hisi: Use generic thermal_zone_get_trip() function
  thermal/drivers/qcom: Use generic thermal_zone_get_trip() function
  thermal/drivers/armada: Use generic thermal_zone_get_trip() function
  thermal/drivers/rcar_gen3: Use the generic function to get the number
    of trips
  thermal/of: Remove of_thermal_get_ntrips()
  thermal/of: Remove of_thermal_is_trip_valid()
  thermal/of: Remove of_thermal_set_trip_hyst()
  thermal/of: Remove of_thermal_get_crit_temp()
  thermal/drivers/st: Use generic trip points
  thermal/drivers/imx: Use generic thermal_zone_get_trip() function
  thermal/drivers/rcar: Use generic thermal_zone_get_trip() function
  thermal/drivers/broadcom: Use generic thermal_zone_get_trip() function
  thermal/drivers/da9062: Use generic thermal_zone_get_trip() function
  thermal/drivers/ti: Remove unused macros ti_thermal_get_trip_value() /
    ti_thermal_trip_is_valid()
  thermal/drivers/acerhdf: Use generic thermal_zone_get_trip() function
  thermal/drivers/cxgb4: Use generic thermal_zone_get_trip() function
  thermal/intel/int340x: Replace parameter to simplify
  thermal/drivers/intel: Use generic thermal_zone_get_trip() function

 drivers/net/ethernet/chelsio/cxgb4/cxgb4.h    |   2 -
 .../ethernet/chelsio/cxgb4/cxgb4_thermal.c    |  41 +----
 drivers/platform/x86/acerhdf.c                |  73 +++-----
 drivers/thermal/armada_thermal.c              |  39 ++---
 drivers/thermal/broadcom/bcm2835_thermal.c    |   8 +-
 drivers/thermal/da9062-thermal.c              |  52 +-----
 drivers/thermal/gov_bang_bang.c               |  39 +++--
 drivers/thermal/gov_fair_share.c              |  18 +-
 drivers/thermal/gov_power_allocator.c         |  51 +++---
 drivers/thermal/gov_step_wise.c               |  22 ++-
 drivers/thermal/hisi_thermal.c                |  11 +-
 drivers/thermal/imx_thermal.c                 |  72 +++-----
 .../int340x_thermal/int340x_thermal_zone.c    |  33 ++--
 .../int340x_thermal/int340x_thermal_zone.h    |   4 +-
 .../processor_thermal_device.c                |  10 +-
 drivers/thermal/intel/x86_pkg_temp_thermal.c  | 120 +++++++------
 drivers/thermal/qcom/qcom-spmi-temp-alarm.c   |  39 ++---
 drivers/thermal/rcar_gen3_thermal.c           |   2 +-
 drivers/thermal/rcar_thermal.c                |  53 +-----
 drivers/thermal/samsung/exynos_tmu.c          |  57 +++----
 drivers/thermal/st/st_thermal.c               |  47 +----
 drivers/thermal/tegra/soctherm.c              |  33 ++--
 drivers/thermal/tegra/tegra30-tsensor.c       |  17 +-
 drivers/thermal/thermal_core.c                | 161 +++++++++++++++---
 drivers/thermal/thermal_core.h                |  24 +--
 drivers/thermal/thermal_helpers.c             |  28 +--
 drivers/thermal/thermal_netlink.c             |  21 +--
 drivers/thermal/thermal_of.c                  | 116 -------------
 drivers/thermal/thermal_sysfs.c               | 133 +++++----------
 drivers/thermal/ti-soc-thermal/ti-thermal.h   |  15 --
 drivers/thermal/uniphier_thermal.c            |  27 ++-
 include/linux/thermal.h                       |  10 ++
 32 files changed, 560 insertions(+), 818 deletions(-)

Comments

Krzysztof Kozlowski Sept. 29, 2022, 7:31 a.m. UTC | #1
On 28/09/2022 23:00, Daniel Lezcano wrote:
> The thermal_zone_get_trip() does the same check as
> of_thermal_is_trip_valid(). Replace the call to
> of_thermal_is_trip_valid() by thermal_zone_get_trip().
> 
> Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>

I think I acked it...

Acked-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>

Best regards,
Krzysztof
Dmitry Baryshkov Sept. 29, 2022, 1:31 p.m. UTC | #2
On 29/09/2022 00:00, Daniel Lezcano wrote:
> The thermal framework gives the possibility to register the trip
> points with the thermal zone. When that is done, no get_trip_* ops are
> needed and they can be removed.
> 
> Convert ops content logic into generic trip points and register them with the
> thermal zone.
> 
> Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
> Acked-by: Amit Kucheria <amitk@kernel.org>

Reviewed-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>

> ---
>   drivers/thermal/qcom/qcom-spmi-temp-alarm.c | 39 +++++++++------------
>   1 file changed, 17 insertions(+), 22 deletions(-)
>
Daniel Lezcano Sept. 29, 2022, 2:57 p.m. UTC | #3
On 29/09/2022 15:58, Rafael J. Wysocki wrote:
> On Thu, Sep 29, 2022 at 2:26 PM Daniel Lezcano
> <daniel.lezcano@linaro.org> wrote:
>>
>>
>> Hi Rafael,
>>
>> are you happy with the changes?
> 
> I'll have a look and let you know.

Great, thanks
> 
>> I would like to integrate those changes with the thermal pull request
> 
> Sure, but it looks like you've got only a few ACKs for these patches
> from the driver people.
> 
> Wouldn't it be prudent to give them some more time to review the changes?

Well I would say I received the ACKs from the drivers which are actively 
maintained. Others are either not with a dedicated maintainer or not a 
reactive one. The first iteration of the series is from August 5th. So 
it has been 2 months.

I pinged for imx, armada and tegra two weeks ago.

The st, hisilicon drivers fall under the thermal maintainers umbrella

There are three series coming after this series to be posted. I would 
like to go forward in the process of cleaning up the framework. IMO two 
months is enough to let the maintainers pay attention to the changes, 
especially if we do a gentle ping and there are seven versions.

And after that comes the thermal_zone_device_register() parameters 
simplification :)

[ ... ]
Florian Fainelli Sept. 29, 2022, 5:34 p.m. UTC | #4
On 9/28/22 14:00, Daniel Lezcano wrote:
> The thermal framework gives the possibility to register the trip
> points with the thermal zone. When that is done, no get_trip_* ops are
> needed and they can be removed.
> 
> Convert ops content logic into generic trip points and register them with the
> thermal zone.
> 
> Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>

Acked-by: Florian Fainelli <f.fainelli@gmail.com>
Rafael J. Wysocki Sept. 29, 2022, 7:35 p.m. UTC | #5
On Thu, Sep 29, 2022 at 4:57 PM Daniel Lezcano
<daniel.lezcano@linaro.org> wrote:
>
> On 29/09/2022 15:58, Rafael J. Wysocki wrote:
> > On Thu, Sep 29, 2022 at 2:26 PM Daniel Lezcano
> > <daniel.lezcano@linaro.org> wrote:
> >>
> >>
> >> Hi Rafael,
> >>
> >> are you happy with the changes?
> >
> > I'll have a look and let you know.
>
> Great, thanks

Well, because you have not added the history of changes to the
patches, that will take more time than it would otherwise.

Generally, please always add information on what has changed in the
patch between different versions of it.

> >> I would like to integrate those changes with the thermal pull request
> >
> > Sure, but it looks like you've got only a few ACKs for these patches
> > from the driver people.
> >
> > Wouldn't it be prudent to give them some more time to review the changes?
>
> Well I would say I received the ACKs from the drivers which are actively
> maintained. Others are either not with a dedicated maintainer or not a
> reactive one. The first iteration of the series is from August 5th. So
> it has been 2 months.
>
> I pinged for imx, armada and tegra two weeks ago.

OK

> The st, hisilicon drivers fall under the thermal maintainers umbrella
>
> There are three series coming after this series to be posted. I would
> like to go forward in the process of cleaning up the framework. IMO two
> months is enough to let the maintainers pay attention to the changes,
> especially if we do a gentle ping and there are seven versions.
>
> And after that comes the thermal_zone_device_register() parameters
> simplification :)

Well, that's all fine, but I don't want people to get surprised by
significant changes they haven't expected and I want to avoid missing
anything subtle.
Rafael J. Wysocki Sept. 30, 2022, 5:20 p.m. UTC | #6
On Wed, Sep 28, 2022 at 11:01 PM Daniel Lezcano
<daniel.lezcano@linaro.org> wrote:
>
> The thermal_zone_device_ops structure defines a set of ops family,
> get_trip_temp(), get_trip_hyst(), get_trip_type(). Each of them is
> returning a property of a trip point.
>
> The result is the code is calling the ops everywhere to get a trip
> point which is supposed to be defined in the backend driver. It is a
> non-sense as a thermal trip can be generic and used by the backend
> driver to declare its trip points.
>
> Part of the thermal framework has been changed and all the OF thermal
> drivers are using the same definition for the trip point and use a
> thermal zone registration variant to pass those trip points which are
> part of the thermal zone device structure.
>
> Consequently, we can use a generic function to get the trip points
> when they are stored in the thermal zone device structure.
>
> This approach can be generalized to all the drivers and we can get rid
> of the ops->get_trip_*. That will result to a much more simpler code
> and make possible to rework how the thermal trip are handled in the
> thermal core framework as discussed previously.
>
> This change adds a function thermal_zone_get_trip() where we get the
> thermal trip point structure which contains all the properties (type,
> temp, hyst) instead of doing multiple calls to ops->get_trip_*.
>
> That opens the door for trip point extension with more attributes. For
> instance, replacing the trip points disabled bitmask with a 'disabled'
> field in the structure.
>
> Here we replace all the calls to ops->get_trip_* in the thermal core
> code with a call to the thermal_zone_get_trip() function.
>
> The thermal zone ops defines a callback to retrieve the critical
> temperature. As the trip handling is being reworked, all the trip
> points will be the same whatever the driver and consequently finding
> the critical trip temperature will be just a loop to search for a
> critical trip point type.
>
> Provide such a generic function, so we encapsulate the ops
> get_crit_temp() which can be removed when all the backend drivers are
> using the generic trip points handling.
>
> While at it, add the thermal_zone_get_num_trips() to encapsulate the
> code more and reduce the grip with the thermal framework internals.
>
> Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
> Reviewed-by: Zhang Rui <rui.zhang@intel.com>

Acked-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>

> ---
>  drivers/thermal/thermal_core.c    | 114 ++++++++++++++++++++++++------
>  drivers/thermal/thermal_core.h    |   2 +
>  drivers/thermal/thermal_helpers.c |  28 ++++----
>  drivers/thermal/thermal_netlink.c |  21 +++---
>  drivers/thermal/thermal_sysfs.c   |  66 +++++++----------
>  include/linux/thermal.h           |   7 ++
>  6 files changed, 149 insertions(+), 89 deletions(-)
>
> diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
> index 4eab77d0e59c..16ef91dc102f 100644
> --- a/drivers/thermal/thermal_core.c
> +++ b/drivers/thermal/thermal_core.c
> @@ -341,35 +341,31 @@ static void handle_critical_trips(struct thermal_zone_device *tz,
>                 tz->ops->critical(tz);
>  }
>
> -static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
> +static void handle_thermal_trip(struct thermal_zone_device *tz, int trip_id)
>  {
> -       enum thermal_trip_type type;
> -       int trip_temp, hyst = 0;
> +       struct thermal_trip trip;
>
>         /* Ignore disabled trip points */
> -       if (test_bit(trip, &tz->trips_disabled))
> +       if (test_bit(trip_id, &tz->trips_disabled))
>                 return;
>
> -       tz->ops->get_trip_temp(tz, trip, &trip_temp);
> -       tz->ops->get_trip_type(tz, trip, &type);
> -       if (tz->ops->get_trip_hyst)
> -               tz->ops->get_trip_hyst(tz, trip, &hyst);
> +       __thermal_zone_get_trip(tz, trip_id, &trip);
>
>         if (tz->last_temperature != THERMAL_TEMP_INVALID) {
> -               if (tz->last_temperature < trip_temp &&
> -                   tz->temperature >= trip_temp)
> -                       thermal_notify_tz_trip_up(tz->id, trip,
> +               if (tz->last_temperature < trip.temperature &&
> +                   tz->temperature >= trip.temperature)
> +                       thermal_notify_tz_trip_up(tz->id, trip_id,
>                                                   tz->temperature);
> -               if (tz->last_temperature >= trip_temp &&
> -                   tz->temperature < (trip_temp - hyst))
> -                       thermal_notify_tz_trip_down(tz->id, trip,
> +               if (tz->last_temperature >= trip.temperature &&
> +                   tz->temperature < (trip.temperature - trip.hysteresis))
> +                       thermal_notify_tz_trip_down(tz->id, trip_id,
>                                                     tz->temperature);
>         }
>
> -       if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
> -               handle_critical_trips(tz, trip, trip_temp, type);
> +       if (trip.type == THERMAL_TRIP_CRITICAL || trip.type == THERMAL_TRIP_HOT)
> +               handle_critical_trips(tz, trip_id, trip.temperature, trip.type);
>         else
> -               handle_non_critical_trips(tz, trip);
> +               handle_non_critical_trips(tz, trip_id);
>  }
>
>  static void update_temperature(struct thermal_zone_device *tz)
> @@ -1142,6 +1138,79 @@ static void thermal_set_delay_jiffies(unsigned long *delay_jiffies, int delay_ms
>                 *delay_jiffies = round_jiffies(*delay_jiffies);
>  }
>
> +int thermal_zone_get_num_trips(struct thermal_zone_device *tz)
> +{
> +       return tz->num_trips;
> +}
> +EXPORT_SYMBOL_GPL(thermal_zone_get_num_trips);
> +
> +int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp)
> +{
> +       int i, ret = -EINVAL;
> +
> +       if (tz->ops->get_crit_temp)
> +               return tz->ops->get_crit_temp(tz, temp);
> +
> +       if (!tz->trips)
> +               return -EINVAL;
> +
> +       mutex_lock(&tz->lock);
> +
> +       for (i = 0; i < tz->num_trips; i++) {
> +               if (tz->trips[i].type == THERMAL_TRIP_CRITICAL) {
> +                       *temp = tz->trips[i].temperature;
> +                       ret = 0;
> +                       break;
> +               }
> +       }
> +
> +       mutex_unlock(&tz->lock);
> +
> +       return ret;
> +}
> +EXPORT_SYMBOL_GPL(thermal_zone_get_crit_temp);
> +
> +int __thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
> +                           struct thermal_trip *trip)
> +{
> +       int ret;
> +
> +       if (!tz || trip_id < 0 || trip_id >= tz->num_trips || !trip)
> +               return -EINVAL;
> +
> +       if (tz->trips) {
> +               *trip = tz->trips[trip_id];
> +               return 0;
> +       }
> +
> +       if (tz->ops->get_trip_hyst) {
> +               ret = tz->ops->get_trip_hyst(tz, trip_id, &trip->hysteresis);
> +               if (ret)
> +                       return ret;
> +       } else {
> +               trip->hysteresis = 0;
> +       }
> +
> +       ret = tz->ops->get_trip_temp(tz, trip_id, &trip->temperature);
> +       if (ret)
> +               return ret;
> +
> +       return tz->ops->get_trip_type(tz, trip_id, &trip->type);
> +}
> +
> +int thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
> +                         struct thermal_trip *trip)
> +{
> +       int ret;
> +
> +       mutex_lock(&tz->lock);
> +       ret = __thermal_zone_get_trip(tz, trip_id, trip);
> +       mutex_unlock(&tz->lock);
> +
> +       return ret;
> +}
> +EXPORT_SYMBOL_GPL(thermal_zone_get_trip);
> +
>  /**
>   * thermal_zone_device_register_with_trips() - register a new thermal zone device
>   * @type:      the thermal zone device type
> @@ -1174,8 +1243,6 @@ thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *t
>                                         int polling_delay)
>  {
>         struct thermal_zone_device *tz;
> -       enum thermal_trip_type trip_type;
> -       int trip_temp;
>         int id;
>         int result;
>         int count;
> @@ -1202,7 +1269,7 @@ thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *t
>                 return ERR_PTR(-EINVAL);
>         }
>
> -       if (num_trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
> +       if (num_trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp) && !trips)
>                 return ERR_PTR(-EINVAL);
>
>         tz = kzalloc(sizeof(*tz), GFP_KERNEL);
> @@ -1252,9 +1319,10 @@ thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *t
>                 goto release_device;
>
>         for (count = 0; count < num_trips; count++) {
> -               if (tz->ops->get_trip_type(tz, count, &trip_type) ||
> -                   tz->ops->get_trip_temp(tz, count, &trip_temp) ||
> -                   !trip_temp)
> +               struct thermal_trip trip;
> +
> +               result = thermal_zone_get_trip(tz, count, &trip);
> +               if (result)
>                         set_bit(count, &tz->trips_disabled);
>         }
>
> diff --git a/drivers/thermal/thermal_core.h b/drivers/thermal/thermal_core.h
> index 1571917bd3c8..879e91a48435 100644
> --- a/drivers/thermal/thermal_core.h
> +++ b/drivers/thermal/thermal_core.h
> @@ -113,6 +113,8 @@ int thermal_build_list_of_policies(char *buf);
>  /* Helpers */
>  void thermal_zone_set_trips(struct thermal_zone_device *tz);
>  void __thermal_zone_set_trips(struct thermal_zone_device *tz);
> +int __thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
> +                           struct thermal_trip *trip);
>  int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp);
>
>  /* sysfs I/F */
> diff --git a/drivers/thermal/thermal_helpers.c b/drivers/thermal/thermal_helpers.c
> index c65cdce8f856..06dded36e95e 100644
> --- a/drivers/thermal/thermal_helpers.c
> +++ b/drivers/thermal/thermal_helpers.c
> @@ -69,7 +69,7 @@ int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
>         int ret = -EINVAL;
>         int count;
>         int crit_temp = INT_MAX;
> -       enum thermal_trip_type type;
> +       struct thermal_trip trip;
>
>         lockdep_assert_held(&tz->lock);
>
> @@ -80,10 +80,9 @@ int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
>
>         if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) {
>                 for (count = 0; count < tz->num_trips; count++) {
> -                       ret = tz->ops->get_trip_type(tz, count, &type);
> -                       if (!ret && type == THERMAL_TRIP_CRITICAL) {
> -                               ret = tz->ops->get_trip_temp(tz, count,
> -                                               &crit_temp);
> +                       ret = __thermal_zone_get_trip(tz, count, &trip);
> +                       if (!ret && trip.type == THERMAL_TRIP_CRITICAL) {
> +                               crit_temp = trip.temperature;
>                                 break;
>                         }
>                 }
> @@ -124,29 +123,30 @@ EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
>
>  void __thermal_zone_set_trips(struct thermal_zone_device *tz)
>  {
> -       int low = -INT_MAX;
> -       int high = INT_MAX;
> -       int trip_temp, hysteresis;
> +       struct thermal_trip trip;
> +       int low = -INT_MAX, high = INT_MAX;
>         int i, ret;
>
>         lockdep_assert_held(&tz->lock);
>
> -       if (!tz->ops->set_trips || !tz->ops->get_trip_hyst)
> +       if (!tz->ops->set_trips)
>                 return;
>
>         for (i = 0; i < tz->num_trips; i++) {
>                 int trip_low;
>
> -               tz->ops->get_trip_temp(tz, i, &trip_temp);
> -               tz->ops->get_trip_hyst(tz, i, &hysteresis);
> +               ret = __thermal_zone_get_trip(tz, i , &trip);
> +               if (ret)
> +                       return;
>
> -               trip_low = trip_temp - hysteresis;
> +               trip_low = trip.temperature - trip.hysteresis;
>
>                 if (trip_low < tz->temperature && trip_low > low)
>                         low = trip_low;
>
> -               if (trip_temp > tz->temperature && trip_temp < high)
> -                       high = trip_temp;
> +               if (trip.temperature > tz->temperature &&
> +                   trip.temperature < high)
> +                       high = trip.temperature;
>         }
>
>         /* No need to change trip points */
> diff --git a/drivers/thermal/thermal_netlink.c b/drivers/thermal/thermal_netlink.c
> index 050d243a5fa1..2a3190d55358 100644
> --- a/drivers/thermal/thermal_netlink.c
> +++ b/drivers/thermal/thermal_netlink.c
> @@ -452,7 +452,8 @@ static int thermal_genl_cmd_tz_get_trip(struct param *p)
>         struct sk_buff *msg = p->msg;
>         struct thermal_zone_device *tz;
>         struct nlattr *start_trip;
> -       int i, id;
> +       struct thermal_trip trip;
> +       int ret, i, id;
>
>         if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
>                 return -EINVAL;
> @@ -471,18 +472,14 @@ static int thermal_genl_cmd_tz_get_trip(struct param *p)
>
>         for (i = 0; i < tz->num_trips; i++) {
>
> -               enum thermal_trip_type type;
> -               int temp, hyst = 0;
> -
> -               tz->ops->get_trip_type(tz, i, &type);
> -               tz->ops->get_trip_temp(tz, i, &temp);
> -               if (tz->ops->get_trip_hyst)
> -                       tz->ops->get_trip_hyst(tz, i, &hyst);
> -
> +               ret = __thermal_zone_get_trip(tz, i, &trip);
> +               if (ret)
> +                       goto out_cancel_nest;
> +
>                 if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_ID, i) ||
> -                   nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_TYPE, type) ||
> -                   nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_TEMP, temp) ||
> -                   nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_HYST, hyst))
> +                   nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_TYPE, trip.type) ||
> +                   nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_TEMP, trip.temperature) ||
> +                   nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_HYST, trip.hysteresis))
>                         goto out_cancel_nest;
>         }
>
> diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysfs.c
> index ec495c7dff03..d093d7aa64c6 100644
> --- a/drivers/thermal/thermal_sysfs.c
> +++ b/drivers/thermal/thermal_sysfs.c
> @@ -83,20 +83,17 @@ trip_point_type_show(struct device *dev, struct device_attribute *attr,
>                      char *buf)
>  {
>         struct thermal_zone_device *tz = to_thermal_zone(dev);
> -       enum thermal_trip_type type;
> -       int trip, result;
> +       struct thermal_trip trip;
> +       int trip_id, result;
>
> -       if (!tz->ops->get_trip_type)
> -               return -EPERM;
> -
> -       if (sscanf(attr->attr.name, "trip_point_%d_type", &trip) != 1)
> +       if (sscanf(attr->attr.name, "trip_point_%d_type", &trip_id) != 1)
>                 return -EINVAL;
>
> -       result = tz->ops->get_trip_type(tz, trip, &type);
> +       result = thermal_zone_get_trip(tz, trip_id, &trip);
>         if (result)
>                 return result;
>
> -       switch (type) {
> +       switch (trip.type) {
>         case THERMAL_TRIP_CRITICAL:
>                 return sprintf(buf, "critical\n");
>         case THERMAL_TRIP_HOT:
> @@ -115,39 +112,33 @@ trip_point_temp_store(struct device *dev, struct device_attribute *attr,
>                       const char *buf, size_t count)
>  {
>         struct thermal_zone_device *tz = to_thermal_zone(dev);
> -       int trip, ret;
> -       int temperature, hyst = 0;
> -       enum thermal_trip_type type;
> +       struct thermal_trip trip;
> +       int trip_id, ret;
>
>         if (!tz->ops->set_trip_temp && !tz->trips)
>                 return -EPERM;
>
> -       if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip) != 1)
> +       if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip_id) != 1)
>                 return -EINVAL;
>
> -       if (kstrtoint(buf, 10, &temperature))
> +       if (kstrtoint(buf, 10, &trip.temperature))
>                 return -EINVAL;
>
>         if (tz->ops->set_trip_temp) {
> -               ret = tz->ops->set_trip_temp(tz, trip, temperature);
> +               ret = tz->ops->set_trip_temp(tz, trip_id, trip.temperature);
>                 if (ret)
>                         return ret;
>         }
>
>         if (tz->trips)
> -               tz->trips[trip].temperature = temperature;
> +               tz->trips[trip_id].temperature = trip.temperature;
>
> -       if (tz->ops->get_trip_hyst) {
> -               ret = tz->ops->get_trip_hyst(tz, trip, &hyst);
> -               if (ret)
> -                       return ret;
> -       }
> -
> -       ret = tz->ops->get_trip_type(tz, trip, &type);
> +       ret = thermal_zone_get_trip(tz, trip_id, &trip);
>         if (ret)
>                 return ret;
>
> -       thermal_notify_tz_trip_change(tz->id, trip, type, temperature, hyst);
> +       thermal_notify_tz_trip_change(tz->id, trip_id, trip.type,
> +                                     trip.temperature, trip.hysteresis);
>
>         thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
>
> @@ -159,21 +150,17 @@ trip_point_temp_show(struct device *dev, struct device_attribute *attr,
>                      char *buf)
>  {
>         struct thermal_zone_device *tz = to_thermal_zone(dev);
> -       int trip, ret;
> -       int temperature;
> +       struct thermal_trip trip;
> +       int trip_id, ret;
>
> -       if (!tz->ops->get_trip_temp)
> -               return -EPERM;
> -
> -       if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip) != 1)
> +       if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip_id) != 1)
>                 return -EINVAL;
>
> -       ret = tz->ops->get_trip_temp(tz, trip, &temperature);
> -
> +       ret = thermal_zone_get_trip(tz, trip_id, &trip);
>         if (ret)
>                 return ret;
>
> -       return sprintf(buf, "%d\n", temperature);
> +       return sprintf(buf, "%d\n", trip.temperature);
>  }
>
>  static ssize_t
> @@ -211,18 +198,17 @@ trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
>                      char *buf)
>  {
>         struct thermal_zone_device *tz = to_thermal_zone(dev);
> -       int trip, ret;
> -       int temperature;
> +       struct thermal_trip trip;
> +       int trip_id, ret;
>
> -       if (!tz->ops->get_trip_hyst)
> -               return -EPERM;
> -
> -       if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip) != 1)
> +       if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip_id) != 1)
>                 return -EINVAL;
>
> -       ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
> +       ret = thermal_zone_get_trip(tz, trip_id, &trip);
> +       if (ret)
> +               return ret;
>
> -       return ret ? ret : sprintf(buf, "%d\n", temperature);
> +       return ret ? ret : sprintf(buf, "%d\n", trip.hysteresis);
>  }
>
>  static ssize_t
> diff --git a/include/linux/thermal.h b/include/linux/thermal.h
> index a5a18351a898..feb8b61df746 100644
> --- a/include/linux/thermal.h
> +++ b/include/linux/thermal.h
> @@ -335,6 +335,13 @@ static inline void devm_thermal_of_zone_unregister(struct device *dev,
>  }
>  #endif
>
> +int thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
> +                         struct thermal_trip *trip);
> +
> +int thermal_zone_get_num_trips(struct thermal_zone_device *tz);
> +
> +int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp);
> +
>  #ifdef CONFIG_THERMAL
>  struct thermal_zone_device *thermal_zone_device_register(const char *, int, int,
>                 void *, struct thermal_zone_device_ops *,
> --
> 2.34.1
>
Rafael J. Wysocki Sept. 30, 2022, 5:27 p.m. UTC | #7
On Wed, Sep 28, 2022 at 11:01 PM Daniel Lezcano
<daniel.lezcano@linaro.org> wrote:
>
> The thermal zone ops defines a set_trip callback where we can invoke
> the backend driver to set an interrupt for the next trip point
> temperature being crossed the way up or down, or setting the low level
> with the hysteresis.
>
> The ops is only called from the thermal sysfs code where the userspace
> has the ability to modify a trip point characteristic.
>
> With the effort of encapsulating the thermal framework core code,
> let's create a thermal_zone_set_trip() which is the writable side of
> the thermal_zone_get_trip() and put there all the ops encapsulation.
>
> Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
> ---
>  drivers/thermal/thermal_core.c  | 47 +++++++++++++++++++++++++++++
>  drivers/thermal/thermal_sysfs.c | 52 +++++++++++----------------------
>  include/linux/thermal.h         |  3 ++
>  3 files changed, 67 insertions(+), 35 deletions(-)
>
> diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
> index 16ef91dc102f..2675671781cd 100644
> --- a/drivers/thermal/thermal_core.c
> +++ b/drivers/thermal/thermal_core.c
> @@ -1211,6 +1211,53 @@ int thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
>  }
>  EXPORT_SYMBOL_GPL(thermal_zone_get_trip);
>
> +int thermal_zone_set_trip(struct thermal_zone_device *tz, int trip_id,
> +                         const struct thermal_trip *trip)
> +{
> +       struct thermal_trip t;
> +       int ret = -EINVAL;
> +
> +       mutex_lock(&tz->lock);
> +
> +       if (!tz->ops->set_trip_temp && !tz->ops->set_trip_hyst && !tz->trips)
> +               goto out;
> +
> +       ret = __thermal_zone_get_trip(tz, trip_id, &t);
> +       if (ret)
> +               goto out;
> +
> +       if (t.type != trip->type) {
> +               ret = -EINVAL;
> +               goto out;
> +       }
> +
> +       if (t.temperature != trip->temperature && tz->ops->set_trip_temp) {
> +               ret = tz->ops->set_trip_temp(tz, trip_id, trip->temperature);
> +               if (ret)
> +                       goto out;
> +       }
> +
> +       if (t.hysteresis != trip->hysteresis && tz->ops->set_trip_hyst) {
> +               ret = tz->ops->set_trip_hyst(tz, trip_id, trip->hysteresis);
> +               if (ret)
> +                       goto out;
> +       }
> +
> +       if (tz->trips && ((t.temperature != trip->temperature) ||
> +                         (t.hysteresis != trip->hysteresis)))

if (tz->trips && (t.temperature != trip->temperature || t.hysteresis
!= trip->hysteresis))

pretty please, and I don't think that it is necessary to break this line.

> +               tz->trips[trip_id] = *trip;
> +out:
> +       mutex_unlock(&tz->lock);
> +
> +       if (!ret) {
> +               thermal_notify_tz_trip_change(tz->id, trip_id, trip->type,
> +                                             trip->temperature, trip->hysteresis);
> +               thermal_zone_device_update(tz, THERMAL_TRIP_CHANGED);
> +       }
> +
> +       return ret;
> +}
> +
>  /**
>   * thermal_zone_device_register_with_trips() - register a new thermal zone device
>   * @type:      the thermal zone device type
> diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysfs.c
> index 6c45194aaabb..8d7b25ab67c2 100644
> --- a/drivers/thermal/thermal_sysfs.c
> +++ b/drivers/thermal/thermal_sysfs.c
> @@ -115,32 +115,19 @@ trip_point_temp_store(struct device *dev, struct device_attribute *attr,
>         struct thermal_trip trip;
>         int trip_id, ret;
>
> -       if (!tz->ops->set_trip_temp && !tz->trips)
> -               return -EPERM;
> -
>         if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip_id) != 1)
>                 return -EINVAL;
>
> -       if (kstrtoint(buf, 10, &trip.temperature))
> -               return -EINVAL;
> -
> -       if (tz->ops->set_trip_temp) {
> -               ret = tz->ops->set_trip_temp(tz, trip_id, trip.temperature);
> -               if (ret)
> -                       return ret;
> -       }
> -
> -       if (tz->trips)
> -               tz->trips[trip_id].temperature = trip.temperature;
> -
>         ret = thermal_zone_get_trip(tz, trip_id, &trip);
>         if (ret)
>                 return ret;
>
> -       thermal_notify_tz_trip_change(tz->id, trip_id, trip.type,
> -                                     trip.temperature, trip.hysteresis);
> +       if (kstrtoint(buf, 10, &trip.temperature))
> +               return -EINVAL;
>
> -       thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
> +       ret = thermal_zone_set_trip(tz, trip_id, &trip);
> +       if (ret)
> +               return ret;
>
>         return count;
>  }
> @@ -168,29 +155,24 @@ trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
>                       const char *buf, size_t count)
>  {
>         struct thermal_zone_device *tz = to_thermal_zone(dev);
> -       int trip, ret;
> -       int temperature;
> -
> -       if (!tz->ops->set_trip_hyst)
> -               return -EPERM;
> +       struct thermal_trip trip;
> +       int trip_id, ret;
>
> -       if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip) != 1)
> +       if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip_id) != 1)
>                 return -EINVAL;
>
> -       if (kstrtoint(buf, 10, &temperature))
> -               return -EINVAL;
> +       ret = thermal_zone_get_trip(tz, trip_id, &trip);
> +       if (ret)
> +               return ret;
>
> -       /*
> -        * We are not doing any check on the 'temperature' value
> -        * here. The driver implementing 'set_trip_hyst' has to
> -        * take care of this.
> -        */
> -       ret = tz->ops->set_trip_hyst(tz, trip, temperature);
> +       if (kstrtoint(buf, 10, &trip.hysteresis))
> +               return -EINVAL;
>
> -       if (!ret)
> -               thermal_zone_set_trips(tz);
> +       ret = thermal_zone_set_trip(tz, trip_id, &trip);
> +       if (ret)
> +               return ret;
>
> -       return ret ? ret : count;
> +       return count;
>  }
>
>  static ssize_t
> diff --git a/include/linux/thermal.h b/include/linux/thermal.h
> index feb8b61df746..66373f872237 100644
> --- a/include/linux/thermal.h
> +++ b/include/linux/thermal.h
> @@ -338,6 +338,9 @@ static inline void devm_thermal_of_zone_unregister(struct device *dev,
>  int thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
>                           struct thermal_trip *trip);
>
> +int thermal_zone_set_trip(struct thermal_zone_device *tz, int trip_id,
> +                         const struct thermal_trip *trip);
> +
>  int thermal_zone_get_num_trips(struct thermal_zone_device *tz);
>
>  int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp);
> --
> 2.34.1
>
Rafael J. Wysocki Sept. 30, 2022, 5:39 p.m. UTC | #8
On Thu, Sep 29, 2022 at 9:35 PM Rafael J. Wysocki <rafael@kernel.org> wrote:
>
> On Thu, Sep 29, 2022 at 4:57 PM Daniel Lezcano
> <daniel.lezcano@linaro.org> wrote:
> >
> > On 29/09/2022 15:58, Rafael J. Wysocki wrote:
> > > On Thu, Sep 29, 2022 at 2:26 PM Daniel Lezcano
> > > <daniel.lezcano@linaro.org> wrote:
> > >>
> > >>
> > >> Hi Rafael,
> > >>
> > >> are you happy with the changes?
> > >
> > > I'll have a look and let you know.
> >
> > Great, thanks
>
> Well, because you have not added the history of changes to the
> patches, that will take more time than it would otherwise.

Done.  I've sent ACKs and still had a comment on one patch (minor but
still).  When that is addressed, the four initial core patches should
be good to go in.

I'm trusting you regarding the thermal/of changes (even though I think
that it would be good if someone involved in that code could review
them) and if you are confident about all of the driver changes, they
are fine with me too.
Lukasz Luba Oct. 3, 2022, 7:32 a.m. UTC | #9
Hi Rafael and Daniel

On 9/30/22 18:39, Rafael J. Wysocki wrote:
> On Thu, Sep 29, 2022 at 9:35 PM Rafael J. Wysocki <rafael@kernel.org> wrote:
>>
>> On Thu, Sep 29, 2022 at 4:57 PM Daniel Lezcano
>> <daniel.lezcano@linaro.org> wrote:
>>>
>>> On 29/09/2022 15:58, Rafael J. Wysocki wrote:
>>>> On Thu, Sep 29, 2022 at 2:26 PM Daniel Lezcano
>>>> <daniel.lezcano@linaro.org> wrote:
>>>>>
>>>>>
>>>>> Hi Rafael,
>>>>>
>>>>> are you happy with the changes?
>>>>
>>>> I'll have a look and let you know.
>>>
>>> Great, thanks
>>
>> Well, because you have not added the history of changes to the
>> patches, that will take more time than it would otherwise.
> 
> Done.  I've sent ACKs and still had a comment on one patch (minor but
> still).  When that is addressed, the four initial core patches should
> be good to go in.
> 
> I'm trusting you regarding the thermal/of changes (even though I think
> that it would be good if someone involved in that code could review
> them) and if you are confident about all of the driver changes, they
> are fine with me too.

Sorry for being late. I have been busy with some internal bug hunting.
I'll check the code today and test it on my dev boards.

Regards,
Lukasz