Skip to contentSkip to conversation

Thermal ACPI APIs for generic trip points

PATCH Superseded Daniel Lezcano 4 participants 24 messages Jan 13, 2023 – Jan 20, 2023

Subsystems (2)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover Thermal ACPI APIs for generic trip points

Recently sent as a RFC, the thermal ACPI for generic trip points is a set of functions to fill the generic trip points structure which will become the standard structure for the thermal framework and its users.

Different Intel drivers and the ACPI thermal driver are using the ACPI tables to get the thermal zone information. As those are getting the same information, providing this set of ACPI function with the generic trip points will consolidate the code.

Also, the Intel PCH and the Intel 34xx drivers are converted to use the generic trip points relying on the ACPI generic trip point parsing functions.

These changes have been tested on a Thinkpad Lenovo x280 with the PCH and INT34xx drivers. No regression have been observed, the trip points remain the same for what is described on this system.

Changelog:
- V5:
- Fixed GTSH unit conversion, deciK -> milli C

- V4:
- Fixed Kconfig option dependency, select THERMAL_ACPI if ACPI is set only for the PCH driver

- V3:
- Took into account Rafael's comments
- Used a silence option THERMAL_ACPI in order to stay consistent with THERMAL_OF. It is up to the API user to select the option.

- V2:
- Fix the thermal ACPI patch where the thermal_acpi.c was not included in the series
- Provide a couple of users of this API which could have been tested on a real system

Daniel Lezcano (3):
thermal/acpi: Add ACPI trip point routines thermal/drivers/intel: Use generic trip points for intel_pch thermal/drivers/intel: Use generic trip points int340x

 drivers/thermal/Kconfig                       |   4 +
 drivers/thermal/Makefile                      |   1 +
 drivers/thermal/intel/Kconfig                 |   1 +
 drivers/thermal/intel/int340x_thermal/Kconfig |   1 +
 .../int340x_thermal/int340x_thermal_zone.c    | 177 ++++-----------
 .../int340x_thermal/int340x_thermal_zone.h    |  10 +-
 drivers/thermal/intel/intel_pch_thermal.c     |  88 ++------
 drivers/thermal/thermal_acpi.c                | 210 ++++++++++++++++++
 include/linux/thermal.h                       |   8 +
 9 files changed, 286 insertions(+), 214 deletions(-)
 create mode 100644 drivers/thermal/thermal_acpi.c
Signature · 1 line
2.34.1
#1 lore raw

PATCH v5 1/3 thermal/acpi: Add ACPI trip point routines

The ACPI specification describes the trip points, the device tree bindings as well.

The OF code uses the generic trip point structures.

The ACPI has their own trip points structure and uses the get_trip_* ops to retrieve them.

We can do the same as the OF code and create a set of ACPI functions to retrieve a trip point description. Having a common code for ACPI will help to cleanup the remaining Intel drivers and get rid of the get_trip_* functions.

These changes add the ACPI thermal calls to retrieve the basic information we need to be reused in the thermal ACPI and Intel drivers.

The different ACPI functions have the generic trip point structure passed as parameter where it is filled.

This structure aims to be the one used by all the thermal drivers and the thermal framework.

After this series, a couple of Intel drivers and the ACPI thermal driver will still have their own trip points definition but a new series on top of this one will finish the conversion to the generic trip point handling.

This series depends on the generic trip point added to the thermal framework and available in the thermal/linux-next branch.

https://lkml.org/lkml/2022/10/3/456

It has been tested on a Intel i7-8650U - x280 with the INT3400, the PCH, ACPITZ, and x86_pkg_temp. No regression observed so far.

---
 drivers/thermal/Kconfig        |   4 +
 drivers/thermal/Makefile       |   1 +
 drivers/thermal/thermal_acpi.c | 209 +++++++++++++++++++++++++++++++++
 include/linux/thermal.h        |   8 ++
 4 files changed, 222 insertions(+)
 create mode 100644 drivers/thermal/thermal_acpi.c
drivers/thermal/Kconfig +4 -0
index e052dae614eb..eaeb2b2ee6e9 100644
@@ -76,6 +76,10 @@ config THERMAL_OF
 	  Say 'Y' here if you need to build thermal infrastructure
 	  based on device tree.
 
+config THERMAL_ACPI
+       depends on ACPI
+       bool
+
 config THERMAL_WRITABLE_TRIPS
 	bool "Enable writable trip points"
 	help
drivers/thermal/Makefile +1 -0
index 2506c6c8ca83..60f0dfa9aae2 100644
@@ -13,6 +13,7 @@ thermal_sys-$(CONFIG_THERMAL_NETLINK)		+= thermal_netlink.o
 # interface to/from other layers providing sensors
 thermal_sys-$(CONFIG_THERMAL_HWMON)		+= thermal_hwmon.o
 thermal_sys-$(CONFIG_THERMAL_OF)		+= thermal_of.o
+thermal_sys-$(CONFIG_THERMAL_ACPI)		+= thermal_acpi.o
 
 # governors
 thermal_sys-$(CONFIG_THERMAL_GOV_FAIR_SHARE)	+= gov_fair_share.o
drivers/thermal/thermal_acpi.c new file +209 -0

209 lines not on this page · raw message

include/linux/thermal.h +8 -0
index 30353e4b1424..ba2d5d4c23e2 100644
@@ -334,6 +334,14 @@ static inline void devm_thermal_of_zone_unregister(struct device *dev,
 }
 #endif
 
+#ifdef CONFIG_THERMAL_ACPI
+int thermal_acpi_trip_gtsh(struct acpi_device *adev);
+int thermal_acpi_trip_crit(struct acpi_device *adev, struct thermal_trip *trip);
+int thermal_acpi_trip_hot(struct acpi_device *adev, struct thermal_trip *trip);
+int thermal_acpi_trip_psv(struct acpi_device *adev, struct thermal_trip *trip);
+int thermal_acpi_trip_act(struct acpi_device *adev, struct thermal_trip *trip, int id);
+#endif
+
 int __thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
 			    struct thermal_trip *trip);
 int thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
Signature · 1 line
2.34.1
#7 lore raw

PATCH v5 2/3 thermal/drivers/intel: Use generic trip points for intel_pch

In reply to Daniel Lezcano

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 the ops content logic into generic trip points and register them with the thermal zone.

In order to consolidate the code, use the ACPI thermal framework API to fill the generic trip point from the ACPI tables.

It has been tested on a Intel i7-8650U - x280 with the INT3400, the PCH, ACPITZ, and x86_pkg_temp. No regression observed so far.

---
 drivers/thermal/intel/Kconfig             |  1 +
 drivers/thermal/intel/intel_pch_thermal.c | 88 +++++------------------
 2 files changed, 20 insertions(+), 69 deletions(-)
drivers/thermal/intel/Kconfig +1 -0
index f0c845679250..7d68c076c23d 100644
@@ -75,6 +75,7 @@ config INTEL_BXT_PMIC_THERMAL
 config INTEL_PCH_THERMAL
 	tristate "Intel PCH Thermal Reporting Driver"
 	depends on X86 && PCI
+	select THERMAL_ACPI if ACPI
 	help
 	  Enable this to support thermal reporting on certain intel PCHs.
 	  Thermal reporting device will provide temperature reading,
drivers/thermal/intel/intel_pch_thermal.c +19 -69

146 lines not on this page · raw message

Signature · 1 line
2.34.1
#8 lore raw

PATCH v5 3/3 thermal/drivers/intel: Use generic trip points int340x

In reply to Daniel Lezcano

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 the ops content logic into generic trip points and register them with the thermal zone.

In order to consolidate the code, use the ACPI thermal framework API to fill the generic trip point from the ACPI tables.

It has been tested on a Intel i7-8650U - x280 with the INT3400, the PCH, ACPITZ, and x86_pkg_temp. No regression observed so far.

---
 drivers/thermal/intel/int340x_thermal/Kconfig |   1 +
 .../int340x_thermal/int340x_thermal_zone.c    | 177 ++++--------------
 .../int340x_thermal/int340x_thermal_zone.h    |  10 +-
 3 files changed, 43 insertions(+), 145 deletions(-)
drivers/thermal/intel/int340x_thermal/Kconfig +1 -0
index 5d046de96a5d..b7072d37101d 100644
@@ -9,6 +9,7 @@ config INT340X_THERMAL
 	select THERMAL_GOV_USER_SPACE
 	select ACPI_THERMAL_REL
 	select ACPI_FAN
+	select THERMAL_ACPI
 	select INTEL_SOC_DTS_IOSF_CORE
 	select PROC_THERMAL_MMIO_RAPL if POWERCAP
 	help
drivers/thermal/intel/int340x_thermal/int340x_thermal_zone.c +40 -137

243 lines not on this page · raw message

drivers/thermal/intel/int340x_thermal/int340x_thermal_zone.h +2 -8
index e28ab1ba5e06..0c2c8de92014 100644
@@ -10,6 +10,7 @@
 #include <acpi/acpi_lpat.h>
 
 #define INT340X_THERMAL_MAX_ACT_TRIP_COUNT	10
+#define INT340X_THERMAL_MAX_TRIP_COUNT INT340X_THERMAL_MAX_ACT_TRIP_COUNT + 3
 
 struct active_trip {
 	int temp;
@@ -19,15 +20,8 @@ struct active_trip {
 
 struct int34x_thermal_zone {
 	struct acpi_device *adev;
-	struct active_trip act_trips[INT340X_THERMAL_MAX_ACT_TRIP_COUNT];
-	unsigned long *aux_trips;
+	struct thermal_trip *trips;
 	int aux_trip_nr;
-	int psv_temp;
-	int psv_trip_id;
-	int crt_temp;
-	int crt_trip_id;
-	int hot_temp;
-	int hot_trip_id;
 	struct thermal_zone_device *zone;
 	struct thermal_zone_device_ops *ops;
 	void *priv_data;
Signature · 1 line
2.34.1
#9 lore raw
In reply to Daniel Lezcano

Hi,

Quoted 9 lines · Daniel Lezcano

Recently sent as a RFC, the thermal ACPI for generic trip points is a set of functions to fill the generic trip points structure which will become the standard structure for the thermal framework and its users.

Different Intel drivers and the ACPI thermal driver are using the ACPI tables to get the thermal zone information. As those are getting the same information, providing this set of ACPI function with the generic trip points will consolidate the code.

Quoted 6 lines

Also, the Intel PCH and the Intel 34xx drivers are converted to use the generic trip points relying on the ACPI generic trip point parsing functions.

These changes have been tested on a Thinkpad Lenovo x280 with the PCH and INT34xx drivers. No regression have been observed, the trip points remain the same for what is described on this system.

Are we ok with this series ?

Sorry for insisting but I would like to go forward with the generic thermal trip work. There are more patches pending depending on this series.

Thanks
-- Daniel

Quoted 36 lines

Changelog:
- V5:
- Fixed GTSH unit conversion, deciK -> milli C

- V4:
- Fixed Kconfig option dependency, select THERMAL_ACPI if ACPI is set only for the PCH driver

- V3:
- Took into account Rafael's comments
- Used a silence option THERMAL_ACPI in order to stay consistent with THERMAL_OF. It is up to the API user to select the option.

- V2:
- Fix the thermal ACPI patch where the thermal_acpi.c was not included in the series
- Provide a couple of users of this API which could have been tested on a real system

Daniel Lezcano (3):
thermal/acpi: Add ACPI trip point routines thermal/drivers/intel: Use generic trip points for intel_pch thermal/drivers/intel: Use generic trip points int340x

  drivers/thermal/Kconfig                       |   4 +
  drivers/thermal/Makefile                      |   1 +
  drivers/thermal/intel/Kconfig                 |   1 +
  drivers/thermal/intel/int340x_thermal/Kconfig |   1 +
  .../int340x_thermal/int340x_thermal_zone.c    | 177 ++++-----------
  .../int340x_thermal/int340x_thermal_zone.h    |  10 +-
  drivers/thermal/intel/intel_pch_thermal.c     |  88 ++------
  drivers/thermal/thermal_acpi.c                | 210 ++++++++++++++++++
  include/linux/thermal.h                       |   8 +
  9 files changed, 286 insertions(+), 214 deletions(-)

create mode 100644 drivers/thermal/thermal_acpi.c

Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
reviewer
#10 lore raw
Quoted 28 lines · Daniel Lezcano

Hi,

Quoted 19 lines · Daniel Lezcano

Recently sent as a RFC, the thermal ACPI for generic trip points is a set of
functions to fill the generic trip points structure which will become the
standard structure for the thermal framework and its users.

Different Intel drivers and the ACPI thermal driver are using the ACPI tables to
get the thermal zone information. As those are getting the same information,
providing this set of ACPI function with the generic trip points will
consolidate the code.

Also, the Intel PCH and the Intel 34xx drivers are converted to use the generic
trip points relying on the ACPI generic trip point parsing functions.

Quoted 5 lines

These changes have been tested on a Thinkpad Lenovo x280 with the PCH and
INT34xx drivers. No regression have been observed, the trip points remain the
same for what is described on this system.

Quoted 5 lines

Are we ok with this series ?

Sorry for insisting but I would like to go forward with the generic thermal trip work. There are more patches pending depending on this series.

The whole series looks good to me.

But we'd better wait for the thermald test result from Srinvias.

thanks,
rui

Quoted 47 lines

Thanks
-- Daniel

Quoted 43 lines

Changelog:
- V5:
- Fixed GTSH unit conversion, deciK -> milli C

- V4:
- Fixed Kconfig option dependency, select THERMAL_ACPI if ACPI
is set
only for the PCH driver

- V3:
- Took into account Rafael's comments
- Used a silence option THERMAL_ACPI in order to stay
consistent
with THERMAL_OF. It is up to the API user to select the
option.

- V2:
- Fix the thermal ACPI patch where the thermal_acpi.c was not
included in
the series
- Provide a couple of users of this API which could have been
tested on a
real system

Daniel Lezcano (3):
thermal/acpi: Add ACPI trip point routines thermal/drivers/intel: Use generic trip points for intel_pch thermal/drivers/intel: Use generic trip points int340x

  drivers/thermal/Kconfig                       |   4 +
  drivers/thermal/Makefile                      |   1 +
  drivers/thermal/intel/Kconfig                 |   1 +
  drivers/thermal/intel/int340x_thermal/Kconfig |   1 +
  .../int340x_thermal/int340x_thermal_zone.c    | 177 ++++---------
--
  .../int340x_thermal/int340x_thermal_zone.h    |  10 +-
  drivers/thermal/intel/intel_pch_thermal.c     |  88 ++------
  drivers/thermal/thermal_acpi.c                | 210
++++++++++++++++++
  include/linux/thermal.h                       |   8 +
  9 files changed, 286 insertions(+), 214 deletions(-)

create mode 100644 drivers/thermal/thermal_acpi.c

#11 lore raw
Quoted 35 lines · Rui
Quoted 28 lines · Daniel Lezcano

Hi,

Quoted 19 lines · Daniel Lezcano

Recently sent as a RFC, the thermal ACPI for generic trip points is a set of
functions to fill the generic trip points structure which will become the
standard structure for the thermal framework and its users.

Different Intel drivers and the ACPI thermal driver are using the ACPI tables to
get the thermal zone information. As those are getting the same information,
providing this set of ACPI function with the generic trip points will
consolidate the code.

Also, the Intel PCH and the Intel 34xx drivers are converted to use the generic
trip points relying on the ACPI generic trip point parsing functions.

Quoted 5 lines

These changes have been tested on a Thinkpad Lenovo x280 with the PCH and
INT34xx drivers. No regression have been observed, the trip points remain the
same for what is described on this system.

Quoted 5 lines

Are we ok with this series ?

Sorry for insisting but I would like to go forward with the generic thermal trip work. There are more patches pending depending on this series.

Quoted 5 lines

The whole series looks good to me.

But we'd better wait for the thermald test result from Srinvias.

Sure, thanks for the review !

Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#12 lore raw
In reply to Zhang, Rui
Quoted 38 lines · Rui
Quoted 31 lines · Daniel Lezcano

Hi,

Quoted 21 lines · Daniel Lezcano

Recently sent as a RFC, the thermal ACPI for generic trip points is
a set of
functions to fill the generic trip points structure which will become the
standard structure for the thermal framework and its users.

Different Intel drivers and the ACPI thermal driver are using the ACPI tables to
get the thermal zone information. As those are getting the same information,
providing this set of ACPI function with the generic trip points will
consolidate the code.

Also, the Intel PCH and the Intel 34xx drivers are converted to use
the generic
trip points relying on the ACPI generic trip point parsing functions.

Quoted 6 lines

These changes have been tested on a Thinkpad Lenovo x280 with the PCH and
INT34xx drivers. No regression have been observed, the trip points
remain the
same for what is described on this system.

Quoted 5 lines

Are we ok with this series ?

Sorry for insisting but I would like to go forward with the generic thermal trip work. There are more patches pending depending on this series.

Quoted 5 lines

The whole series looks good to me.

But we'd better wait for the thermald test result from Srinvias.

A quick test show that things still work with thermald and these changes.

Thanks,
Srinivas

Quoted 52 lines

thanks,
rui

Quoted 49 lines

Thanks
   -- Daniel

Quoted 45 lines

Changelog:
  - V5:
    - Fixed GTSH unit conversion, deciK -> milli C

  - V4:
    - Fixed Kconfig option dependency, select THERMAL_ACPI if ACPI
is set
      only for the PCH driver

  - V3:
    - Took into account Rafael's comments
    - Used a silence option THERMAL_ACPI in order to stay consistent
      with THERMAL_OF. It is up to the API user to select the option.

  - V2:
    - Fix the thermal ACPI patch where the thermal_acpi.c was not included in
      the series
    - Provide a couple of users of this API which could have been tested on a
      real system

Daniel Lezcano (3):
   thermal/acpi: Add ACPI trip point routines thermal/drivers/intel: Use generic trip points for intel_pch thermal/drivers/intel: Use generic trip points int340x

  drivers/thermal/Kconfig                       |   4 + drivers/thermal/Makefile                      |   1 + drivers/thermal/intel/Kconfig                 |   1 + drivers/thermal/intel/int340x_thermal/Kconfig |   1 + .../int340x_thermal/int340x_thermal_zone.c    | 177 ++++------- --
--
  .../int340x_thermal/int340x_thermal_zone.h    |  10 +- drivers/thermal/intel/intel_pch_thermal.c     |  88 ++------ drivers/thermal/thermal_acpi.c                | 210
++++++++++++++++++
  include/linux/thermal.h                       |   8 +

  9 files changed, 286 insertions(+), 214 deletions(-)

  create mode 100644 drivers/thermal/thermal_acpi.c

#13 lore raw

Hi Srinivas,

On 18/01/2023 20:01, srinivas pandruvada wrote:

[ ... ]

Quoted 8 lines
Quoted 5 lines

The whole series looks good to me.

But we'd better wait for the thermald test result from Srinvias.

A quick test show that things still work with thermald and these changes.

Thanks for testing. Shall I consider as Tested-by or do you want to test more ?

Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#14 lore raw
In reply to srinivas pandruvada
Quoted 45 lines · srinivas pandruvada
Quoted 44 lines · Rui
Quoted 42 lines · Daniel Lezcano

Hi,

Quoted 24 lines · Daniel Lezcano

Recently sent as a RFC, the thermal ACPI for generic trip points
is
a set of
functions to fill the generic trip points structure which will become the
standard structure for the thermal framework and its users.

Different Intel drivers and the ACPI thermal driver are using the
ACPI tables to
get the thermal zone information. As those are getting the same information,
providing this set of ACPI function with the generic trip points
will
consolidate the code.

Also, the Intel PCH and the Intel 34xx drivers are converted to use
the generic
trip points relying on the ACPI generic trip point parsing functions.

Quoted 7 lines

These changes have been tested on a Thinkpad Lenovo x280 with the
PCH and
INT34xx drivers. No regression have been observed, the trip points
remain the
same for what is described on this system.

Are we ok with this series ?

Sorry for insisting but I would like to go forward with the generic
thermal trip work. There are more patches pending depending on this
series.

Quoted 8 lines
Quoted 5 lines

The whole series looks good to me.

But we'd better wait for the thermald test result from Srinvias.

A quick test show that things still work with thermald and these changes.

But I have a question. In some devices trip point temperature is not static. When hardware changes, we get notification. For example INT3403_PERF_TRIP_POINT_CHANGED for INT3403 drivers. Currently get_trip can get the latest changed value. But if we preregister, we need some mechanism to update them.

Thanks,
Srinivas

Quoted 59 lines

Thanks,
Srinivas

Quoted 55 lines

thanks,
rui

Quoted 52 lines

Thanks
   -- Daniel

Quoted 48 lines

Changelog:
  - V5:
    - Fixed GTSH unit conversion, deciK -> milli C

  - V4:
    - Fixed Kconfig option dependency, select THERMAL_ACPI if ACPI
is set
      only for the PCH driver

  - V3:
    - Took into account Rafael's comments
    - Used a silence option THERMAL_ACPI in order to stay consistent
      with THERMAL_OF. It is up to the API user to select the option.

  - V2:
    - Fix the thermal ACPI patch where the thermal_acpi.c was not
included in
      the series
    - Provide a couple of users of this API which could have been
tested on a
      real system

Daniel Lezcano (3):
   thermal/acpi: Add ACPI trip point routines thermal/drivers/intel: Use generic trip points for intel_pch thermal/drivers/intel: Use generic trip points int340x

  drivers/thermal/Kconfig                       |   4 + drivers/thermal/Makefile                      |   1 + drivers/thermal/intel/Kconfig                 |   1 + drivers/thermal/intel/int340x_thermal/Kconfig |   1 + .../int340x_thermal/int340x_thermal_zone.c    | 177 ++++----- --
--
--
  .../int340x_thermal/int340x_thermal_zone.h    |  10 +- drivers/thermal/intel/intel_pch_thermal.c     |  88 ++------ drivers/thermal/thermal_acpi.c                | 210
++++++++++++++++++
  include/linux/thermal.h                       |   8 +

  9 files changed, 286 insertions(+), 214 deletions(-)

  create mode 100644 drivers/thermal/thermal_acpi.c

#15 lore raw

On 18/01/2023 20:16, srinivas pandruvada wrote:

[ ... ]

Quoted 10 lines
Quoted 4 lines
Quoted 1 line

But we'd better wait for the thermald test result from Srinvias.

A quick test show that things still work with thermald and these changes.

But I have a question. In some devices trip point temperature is not static. When hardware changes, we get notification. For example INT3403_PERF_TRIP_POINT_CHANGED for INT3403 drivers. Currently get_trip can get the latest changed value. But if we preregister, we need some mechanism to update them.

When the notification INT3403_PERF_TRIP_POINT_CHANGED happens, we call int340x_thermal_read_trips() which in turn updates the trip points.

Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#16 lore raw
Quoted 17 lines · Daniel Lezcano

On 18/01/2023 20:16, srinivas pandruvada wrote:

[ ... ]

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

But we'd better wait for the thermald test result from Srinvias.

A quick test show that things still work with thermald and these changes.

Quoted 6 lines

But I have a question. In some devices trip point temperature is not
static. When hardware changes, we get notification. For example INT3403_PERF_TRIP_POINT_CHANGED for INT3403 drivers. Currently get_trip can get the latest changed value. But if we preregister, we need some mechanism to update them.

Quoted 4 lines

When the notification INT3403_PERF_TRIP_POINT_CHANGED happens, we call
int340x_thermal_read_trips() which in turn updates the trip points.

Not sure how we handle concurrency here when driver can freely update trips while thermal core is using trips.

Thanks,
Srinivas

Quoted 2 lines
#17 lore raw
Quoted 18 lines · srinivas pandruvada
Quoted 17 lines · Daniel Lezcano

On 18/01/2023 20:16, srinivas pandruvada wrote:

[ ... ]

Quoted 12 lines
Quoted 5 lines
Quoted 2 lines

But we'd better wait for the thermald test result from Srinvias.

A quick test show that things still work with thermald and these changes.

But I have a question. In some devices trip point temperature is not
static. When hardware changes, we get notification. For example INT3403_PERF_TRIP_POINT_CHANGED for INT3403 drivers. Currently get_trip can get the latest changed value. But if we preregister, we need some mechanism to update them.

Quoted 7 lines
Quoted 4 lines

When the notification INT3403_PERF_TRIP_POINT_CHANGED happens, we call
int340x_thermal_read_trips() which in turn updates the trip points.

Not sure how we handle concurrency here when driver can freely update trips while thermal core is using trips.

Don't we have the same race without this patch ? The thermal core can call get_trip_temp() while there is an update, no ?

Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#18 lore raw
Quoted 27 lines · Daniel Lezcano
Quoted 26 lines · srinivas pandruvada
Quoted 24 lines · Daniel Lezcano

On 18/01/2023 20:16, srinivas pandruvada wrote:

[ ... ]

Quoted 7 lines
Quoted 6 lines
Quoted 2 lines

But we'd better wait for the thermald test result from Srinvias.

A quick test show that things still work with thermald and these
changes.

Quoted 7 lines

But I have a question. In some devices trip point temperature is
not
static. When hardware changes, we get notification. For example INT3403_PERF_TRIP_POINT_CHANGED for INT3403 drivers. Currently get_trip can get the latest changed value. But if we preregister, we need some mechanism to update them.

When the notification INT3403_PERF_TRIP_POINT_CHANGED happens, we call
int340x_thermal_read_trips() which in turn updates the trip points.

Quoted 6 lines
Quoted 3 lines

Not sure how we handle concurrency here when driver can freely update
trips while thermal core is using trips.

Don't we have the same race without this patch ? The thermal core can call get_trip_temp() while there is an update, no ?

Yes it is. But I can add a mutex locally here to solve. But not any longer.

I think you need some thermal_zone_read_lock/unlock() in core, which can use rcu. Even mutex is fine as there will be no contention as updates to trips will be rare.

Thanks,
Srinivas

Quoted 1 line
#19 lore raw
Quoted 32 lines · srinivas pandruvada
Quoted 31 lines · Daniel Lezcano
Quoted 29 lines · srinivas pandruvada
Quoted 19 lines · Daniel Lezcano

On 18/01/2023 20:16, srinivas pandruvada wrote:

[ ... ]

Quoted 7 lines
Quoted 6 lines
Quoted 2 lines

But we'd better wait for the thermald test result from Srinvias.

A quick test show that things still work with thermald and these
changes.

Quoted 7 lines

But I have a question. In some devices trip point temperature is
not
static. When hardware changes, we get notification. For example INT3403_PERF_TRIP_POINT_CHANGED for INT3403 drivers. Currently get_trip can get the latest changed value. But if we preregister, we need some mechanism to update them.

Quoted 5 lines

When the notification INT3403_PERF_TRIP_POINT_CHANGED happens, we call
int340x_thermal_read_trips() which in turn updates the trip points.

Not sure how we handle concurrency here when driver can freely update
trips while thermal core is using trips.

Quoted 8 lines
Quoted 2 lines

Don't we have the same race without this patch ? The thermal core can call get_trip_temp() while there is an update, no ?

Yes it is. But I can add a mutex locally here to solve. But not any longer.

I think you need some thermal_zone_read_lock/unlock() in core, which can use rcu. Even mutex is fine as there will be no contention as updates to trips will be rare.

I was planning to provide a thermal_trips_update(tz, trips) and from there handle the locking.

As the race was already existing, can we postpone this change after the generic trip points changes?

There is still a lot of work to do to consolidate the code. One of them is to provide a generic function to browse the trip points and ensure the code is using it instead of directly inspect the thermal zone internals structure.

I'm almost there but I need the remaining Intel drivers changes to be merged (as well as ACPI which is finished but depending on this series).

Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#20 lore raw
Quoted 49 lines · Daniel Lezcano
Quoted 40 lines · srinivas pandruvada
Quoted 32 lines · Daniel Lezcano
Quoted 31 lines · srinivas pandruvada
Quoted 29 lines · Daniel Lezcano

On 18/01/2023 20:16, srinivas pandruvada wrote:

[ ... ]

Quoted 8 lines
Quoted 7 lines
Quoted 2 lines

But we'd better wait for the thermald test result from Srinvias.

A quick test show that things still work with thermald and
these
changes.

Quoted 10 lines

But I have a question. In some devices trip point temperature
is
not
static. When hardware changes, we get notification. For example
INT3403_PERF_TRIP_POINT_CHANGED for INT3403 drivers. Currently get_trip can get the latest changed value. But if we
preregister, we need some mechanism to update them.

When the notification INT3403_PERF_TRIP_POINT_CHANGED happens, we
call
int340x_thermal_read_trips() which in turn updates the trip points.

Quoted 7 lines
Quoted 3 lines

Not sure how we handle concurrency here when driver can freely update
trips while thermal core is using trips.

Don't we have the same race without this patch ? The thermal core can
call get_trip_temp() while there is an update, no ?

Quoted 7 lines

Yes it is. But I can add a mutex locally here to solve. But not any longer.

I think you need some thermal_zone_read_lock/unlock() in core, which
can use rcu. Even mutex is fine as there will be no contention as updates to trips will be rare.

Quoted 6 lines

I was planning to provide a thermal_trips_update(tz, trips) and from there handle the locking.

As the race was already existing, can we postpone this change after the
generic trip points changes?

I think so.

Quoted 11 lines

There is still a lot of work to do to consolidate the code. One of them
is to provide a generic function to browse the trip points and ensure the code is using it instead of directly inspect the thermal zone internals structure.

I'm almost there but I need the remaining Intel drivers changes to be merged (as well as ACPI which is finished but depending on this series).

Sounds good.

You can add my tested by for this.

Thanks,
Srinivas

#21 lore raw
Quoted 51 lines · srinivas pandruvada
Quoted 49 lines · Daniel Lezcano
Quoted 47 lines · srinivas pandruvada
Quoted 32 lines · Daniel Lezcano
Quoted 31 lines · srinivas pandruvada
Quoted 29 lines · Daniel Lezcano

On 18/01/2023 20:16, srinivas pandruvada wrote:

[ ... ]

Quoted 8 lines
Quoted 7 lines
Quoted 2 lines

But we'd better wait for the thermald test result from Srinvias.

A quick test show that things still work with thermald and
these
changes.

Quoted 10 lines

But I have a question. In some devices trip point temperature
is
not
static. When hardware changes, we get notification. For example
INT3403_PERF_TRIP_POINT_CHANGED for INT3403 drivers. Currently get_trip can get the latest changed value. But if we
preregister, we need some mechanism to update them.

When the notification INT3403_PERF_TRIP_POINT_CHANGED happens, we
call
int340x_thermal_read_trips() which in turn updates the trip points.

Quoted 7 lines
Quoted 3 lines

Not sure how we handle concurrency here when driver can freely update
trips while thermal core is using trips.

Don't we have the same race without this patch ? The thermal core can
call get_trip_temp() while there is an update, no ?

Yes it is. But I can add a mutex locally here to solve. But not any longer.

I think you need some thermal_zone_read_lock/unlock() in core, which
can use rcu. Even mutex is fine as there will be no contention as updates to trips will be rare.

Quoted 7 lines
Quoted 6 lines

I was planning to provide a thermal_trips_update(tz, trips) and from there handle the locking.

As the race was already existing, can we postpone this change after the
generic trip points changes?

I think so.

Well, what if this bug is reported by a user and a fix needs to be backported to "stable"?

Are we going to backport the whole framework redesign along with it?

Or is this extremely unlikely?

#2 lore raw

Re: [PATCH v5 1/3] thermal/acpi: Add ACPI trip point routines

In reply to Daniel Lezcano
Quoted 29 lines · Daniel Lezcano

The ACPI specification describes the trip points, the device tree bindings as well.

The OF code uses the generic trip point structures.

The ACPI has their own trip points structure and uses the get_trip_* ops to retrieve them.

We can do the same as the OF code and create a set of ACPI functions to retrieve a trip point description. Having a common code for ACPI will help to cleanup the remaining Intel drivers and get rid of the get_trip_* functions.

These changes add the ACPI thermal calls to retrieve the basic information we need to be reused in the thermal ACPI and Intel drivers.

The different ACPI functions have the generic trip point structure passed as parameter where it is filled.

This structure aims to be the one used by all the thermal drivers and the thermal framework.

After this series, a couple of Intel drivers and the ACPI thermal driver will still have their own trip points definition but a new series on top of this one will finish the conversion to the generic trip point handling.

Quoted 7 lines

This series depends on the generic trip point added to the thermal framework and available in the thermal/linux-next branch.

https://lkml.org/lkml/2022/10/3/456

It has been tested on a Intel i7-8650U - x280 with the INT3400, the PCH, ACPITZ, and x86_pkg_temp. No regression observed so far.

Some names of the functions defined below can be less cryptic IMO. Please see the following comments.

Quoted 54 lines
---
 drivers/thermal/Kconfig        |   4 +
 drivers/thermal/Makefile       |   1 +
 drivers/thermal/thermal_acpi.c | 209 +++++++++++++++++++++++++++++++++
 include/linux/thermal.h        |   8 ++
 4 files changed, 222 insertions(+)
 create mode 100644 drivers/thermal/thermal_acpi.c
drivers/thermal/Kconfig +4 -0
index e052dae614eb..eaeb2b2ee6e9 100644
@@ -76,6 +76,10 @@ config THERMAL_OF
          Say 'Y' here if you need to build thermal infrastructure
          based on device tree.
 
+config THERMAL_ACPI
+       depends on ACPI
+       bool
+
 config THERMAL_WRITABLE_TRIPS
        bool "Enable writable trip points"
        help
drivers/thermal/Makefile +1 -0
index 2506c6c8ca83..60f0dfa9aae2 100644
@@ -13,6 +13,7 @@ thermal_sys-$(CONFIG_THERMAL_NETLINK)         += thermal_netlink.o
 # interface to/from other layers providing sensors
 thermal_sys-$(CONFIG_THERMAL_HWMON)            += thermal_hwmon.o
 thermal_sys-$(CONFIG_THERMAL_OF)               += thermal_of.o
+thermal_sys-$(CONFIG_THERMAL_ACPI)             += thermal_acpi.o
 
 # governors
 thermal_sys-$(CONFIG_THERMAL_GOV_FAIR_SHARE)   += gov_fair_share.o
drivers/thermal/thermal_acpi.c new file +12 -0
new file mode 100644
index 000000000000..ef6f10713650
@@ -0,0 +1,209 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2022 Linaro Limited
+ *
+ * Author: Daniel Lezcano <[email protected]>
+ *
+ * ACPI thermal configuration
+ */
+#include <linux/acpi.h>
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/units.h>

drivers/thermal/thermal_acpi.c line 22

Quoted 10 lines
drivers/thermal/thermal_acpi.c new file +10 -0
+#include <uapi/linux/thermal.h>
+
+#include "thermal_core.h"
+
+/*
+ * An hysteresis value below zero is invalid and we can consider a
+ * value greater than 20°K/°C is invalid too.
+ */
+#define HYSTERESIS_MIN_DECIK   0
+#define HYSTERESIS_MAX_DECIK   200

If the full word "hysteresis" is used here, it should also be used in the hysteresis-related names below.

I would use the "hyst" abbreviation here and elsewhere where applicable.

drivers/thermal/thermal_acpi.c line 32

Quoted 10 lines
drivers/thermal/thermal_acpi.c new file +10 -0
+
+/*
+ * Minimum temperature for full military grade is 218°K (-55°C) and
+ * max temperature is 448°K (175°C). We can consider those values as
+ * the boundaries for the [trips] temperature returned by the
+ * firmware. Any values out of these boundaries can be considered
+ * bogus and we can assume the firmware has no data to provide.
+ */
+#define TEMPERATURE_MIN_DECIK  2180
+#define TEMPERATURE_MAX_DECIK  4480

Like the above, but regarding "temperature" and "temp" instead of "hysteresis" and "hyst", respectively.

drivers/thermal/thermal_acpi.c line 35

Quoted 3 lines
drivers/thermal/thermal_acpi.c new file +3 -0
+
+static int thermal_acpi_get_temperature_object(struct acpi_device *adev,
+                                              char *object, int *temperature)

So this would become thermal_acpi_get_temp_object(). or even thermal_acpi_get_temp() because it really returns the temperature value.

I also don't particularly like returning values via pointers, which is entirely avoidable here, because the temperature value obtained from the ACPI control methods must be a positive number.

So I would make it

static int thermal_acpi_get_temp(struct acpi_device *adev, char *object_name) {

which would be consistent with the definition of the hysteresis function below (which returns the value directly).

drivers/thermal/thermal_acpi.c line 42

Quoted 7 lines
drivers/thermal/thermal_acpi.c new file +7 -0
+{
+       unsigned long long temp;
+       acpi_status status;
+
+       status = acpi_evaluate_integer(adev->handle, object, NULL, &temp);
+       if (ACPI_FAILURE(status)) {
+               acpi_handle_debug(adev->handle, "No temperature object '%s'\n", object);

This message may not be true, because status need not be AE_NOT_FOUND.

drivers/thermal/thermal_acpi.c line 48

Quoted 6 lines
drivers/thermal/thermal_acpi.c new file +6 -0
+               return -ENODEV;
+       }
+
+       if (temp < TEMPERATURE_MIN_DECIK || temp >= TEMPERATURE_MAX_DECIK) {
+               acpi_handle_info(adev->handle, "Invalid temperature '%llu deci°K' for object '%s'\n",
+                                temp, object);

I think that the message can be debug-level as well and I would just say "Invalid value %llu returned by %s\n" in it.

drivers/thermal/thermal_acpi.c line 49

Quoted 1 line
drivers/thermal/thermal_acpi.c new file +1 -0
+               return -ENODATA;

And I'm not sure if the difference between -ENODEV and -ENODATA matters here. Maybe return -ENODATA in all error cases?

drivers/thermal/thermal_acpi.c line 58

Quoted 9 lines
drivers/thermal/thermal_acpi.c new file +9 -0
+       }
+
+       *temperature = deci_kelvin_to_millicelsius(temp);
+
+       return 0;
+}
+
+/**
+ * thermal_acpi_trip_gtsh() - Get the global hysteresis value

thermal_acpi_global_hyst() please.

drivers/thermal/thermal_acpi.c line 71

Quoted 13 lines
drivers/thermal/thermal_acpi.c new file +13 -0
+ * @adev: the acpi device to get the description from
+ *
+ * Get the global hysteresis value for the trip points. If any call
+ * fail, we shall return a zero hysteresis value.
+ *
+ * Return: An integer between %HYSTERESIS_MIN_DECIK and %HYSTERESIS_MAX_DECIK
+ */
+int thermal_acpi_trip_gtsh(struct acpi_device *adev)
+{
+       unsigned long long hyst;
+       acpi_status status;
+
+       status = acpi_evaluate_integer(adev->handle, "GTSH", NULL, &hyst);

GTSH is not a standard ACPI thing.

AFAICS, it's int3403 specific, so using it in a generic ACPI library is questionable.

Quoted 5 lines
drivers/thermal/thermal_acpi.c new file +5 -0
+       if (ACPI_FAILURE(status))
+               return 0;
+
+       if (hyst < HYSTERESIS_MIN_DECIK || hyst >= HYSTERESIS_MAX_DECIK) {
+               acpi_handle_info(adev->handle, "Invalid hysteresis '%llu deci°K' for object 'GTSH'\n",

drivers/thermal/thermal_acpi.c line 86

Quoted 10 lines
drivers/thermal/thermal_acpi.c new file +10 -0
+                                hyst);
+               return 0;
+       }
+
+       return deci_kelvin_to_millicelsius(hyst);
+}
+EXPORT_SYMBOL_GPL(thermal_acpi_trip_gtsh);
+
+/**
+ * thermal_acpi_trip_act() - Get the specified active trip point

thermal_acpi_active_trip_temp, please.

drivers/thermal/thermal_acpi.c line 87

Quoted 1 line
drivers/thermal/thermal_acpi.c new file +1 -0
+ * @adev: the acpi device to get the description from

Please spell ACPI in capitals in all comments.

And I would say

@adev: Thermal zone ACPI device object

drivers/thermal/thermal_acpi.c line 88

Quoted 1 line
drivers/thermal/thermal_acpi.c new file +1 -0
+ * @trip: a &struct thermal_trip to be filled if the function succeed

@trip: Trip point structure to be populated on success

drivers/thermal/thermal_acpi.c line 89

Quoted 1 line
drivers/thermal/thermal_acpi.c new file +1 -0
+ * @id: an integer speciyfing the active trip point id

@id: Active cooling level (0 - 9)

drivers/thermal/thermal_acpi.c

Quoted 3 lines
drivers/thermal/thermal_acpi.c new file +3 -0
+ *
+ * The function calls the ACPI framework to get the "_ACTx" objects
+ * which describe the active trip points.

No, it doesn't do that. What it really does can be described as

"Evaluate the _ACx object for the thermal zone represented by @adev to obtain the temperature of the active cooling trip point corresponding to the active cooling level given by @id and initialize @trip as an active trip point using that temperature value"

drivers/thermal/thermal_acpi.c line 104

Quoted 13 lines

The @id builds the "_ACTx"
+ * string with the numbered active trip point name. Then it fills the
+ * @trip structure with the information retrieved from those objects.
+ *
+ * Return:
+ * * 0 - Success
+ * * -ENODEV - Failed to retrieve the ACPI object
+ * * -ENODATA - The ACPI object value appears to be inconsistent
+ */

drivers/thermal/thermal_acpi.c new file +4 -0
+int thermal_acpi_trip_act(struct acpi_device *adev,
+                         struct thermal_trip *trip, int id)
+{
+       char name[5];

char name[] = "_AC0";

Quoted 1 line

+ int ret;

int temp;

if (id < 0 || id > 9)

        return -EINVAL;

name[3] += id;

drivers/thermal/thermal_acpi.c line 111

Quoted 6 lines
drivers/thermal/thermal_acpi.c new file +6 -0
+
+       sprintf(name, "_AC%d", id);
+
+       ret = thermal_acpi_get_temperature_object(adev, name, &trip->temperature);
+       if (ret)
+               return ret;

temp = thermal_acpi_get_temp(adev, name);
if (temp < 0)

        return temp;

trip->temperature = temp;

And analogously below.

drivers/thermal/thermal_acpi.c line 121

Quoted 10 lines
drivers/thermal/thermal_acpi.c new file +10 -0
+
+       trip->hysteresis = 0;
+       trip->type = THERMAL_TRIP_ACTIVE;
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(thermal_acpi_trip_act);
+
+/**
+ * thermal_acpi_trip_psv() - Get the passive trip point

thermal_acpi_passive_trip_temp, please.

Quoted 2 lines

+ * @adev: the acpi device to get the description from
+ * @trip: a &struct thermal_trip to be filled if the function succeed

The same comments as above apply.

Quoted 4 lines

+ *
+ * The function calls the ACPI framework to get the "_PSV" object
+ * which describe the passive trip point. Then it fills the @trip
+ * structure with the information retrieved from those objects.

"Evaluate the _PSV object for the thermal zone represented by @adev to obtain the temperature of the passive cooling trip point and initialize @trip as a passive trip point using that temperature value."

Quoted 13 lines

+ *
+ * Return:
+ * * 0 - Success
+ * * -ENODEV - Failed to retrieve the ACPI object
+ * * -ENODATA - The ACPI object value appears to be inconsistent
+ */

drivers/thermal/thermal_acpi.c new file +7 -0
+int thermal_acpi_trip_psv(struct acpi_device *adev, struct thermal_trip *trip)
+{
+       int ret;
+
+       ret = thermal_acpi_get_temperature_object(adev, "_PSV", &trip->temperature);
+       if (ret)
+               return ret;

drivers/thermal/thermal_acpi.c line 150

Quoted 10 lines
drivers/thermal/thermal_acpi.c new file +10 -0
+
+       trip->hysteresis = 0;
+       trip->type = THERMAL_TRIP_PASSIVE;
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(thermal_acpi_trip_psv);
+
+/**
+ * thermal_acpi_trip_hot() - Get the near critical trip point

thermal_acpi_hot_trip_temp, please.

Quoted 2 lines

+ * @adev: the acpi device to get the description from
+ * @trip: a &struct thermal_trip to be filled if the function succeed

The same comments as above apply.

Quoted 4 lines

+ *
+ * The function calls the ACPI framework to get the "_HOT" object
+ * which describe the hot trip point. Then it fills the @trip
+ * structure with the information retrieved from those objects.

"Evaluate the _HOT object for the thermal zone represented by @adev to obtain the temperature of the trip point at which the system is expected to be put into the S4 sleep state and initialize @trip as a hot trip point using that temperature value."

Quoted 13 lines

+ *
+ * Return:
+ * * 0 - Success
+ * * -ENODEV - Failed to retrieve the ACPI object
+ * * -ENODATA - The ACPI object appears to be inconsistent
+ */

drivers/thermal/thermal_acpi.c new file +7 -0
+int thermal_acpi_trip_hot(struct acpi_device *adev, struct thermal_trip *trip)
+{
+       int ret;
+
+       ret = thermal_acpi_get_temperature_object(adev, "_HOT", &trip->temperature);
+       if (ret)
+               return ret;

drivers/thermal/thermal_acpi.c line 179

Quoted 10 lines
drivers/thermal/thermal_acpi.c new file +10 -0
+
+       trip->hysteresis = 0;
+       trip->type = THERMAL_TRIP_HOT;
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(thermal_acpi_trip_hot);
+
+/**
+ * thermal_acpi_trip_crit() - Get the critical trip point

thermal_acpi_crit_trip_temp, please.

Quoted 2 lines

+ * @adev: the acpi device to get the description from
+ * @trip: a &struct thermal_trip to be filled if the function succeed

The same comments as above apply.

Quoted 4 lines

+ *
+ * The function calls the ACPI framework to get the "_CRT" object
+ * which describe the critical trip point. Then it fills the @trip
+ * structure with the information retrieved from this object.

"Evaluate the _CRT object for the thermal zone represented by @adev to obtain the temperature of the critical cooling trip point and initialize @trip as a critical trip point using that temperature value."

Quoted 44 lines

+ *
+ * Return:
+ * * 0 - Success
+ * * -ENODEV - Failed to retrieve the ACPI object
+ * * -ENODATA - The ACPI object value appears to be inconsistent
+ */

drivers/thermal/thermal_acpi.c new file +18 -0
+int thermal_acpi_trip_crit(struct acpi_device *adev, struct thermal_trip *trip)
+{
+       int ret;
+
+       ret = thermal_acpi_get_temperature_object(adev, "_CRT", &trip->temperature);
+       if (ret)
+               return ret;
+
+       /*
+        * The hysteresis value has no sense here because critical
+        * trip point has no u-turn
+        */
+       trip->hysteresis = 0;
+       trip->type = THERMAL_TRIP_CRITICAL;
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(thermal_acpi_trip_crit);
include/linux/thermal.h +8 -1
index 30353e4b1424..ba2d5d4c23e2 100644
@@ -334,6 +334,14 @@ static inline void devm_thermal_of_zone_unregister(struct device *dev,
 }
 #endif
 
+#ifdef CONFIG_THERMAL_ACPI
+int thermal_acpi_trip_gtsh(struct acpi_device *adev);
+int thermal_acpi_trip_crit(struct acpi_device *adev, struct thermal_trip *trip);
+int thermal_acpi_trip_hot(struct acpi_device *adev, struct thermal_trip *trip);
+int thermal_acpi_trip_psv(struct acpi_device *adev, struct thermal_trip *trip);
+int thermal_acpi_trip_act(struct acpi_device *adev, struct thermal_trip *trip, int id);
+#endif
+
 int __thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
                            struct thermal_trip *trip);
 int thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
--
#22 lore raw
In reply to Rafael J. Wysocki
Quoted 68 lines · Rafael J. Wysocki
Quoted 66 lines · srinivas pandruvada
Quoted 64 lines · Daniel Lezcano
Quoted 46 lines · srinivas pandruvada
Quoted 41 lines · Daniel Lezcano
Quoted 35 lines · srinivas pandruvada
Quoted 26 lines · Daniel Lezcano

On 18/01/2023 20:16, srinivas pandruvada wrote:

[ ... ]

Quoted 10 lines
Quoted 9 lines
Quoted 3 lines

But we'd better wait for the thermald test result
from
Srinvias.

A quick test show that things still work with
thermald
and
these
changes.

Quoted 11 lines

But I have a question. In some devices trip point temperature
is
not
static. When hardware changes, we get notification. For example
INT3403_PERF_TRIP_POINT_CHANGED for INT3403 drivers. Currently get_trip can get the latest changed value. But if
we
preregister, we need some mechanism to update them.

Quoted 7 lines

When the notification INT3403_PERF_TRIP_POINT_CHANGED happens, we
call
int340x_thermal_read_trips() which in turn updates the trip
points.

Quoted 4 lines

Not sure how we handle concurrency here when driver can freely
update
trips while thermal core is using trips.

Quoted 4 lines

Don't we have the same race without this patch ? The thermal core
can
call get_trip_temp() while there is an update, no ?

Quoted 8 lines

Yes it is. But I can add a mutex locally here to solve. But not any longer.

I think you need some thermal_zone_read_lock/unlock() in core, which
can use rcu. Even mutex is fine as there will be no contention as
updates to trips will be rare.

I was planning to provide a thermal_trips_update(tz, trips) and from
there handle the locking.

As the race was already existing, can we postpone this change after
the
generic trip points changes?

Quoted 8 lines
Quoted 1 line

I think so.

Well, what if this bug is reported by a user and a fix needs to be backported to "stable"?

Are we going to backport the whole framework redesign along with it?

Or is this extremely unlikely?

These trips are read at the start of DTT/Thermald and will be read once after notification is received. So extremely unlikely.
But we can add the patch before this series to address this issue, which can be marked stable. I can submit this.

Thanks,
Srinivas

From 2157c15ab856585c421119ac4587747311cb2a99 Mon Sep 17 00:00:00 2001
From: Srinivas Pandruvada <[email protected]>
Date: Thu, 19 Jan 2023 08:37:15 -0800
Subject: [PATCH] thermal: int340x: Protect trip temperature from dynamic
update

Trip temperatures are read using ACPI methods and stored in the memory during zone initializtion and when the firmware sends a notification for change. This trip temperature is returned when the thermal core calls via callback get_trip_temp().

But it is possible that while updating the memory copy of the trips when the firmware sends a notification for change, thermal core is reading the trip temperature via the callback get_trip_temp(). This may return invalid trip temperature.

To address this add a mutex to protect the invalid temperature reads in the callback get_trip_temp() and int340x_thermal_read_trips().

---
 .../intel/int340x_thermal/int340x_thermal_zone.c | 16 +++++++++++++++-
 .../intel/int340x_thermal/int340x_thermal_zone.h |  1 +
 2 files changed, 16 insertions(+), 1 deletion(-)
drivers/thermal/intel/int340x_thermal/int340x_thermal_zone.c +15 -1
index 62c0aa5d0783..fd9080640e03 100644
@@ -49,6 +49,8 @@ static int int340x_thermal_get_trip_temp(struct thermal_zone_device *zone,
 	if (d->override_ops && d->override_ops->get_trip_temp)
 		return d->override_ops->get_trip_temp(zone, trip, temp);
 
+	mutex_lock(&d->trip_mutex);
+
 	if (trip < d->aux_trip_nr)
 		*temp = d->aux_trips[trip];
 	else if (trip == d->crt_trip_id)
@@ -65,10 +67,14 @@ static int int340x_thermal_get_trip_temp(struct thermal_zone_device *zone,
 				break;
 			}
 		}
-		if (i == INT340X_THERMAL_MAX_ACT_TRIP_COUNT)
+		if (i == INT340X_THERMAL_MAX_ACT_TRIP_COUNT) {
+			mutex_unlock(&d->trip_mutex);
 			return -EINVAL;
+		}
 	}
 
+	mutex_unlock(&d->trip_mutex);
+
 	return 0;
 }
 
@@ -180,6 +186,8 @@ int int340x_thermal_read_trips(struct int34x_thermal_zone *int34x_zone)
 	int trip_cnt = int34x_zone->aux_trip_nr;
 	int i;
 
+	mutex_lock(&int34x_zone->trip_mutex);
+
 	int34x_zone->crt_trip_id = -1;
 	if (!int340x_thermal_get_trip_config(int34x_zone->adev->handle, "_CRT",
 					     &int34x_zone->crt_temp))
@@ -207,6 +215,8 @@ int int340x_thermal_read_trips(struct int34x_thermal_zone *int34x_zone)
 		int34x_zone->act_trips[i].valid = true;
 	}
 
+	mutex_unlock(&int34x_zone->trip_mutex);
+
 	return trip_cnt;
 }
 EXPORT_SYMBOL_GPL(int340x_thermal_read_trips);
@@ -230,6 +240,8 @@ struct int34x_thermal_zone *int340x_thermal_zone_add(struct acpi_device *adev,
 	if (!int34x_thermal_zone)
 		return ERR_PTR(-ENOMEM);
 
+	mutex_init(&int34x_thermal_zone->trip_mutex);
+
 	int34x_thermal_zone->adev = adev;
 	int34x_thermal_zone->override_ops = override_ops;
 
@@ -281,6 +293,7 @@ struct int34x_thermal_zone *int340x_thermal_zone_add(struct acpi_device *adev,
 	acpi_lpat_free_conversion_table(int34x_thermal_zone->lpat_table);
 	kfree(int34x_thermal_zone->aux_trips);
 err_trip_alloc:
+	mutex_destroy(&int34x_thermal_zone->trip_mutex);
 	kfree(int34x_thermal_zone);
 	return ERR_PTR(ret);
 }
@@ -292,6 +305,7 @@ void int340x_thermal_zone_remove(struct int34x_thermal_zone
 	thermal_zone_device_unregister(int34x_thermal_zone->zone);
 	acpi_lpat_free_conversion_table(int34x_thermal_zone->lpat_table);
 	kfree(int34x_thermal_zone->aux_trips);
+	mutex_destroy(&int34x_thermal_zone->trip_mutex);
 	kfree(int34x_thermal_zone);
 }
 EXPORT_SYMBOL_GPL(int340x_thermal_zone_remove);
drivers/thermal/intel/int340x_thermal/int340x_thermal_zone.h +1 -0
index 3b4971df1b33..8f9872afd0d3 100644
@@ -32,6 +32,7 @@ struct int34x_thermal_zone {
 	struct thermal_zone_device_ops *override_ops;
 	void *priv_data;
 	struct acpi_lpat_conversion_table *lpat_table;
+	struct mutex trip_mutex;
 };
 
 struct int34x_thermal_zone *int340x_thermal_zone_add(struct acpi_device *,
Signature · 1 line
2.38.1
#23 lore raw
Quoted 75 lines · srinivas pandruvada
Quoted 73 lines · Rafael J. Wysocki
Quoted 62 lines · srinivas pandruvada
Quoted 60 lines · Daniel Lezcano
Quoted 46 lines · srinivas pandruvada
Quoted 41 lines · Daniel Lezcano
Quoted 35 lines · srinivas pandruvada
Quoted 26 lines · Daniel Lezcano

On 18/01/2023 20:16, srinivas pandruvada wrote:

[ ... ]

Quoted 10 lines
Quoted 9 lines
Quoted 3 lines

But we'd better wait for the thermald test result
from
Srinvias.

A quick test show that things still work with
thermald
and
these
changes.

Quoted 11 lines

But I have a question. In some devices trip point temperature
is
not
static. When hardware changes, we get notification. For example
INT3403_PERF_TRIP_POINT_CHANGED for INT3403 drivers. Currently get_trip can get the latest changed value. But if
we
preregister, we need some mechanism to update them.

Quoted 7 lines

When the notification INT3403_PERF_TRIP_POINT_CHANGED happens, we
call
int340x_thermal_read_trips() which in turn updates the trip
points.

Quoted 4 lines

Not sure how we handle concurrency here when driver can freely
update
trips while thermal core is using trips.

Quoted 4 lines

Don't we have the same race without this patch ? The thermal core
can
call get_trip_temp() while there is an update, no ?

Quoted 8 lines

Yes it is. But I can add a mutex locally here to solve. But not any longer.

I think you need some thermal_zone_read_lock/unlock() in core, which
can use rcu. Even mutex is fine as there will be no contention as
updates to trips will be rare.

I was planning to provide a thermal_trips_update(tz, trips) and from
there handle the locking.

Quoted 5 lines
Quoted 4 lines

As the race was already existing, can we postpone this change after
the
generic trip points changes?

I think so.

Well, what if this bug is reported by a user and a fix needs to be backported to "stable"?

Quoted 7 lines
Quoted 3 lines

Are we going to backport the whole framework redesign along with it?

Or is this extremely unlikely?

These trips are read at the start of DTT/Thermald and will be read once after notification is received. So extremely unlikely.
But we can add the patch before this series to address this issue, which can be marked stable. I can submit this.

Looks reasonable to me.

#3 lore raw

Re: [PATCH v5 1/3] thermal/acpi: Add ACPI trip point routines

In reply to Rafael J. Wysocki

Hi Rafael,

On 19/01/2023 14:15, Rafael J. Wysocki wrote:

[ ... ]

Quoted 7 lines

drivers/thermal/thermal_acpi.c line 35

Quoted 2 lines
drivers/thermal/thermal_acpi.c new file +2 -0
+static int thermal_acpi_get_temperature_object(struct acpi_device *adev,
+                                              char *object, int *temperature)

So this would become thermal_acpi_get_temp_object(). or even thermal_acpi_get_temp() because it really returns the temperature value.

Quoted 8 lines

I also don't particularly like returning values via pointers, which is entirely avoidable here, because the temperature value obtained from the ACPI control methods must be a positive number.

So I would make it

static int thermal_acpi_get_temp(struct acpi_device *adev, char *object_name) {

We are converting decikelvin -> millicelsius. Even it is very unlikely, the result could be less than zero (eg. -1°C). We won't be able to differentiate -ENODATA with a negative value, no ?

In the future, it is possible we will have to deal with cold trip points in order to warm a board. May be we should don't care for now ?

Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#4 lore raw

Re: [PATCH v5 1/3] thermal/acpi: Add ACPI trip point routines

Quoted 25 lines · Daniel Lezcano

Hi Rafael,

On 19/01/2023 14:15, Rafael J. Wysocki wrote:

[ ... ]

Quoted 7 lines

drivers/thermal/thermal_acpi.c line 35

Quoted 2 lines
drivers/thermal/thermal_acpi.c new file +2 -0
+static int thermal_acpi_get_temperature_object(struct acpi_device *adev,
+                                              char *object, int *temperature)

So this would become thermal_acpi_get_temp_object(). or even thermal_acpi_get_temp() because it really returns the temperature value.

Quoted 8 lines

I also don't particularly like returning values via pointers, which is entirely avoidable here, because the temperature value obtained from the ACPI control methods must be a positive number.

So I would make it

static int thermal_acpi_get_temp(struct acpi_device *adev, char *object_name) {

Quoted 6 lines

We are converting decikelvin -> millicelsius. Even it is very unlikely, the result could be less than zero (eg. -1°C). We won't be able to differentiate -ENODATA with a negative value, no ?

In the future, it is possible we will have to deal with cold trip points in order to warm a board. May be we should don't care for now ?

My point is that the ACPI specification mandates that the return values be in deciK and so always non-negative.

#5 lore raw

Re: [PATCH v5 1/3] thermal/acpi: Add ACPI trip point routines

Quoted 31 lines · Rafael J. Wysocki
Quoted 29 lines · Daniel Lezcano

Hi Rafael,

On 19/01/2023 14:15, Rafael J. Wysocki wrote:

[ ... ]

Quoted 7 lines

drivers/thermal/thermal_acpi.c line 35

Quoted 2 lines
drivers/thermal/thermal_acpi.c new file +2 -0
+static int thermal_acpi_get_temperature_object(struct acpi_device *adev,
+                                              char *object, int *temperature)

So this would become thermal_acpi_get_temp_object(). or even thermal_acpi_get_temp() because it really returns the temperature value.

Quoted 8 lines

I also don't particularly like returning values via pointers, which is entirely avoidable here, because the temperature value obtained from the ACPI control methods must be a positive number.

So I would make it

static int thermal_acpi_get_temp(struct acpi_device *adev, char *object_name) {

We are converting decikelvin -> millicelsius. Even it is very unlikely, the result could be less than zero (eg. -1°C). We won't be able to differentiate -ENODATA with a negative value, no ?

Quoted 5 lines
Quoted 2 lines

In the future, it is possible we will have to deal with cold trip points in order to warm a board. May be we should don't care for now ?

My point is that the ACPI specification mandates that the return values be in deciK and so always non-negative.

I understand that but the code does:

static int thermal_acpi_get_temp(struct acpi_device *adev, char *object_name)

{
	...

	return deci_kelvin_to_millicelsius(temp);
}

All the callers do:

...

        ret = thermal_acpi_get_temp(adev, name);
        if (ret < 0)
		return ret;
	/* This could be an error
	 * or negative millicelsius temperature
	 */

	/* here we already have millicelsius */
        trip->temperature = ret;

...

So I guess we want to do:

...

        ret = thermal_acpi_get_temp(adev, name);
        if (ret < 0)
		return ret;

	/* we convert here instead in thermal_acpi_get_temp() */
        trip->temperature = deci_kelvin_to_millicelsius(ret);

...

Sounds good ?

Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#6 lore raw

Re: [PATCH v5 1/3] thermal/acpi: Add ACPI trip point routines

Quoted 74 lines · Daniel Lezcano
Quoted 31 lines · Rafael J. Wysocki
Quoted 29 lines · Daniel Lezcano

Hi Rafael,

On 19/01/2023 14:15, Rafael J. Wysocki wrote:

[ ... ]

Quoted 7 lines

drivers/thermal/thermal_acpi.c line 35

Quoted 2 lines
drivers/thermal/thermal_acpi.c new file +2 -0
+static int thermal_acpi_get_temperature_object(struct acpi_device *adev,
+                                              char *object, int *temperature)

So this would become thermal_acpi_get_temp_object(). or even thermal_acpi_get_temp() because it really returns the temperature value.

Quoted 8 lines

I also don't particularly like returning values via pointers, which is entirely avoidable here, because the temperature value obtained from the ACPI control methods must be a positive number.

So I would make it

static int thermal_acpi_get_temp(struct acpi_device *adev, char *object_name) {

We are converting decikelvin -> millicelsius. Even it is very unlikely, the result could be less than zero (eg. -1°C). We won't be able to differentiate -ENODATA with a negative value, no ?

Quoted 5 lines
Quoted 2 lines

In the future, it is possible we will have to deal with cold trip points in order to warm a board. May be we should don't care for now ?

My point is that the ACPI specification mandates that the return values be in deciK and so always non-negative.

I understand that but the code does:

static int thermal_acpi_get_temp(struct acpi_device *adev, char *object_name)

{
        ...

        return deci_kelvin_to_millicelsius(temp);
}

All the callers do:

...

         ret = thermal_acpi_get_temp(adev, name);
         if (ret < 0)
                return ret;
        /* This could be an error
         * or negative millicelsius temperature
         */

        /* here we already have millicelsius */
         trip->temperature = ret;

...

So I guess we want to do:

...

         ret = thermal_acpi_get_temp(adev, name);
         if (ret < 0)
                return ret;

        /* we convert here instead in thermal_acpi_get_temp() */
         trip->temperature = deci_kelvin_to_millicelsius(ret);
Quoted 3 lines

...

Sounds good ?

Yes, it does. Convert when it is known to be valid.