Skip to contentSkip to conversation

cpufreq: Add flag to auto-register as cooling

PATCH Superseded Amit Kucheria 3 participants 18 messages Jan 21, 2019 – Jan 23, 2019

Subsystems (6)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover cpufreq: Add flag to auto-register as cooling

Add a flag to be used by cpufreq drivers to tell cpufreq core to auto-register themselves as a thermal cooling device.

There series converts over all the drivers except arm_big_little.c. Tested on SDM845 with the qcom-cpufreq-hw driver. Only compile-tested the others.

Things needing fixing (but not a blocker for the series):
- Look at how to detect that we're not in IKS mode in arm_big_little's .ready callback.

Changes since v1:
- Fix compilation failures with allmodconfig
- Get rid of #ifdef in cpufreq.c
- Removed miscellaneous patches and sent them separately
- Merged patches 1 and 2 from v1

Amit Kucheria (9):
thermal: cpu_cooling: Require thermal core to be compiled in
cpufreq: Auto-register the driver as a thermal cooling device if asked
cpufreq: qcom-hw: Register as a cpufreq cooling device
cpufreq: imx6q: Use auto-registration of thermal cooling device
cpufreq: cpufreq-dt: Use auto-registration of thermal cooling device
cpufreq: mediatek: Use auto-registration of thermal cooling device
cpufreq: qoriq: Use auto-registration of thermal cooling device
cpufreq: scmi: Use auto-registration of thermal cooling device
cpufreq: scpi: Use auto-registration of thermal cooling device

 drivers/cpufreq/cpufreq-dt.c       | 14 ++------------
 drivers/cpufreq/cpufreq.c          |  6 ++++++
 drivers/cpufreq/imx6q-cpufreq.c    | 24 ++----------------------
 drivers/cpufreq/mediatek-cpufreq.c | 14 ++------------
 drivers/cpufreq/qcom-cpufreq-hw.c  |  3 ++-
 drivers/cpufreq/qoriq-cpufreq.c    | 15 ++-------------
 drivers/cpufreq/scmi-cpufreq.c     | 14 ++------------
 drivers/cpufreq/scpi-cpufreq.c     | 14 ++------------
 drivers/thermal/Kconfig            |  1 +
 include/linux/cpufreq.h            | 25 +++++++++++++++++++++++++
 10 files changed, 46 insertions(+), 84 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 1/9 thermal: cpu_cooling: Require thermal core to be compiled in

The CPU cooling driver (cpu_cooling.c) allows the platform's cpufreq driver to register as a cooling device and cool down the platform by throttling the CPU frequency. In order to be able to auto-register a cpufreq driver as a cooling device from the cpufreq core, we need access to code inside cpu_cooling.c which, in turn, accesses code inside thermal core.

CPU_FREQ is a bool while THERMAL is tristate. In some configurations (e.g. allmodconfig), CONFIG_THERMAL ends up as a module while CONFIG_CPU_FREQ is compiled in. This leads to following error:

drivers/cpufreq/cpufreq.o: In function `cpufreq_offline':
cpufreq.c:(.text+0x407c): undefined reference to cpufreq_cooling_unregister' drivers/cpufreq/cpufreq.o: In function cpufreq_online':
cpufreq.c:(.text+0x70c0): undefined reference to `of_cpufreq_cooling_register'

Given that platforms using CPU_THERMAL usually want it compiled-in so it is available early in boot, make CPU_THERMAL depend on THERMAL being compiled-in instead of allowing it to be a module.

---
 drivers/thermal/Kconfig | 1 +
 1 file changed, 1 insertion(+)
drivers/thermal/Kconfig +1 -0
index 30323426902e..58bb7d72dc2b 100644
@@ -152,6 +152,7 @@ config CPU_THERMAL
 	bool "generic cpu cooling support"
 	depends on CPU_FREQ
 	depends on THERMAL_OF
+	depends on THERMAL=y
 	help
 	  This implements the generic cpu cooling mechanism through frequency
 	  reduction. An ACPI version of this already exists
Signature · 1 line
2.17.1
maintainer
#2 lore raw

Re: [PATCH v2 1/9] thermal: cpu_cooling: Require thermal core to be compiled in

Quoted 31 lines · Amit Kucheria

The CPU cooling driver (cpu_cooling.c) allows the platform's cpufreq driver to register as a cooling device and cool down the platform by throttling the CPU frequency. In order to be able to auto-register a cpufreq driver as a cooling device from the cpufreq core, we need access to code inside cpu_cooling.c which, in turn, accesses code inside thermal core.

CPU_FREQ is a bool while THERMAL is tristate. In some configurations (e.g. allmodconfig), CONFIG_THERMAL ends up as a module while CONFIG_CPU_FREQ is compiled in. This leads to following error:

drivers/cpufreq/cpufreq.o: In function `cpufreq_offline':
cpufreq.c:(.text+0x407c): undefined reference to cpufreq_cooling_unregister' drivers/cpufreq/cpufreq.o: In function cpufreq_online':
cpufreq.c:(.text+0x70c0): undefined reference to `of_cpufreq_cooling_register'

Given that platforms using CPU_THERMAL usually want it compiled-in so it is available early in boot, make CPU_THERMAL depend on THERMAL being compiled-in instead of allowing it to be a module.

---
 drivers/thermal/Kconfig | 1 +
 1 file changed, 1 insertion(+)
drivers/thermal/Kconfig +0 -0
index 30323426902e..58bb7d72dc2b 100644

drivers/thermal/Kconfig config CPU_THERMAL · line 158

Quoted 7 lines
drivers/thermal/Kconfig +1 -0
@@ -152,6 +152,7 @@ config CPU_THERMAL
 	bool "generic cpu cooling support"
 	depends on CPU_FREQ
 	depends on THERMAL_OF
+	depends on THERMAL=y
 	help
 	  This implements the generic cpu cooling mechanism through frequency
 	  reduction. An ACPI version of this already exists

Please remove all Kconfig crap, which gets fixed with this, as well in this patch itself. Like:

drivers/cpufreq/Kconfig.arm +0 -2
index 10bc5c798d17..40f8cc323996 100644
@@ -40,8 +40,6 @@ config ARM_ARMADA_8K_CPUFREQ
 config ARM_BIG_LITTLE_CPUFREQ
        tristate "Generic ARM big LITTLE CPUfreq driver"
        depends on ARM_CPU_TOPOLOGY && HAVE_CLK
-       # if CPU_THERMAL is on and THERMAL=m, ARM_BIT_LITTLE_CPUFREQ cannot be =y
-       depends on !CPU_THERMAL || THERMAL
        select PM_OPP
        help
          This enables the Generic CPUfreq driver for ARM big.LITTLE platforms.
Signature · 1 line
viresh
#3 lore raw

Re: [PATCH v2 1/9] thermal: cpu_cooling: Require thermal core to be compiled in

Quoted 28 lines · Viresh Kumar
Quoted 26 lines · Amit Kucheria

The CPU cooling driver (cpu_cooling.c) allows the platform's cpufreq driver to register as a cooling device and cool down the platform by throttling the CPU frequency. In order to be able to auto-register a cpufreq driver as a cooling device from the cpufreq core, we need access to code inside cpu_cooling.c which, in turn, accesses code inside thermal core.

CPU_FREQ is a bool while THERMAL is tristate. In some configurations (e.g. allmodconfig), CONFIG_THERMAL ends up as a module while CONFIG_CPU_FREQ is compiled in. This leads to following error:

drivers/cpufreq/cpufreq.o: In function `cpufreq_offline':
cpufreq.c:(.text+0x407c): undefined reference to cpufreq_cooling_unregister' drivers/cpufreq/cpufreq.o: In function cpufreq_online':
cpufreq.c:(.text+0x70c0): undefined reference to `of_cpufreq_cooling_register'

Given that platforms using CPU_THERMAL usually want it compiled-in so it is available early in boot, make CPU_THERMAL depend on THERMAL being compiled-in instead of allowing it to be a module.

---
 drivers/thermal/Kconfig | 1 +
 1 file changed, 1 insertion(+)
Quoted 15 lines

drivers/thermal/Kconfig config CPU_THERMAL · line 158

Quoted 12 lines
drivers/thermal/Kconfig +1 -0
index 30323426902e..58bb7d72dc2b 100644
@@ -152,6 +152,7 @@ config CPU_THERMAL
      bool "generic cpu cooling support"
      depends on CPU_FREQ
      depends on THERMAL_OF
+     depends on THERMAL=y
      help
        This implements the generic cpu cooling mechanism through frequency
        reduction. An ACPI version of this already exists

Please remove all Kconfig crap, which gets fixed with this, as well in this patch itself. Like:

OK, I planned to if/when this series was accepted. Will send out a patch.

Quoted 16 lines
drivers/cpufreq/Kconfig.arm +0 -3
index 10bc5c798d17..40f8cc323996 100644
@@ -40,8 +40,6 @@ config ARM_ARMADA_8K_CPUFREQ
 config ARM_BIG_LITTLE_CPUFREQ
        tristate "Generic ARM big LITTLE CPUfreq driver"
        depends on ARM_CPU_TOPOLOGY && HAVE_CLK
-       # if CPU_THERMAL is on and THERMAL=m, ARM_BIT_LITTLE_CPUFREQ cannot be =y
-       depends on !CPU_THERMAL || THERMAL
        select PM_OPP
        help
          This enables the Generic CPUfreq driver for ARM big.LITTLE platforms.
 
--

viresh

#4 lore raw

Re: [PATCH v2 1/9] thermal: cpu_cooling: Require thermal core to be compiled in

Quoted 46 lines · Amit Kucheria
Quoted 28 lines · Viresh Kumar
Quoted 26 lines · Amit Kucheria

The CPU cooling driver (cpu_cooling.c) allows the platform's cpufreq driver to register as a cooling device and cool down the platform by throttling the CPU frequency. In order to be able to auto-register a cpufreq driver as a cooling device from the cpufreq core, we need access to code inside cpu_cooling.c which, in turn, accesses code inside thermal core.

CPU_FREQ is a bool while THERMAL is tristate. In some configurations (e.g. allmodconfig), CONFIG_THERMAL ends up as a module while CONFIG_CPU_FREQ is compiled in. This leads to following error:

drivers/cpufreq/cpufreq.o: In function `cpufreq_offline':
cpufreq.c:(.text+0x407c): undefined reference to cpufreq_cooling_unregister' drivers/cpufreq/cpufreq.o: In function cpufreq_online':
cpufreq.c:(.text+0x70c0): undefined reference to `of_cpufreq_cooling_register'

Given that platforms using CPU_THERMAL usually want it compiled-in so it is available early in boot, make CPU_THERMAL depend on THERMAL being compiled-in instead of allowing it to be a module.

---
 drivers/thermal/Kconfig | 1 +
 1 file changed, 1 insertion(+)
Quoted 15 lines

drivers/thermal/Kconfig config CPU_THERMAL · line 158

Quoted 12 lines
drivers/thermal/Kconfig +1 -0
index 30323426902e..58bb7d72dc2b 100644
@@ -152,6 +152,7 @@ config CPU_THERMAL
      bool "generic cpu cooling support"
      depends on CPU_FREQ
      depends on THERMAL_OF
+     depends on THERMAL=y
      help
        This implements the generic cpu cooling mechanism through frequency
        reduction. An ACPI version of this already exists

Please remove all Kconfig crap, which gets fixed with this, as well in this patch itself. Like:

Quoted 1 line

OK, I planned to if/when this series was accepted. Will send out a patch.

You can make it part of this series, though.

#5 lore raw

PATCH v2 2/9 cpufreq: Auto-register the driver as a thermal cooling device if asked

In reply to Amit Kucheria

All cpufreq drivers do similar things to register as a cooling device. Provide a cpufreq driver flag so drivers can just ask the cpufreq core to register the cooling device on their behalf. This allows us to get rid of duplicated code in the drivers.

In order to allow this, we add a struct thermal_cooling_device pointer to struct cpufreq_policy so that drivers don't need to store it in a private data structure.

---
 drivers/cpufreq/cpufreq.c |  6 ++++++
 include/linux/cpufreq.h   | 25 +++++++++++++++++++++++++
 2 files changed, 31 insertions(+)
drivers/cpufreq/cpufreq.c +6 -0
index e35a886e00bc..cf1be057caf4 100644
@@ -1318,6 +1318,9 @@ static int cpufreq_online(unsigned int cpu)
 	if (cpufreq_driver->ready)
 		cpufreq_driver->ready(policy);
 
+	if (cpufreq_driver->flags & CPUFREQ_AUTO_REGISTER_COOLING_DEV)
+		register_cooling_device(policy);
+
 	pr_debug("initialization complete\n");
 
 	return 0;
@@ -1405,6 +1408,9 @@ static int cpufreq_offline(unsigned int cpu)
 		goto unlock;
 	}
 
+	if (cpufreq_driver->flags & CPUFREQ_AUTO_REGISTER_COOLING_DEV)
+		unregister_cooling_device(policy);
+
 	if (cpufreq_driver->stop_cpu)
 		cpufreq_driver->stop_cpu(policy);
 
include/linux/cpufreq.h +25 -0
index bd7fbd6a4478..02fc9f210aee 100644
@@ -13,6 +13,7 @@
 
 #include <linux/clk.h>
 #include <linux/cpumask.h>
+#include <linux/cpu_cooling.h>
 #include <linux/completion.h>
 #include <linux/kobject.h>
 #include <linux/notifier.h>
@@ -151,6 +152,11 @@ struct cpufreq_policy {
 
 	/* For cpufreq driver's internal use */
 	void			*driver_data;
+
+#ifdef CONFIG_CPU_THERMAL
+	/* Pointer to the cooling device if used for thermal mitigation */
+	struct thermal_cooling_device *cdev;
+#endif
 };
 
 /* Only for ACPI */
@@ -386,6 +392,12 @@ struct cpufreq_driver {
  */
 #define CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING	BIT(6)
 
+/*
+ * Set by drivers that want the core to automatically register the cpufreq
+ * driver as a thermal cooling device.
+ */
+#define CPUFREQ_AUTO_REGISTER_COOLING_DEV	BIT(7)
+
 int cpufreq_register_driver(struct cpufreq_driver *driver_data);
 int cpufreq_unregister_driver(struct cpufreq_driver *driver_data);
 
@@ -415,6 +427,19 @@ cpufreq_verify_within_cpu_limits(struct cpufreq_policy *policy)
 			policy->cpuinfo.max_freq);
 }
 
+#ifdef CONFIG_CPU_THERMAL
+static inline void register_cooling_device(struct cpufreq_policy *policy) {
+	policy->cdev = of_cpufreq_cooling_register(policy);
+}
+
+static inline void unregister_cooling_device(struct cpufreq_policy *policy) {
+	cpufreq_cooling_unregister(policy->cdev);
+	policy->cdev = NULL;
+}
+#else
+static inline void register_cooling_device(struct cpufreq_policy *policy) {}
+static inline void unregister_cooling_device(struct cpufreq_policy *policy) {}
+#endif
 #ifdef CONFIG_CPU_FREQ
 void cpufreq_suspend(void);
 void cpufreq_resume(void);
Signature · 1 line
2.17.1
maintainer
#6 lore raw

Re: [PATCH v2 2/9] cpufreq: Auto-register the driver as a thermal cooling device if asked

Quoted 32 lines · Amit Kucheria
include/linux/cpufreq.h +14 -0
@@ -151,6 +152,11 @@ struct cpufreq_policy {
 
 	/* For cpufreq driver's internal use */
 	void			*driver_data;
+
+#ifdef CONFIG_CPU_THERMAL
+	/* Pointer to the cooling device if used for thermal mitigation */
+	struct thermal_cooling_device *cdev;
+#endif
 };
 
 /* Only for ACPI */
@@ -386,6 +392,12 @@ struct cpufreq_driver {
  */
 #define CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING	BIT(6)
 
+/*
+ * Set by drivers that want the core to automatically register the cpufreq
+ * driver as a thermal cooling device.
+ */
+#define CPUFREQ_AUTO_REGISTER_COOLING_DEV	BIT(7)
+
 int cpufreq_register_driver(struct cpufreq_driver *driver_data);
 int cpufreq_unregister_driver(struct cpufreq_driver *driver_data);
 
@@ -415,6 +427,19 @@ cpufreq_verify_within_cpu_limits(struct cpufreq_policy *policy)
 			policy->cpuinfo.max_freq);
 }
 
+#ifdef CONFIG_CPU_THERMAL
+static inline void register_cooling_device(struct cpufreq_policy *policy) {
+	policy->cdev = of_cpufreq_cooling_register(policy);

include/linux/cpufreq.h cpufreq_verify_within_cpu_limits(struct cpufreq_policy *policy) · line 442

Quoted 10 lines
include/linux/cpufreq.h +10 -0
+}
+
+static inline void unregister_cooling_device(struct cpufreq_policy *policy) {
+	cpufreq_cooling_unregister(policy->cdev);
+	policy->cdev = NULL;
+}
+#else
+static inline void register_cooling_device(struct cpufreq_policy *policy) {}
+static inline void unregister_cooling_device(struct cpufreq_policy *policy) {}
+#endif

The whole ifdef hackery here saves space for a pointer per policy. Just get rid of it, it isn't worth it.

Signature · 1 line
viresh
#7 lore raw

Re: [PATCH v2 2/9] cpufreq: Auto-register the driver as a thermal cooling device if asked

Quoted 31 lines · Viresh Kumar
Quoted 29 lines · Amit Kucheria
include/linux/cpufreq.h +11 -0
@@ -151,6 +152,11 @@ struct cpufreq_policy {
 
      /* For cpufreq driver's internal use */
      void                    *driver_data;
+
+#ifdef CONFIG_CPU_THERMAL
+     /* Pointer to the cooling device if used for thermal mitigation */
+     struct thermal_cooling_device *cdev;
+#endif
 };
 
 /* Only for ACPI */
@@ -386,6 +392,12 @@ struct cpufreq_driver {
  */
 #define CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING    BIT(6)
 
+/*
+ * Set by drivers that want the core to automatically register the cpufreq
+ * driver as a thermal cooling device.
+ */
+#define CPUFREQ_AUTO_REGISTER_COOLING_DEV    BIT(7)
+
 int cpufreq_register_driver(struct cpufreq_driver *driver_data);
 int cpufreq_unregister_driver(struct cpufreq_driver *driver_data);
 
@@ -415,6 +427,19 @@ cpufreq_verify_within_cpu_limits(struct cpufreq_policy *policy)
                      policy->cpuinfo.max_freq);
 }
 
Quoted 16 lines

include/linux/cpufreq.h cpufreq_verify_within_cpu_limits(struct cpufreq_policy *policy) · line 442

Quoted 13 lines
include/linux/cpufreq.h +13 -0
+#ifdef CONFIG_CPU_THERMAL
+static inline void register_cooling_device(struct cpufreq_policy *policy) {
+     policy->cdev = of_cpufreq_cooling_register(policy);
+}
+
+static inline void unregister_cooling_device(struct cpufreq_policy *policy) {
+     cpufreq_cooling_unregister(policy->cdev);
+     policy->cdev = NULL;
+}
+#else
+static inline void register_cooling_device(struct cpufreq_policy *policy) {}
+static inline void unregister_cooling_device(struct cpufreq_policy *policy) {}
+#endif

The whole ifdef hackery here saves space for a pointer per policy. Just get rid of it, it isn't worth it.

Is struct thermal_cooling_device defined if CONFIG_THERMAL is unset?

maintainer
#8 lore raw

Re: [PATCH v2 2/9] cpufreq: Auto-register the driver as a thermal cooling device if asked

Quoted 49 lines · Rafael J. Wysocki
Quoted 31 lines · Viresh Kumar
Quoted 29 lines · Amit Kucheria
include/linux/cpufreq.h +11 -0
@@ -151,6 +152,11 @@ struct cpufreq_policy {
 
      /* For cpufreq driver's internal use */
      void                    *driver_data;
+
+#ifdef CONFIG_CPU_THERMAL
+     /* Pointer to the cooling device if used for thermal mitigation */
+     struct thermal_cooling_device *cdev;
+#endif
 };
 
 /* Only for ACPI */
@@ -386,6 +392,12 @@ struct cpufreq_driver {
  */
 #define CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING    BIT(6)
 
+/*
+ * Set by drivers that want the core to automatically register the cpufreq
+ * driver as a thermal cooling device.
+ */
+#define CPUFREQ_AUTO_REGISTER_COOLING_DEV    BIT(7)
+
 int cpufreq_register_driver(struct cpufreq_driver *driver_data);
 int cpufreq_unregister_driver(struct cpufreq_driver *driver_data);
 
@@ -415,6 +427,19 @@ cpufreq_verify_within_cpu_limits(struct cpufreq_policy *policy)
                      policy->cpuinfo.max_freq);
 }
 
Quoted 16 lines

include/linux/cpufreq.h cpufreq_verify_within_cpu_limits(struct cpufreq_policy *policy) · line 442

Quoted 13 lines
include/linux/cpufreq.h +13 -0
+#ifdef CONFIG_CPU_THERMAL
+static inline void register_cooling_device(struct cpufreq_policy *policy) {
+     policy->cdev = of_cpufreq_cooling_register(policy);
+}
+
+static inline void unregister_cooling_device(struct cpufreq_policy *policy) {
+     cpufreq_cooling_unregister(policy->cdev);
+     policy->cdev = NULL;
+}
+#else
+static inline void register_cooling_device(struct cpufreq_policy *policy) {}
+static inline void unregister_cooling_device(struct cpufreq_policy *policy) {}
+#endif

The whole ifdef hackery here saves space for a pointer per policy. Just get rid of it, it isn't worth it.

Quoted 1 line

Is struct thermal_cooling_device defined if CONFIG_THERMAL is unset?

No and it is defined in thermal.h without any ifdef stuff.

Signature · 1 line
viresh
maintainer
#9 lore raw

Re: [PATCH v2 2/9] cpufreq: Auto-register the driver as a thermal cooling device if asked

Quoted 50 lines · Viresh Kumar
Quoted 49 lines · Rafael J. Wysocki
Quoted 47 lines · Viresh Kumar
Quoted 32 lines · Amit Kucheria
include/linux/cpufreq.h +14 -0
@@ -151,6 +152,11 @@ struct cpufreq_policy {
 
      /* For cpufreq driver's internal use */
      void                    *driver_data;
+
+#ifdef CONFIG_CPU_THERMAL
+     /* Pointer to the cooling device if used for thermal mitigation */
+     struct thermal_cooling_device *cdev;
+#endif
 };
 
 /* Only for ACPI */
@@ -386,6 +392,12 @@ struct cpufreq_driver {
  */
 #define CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING    BIT(6)
 
+/*
+ * Set by drivers that want the core to automatically register the cpufreq
+ * driver as a thermal cooling device.
+ */
+#define CPUFREQ_AUTO_REGISTER_COOLING_DEV    BIT(7)
+
 int cpufreq_register_driver(struct cpufreq_driver *driver_data);
 int cpufreq_unregister_driver(struct cpufreq_driver *driver_data);
 
@@ -415,6 +427,19 @@ cpufreq_verify_within_cpu_limits(struct cpufreq_policy *policy)
                      policy->cpuinfo.max_freq);
 }
 
+#ifdef CONFIG_CPU_THERMAL
+static inline void register_cooling_device(struct cpufreq_policy *policy) {
+     policy->cdev = of_cpufreq_cooling_register(policy);

include/linux/cpufreq.h cpufreq_verify_within_cpu_limits(struct cpufreq_policy *policy) · line 442

Quoted 10 lines
include/linux/cpufreq.h +10 -0
+}
+
+static inline void unregister_cooling_device(struct cpufreq_policy *policy) {
+     cpufreq_cooling_unregister(policy->cdev);
+     policy->cdev = NULL;
+}
+#else
+static inline void register_cooling_device(struct cpufreq_policy *policy) {}
+static inline void unregister_cooling_device(struct cpufreq_policy *policy) {}
+#endif

The whole ifdef hackery here saves space for a pointer per policy. Just get rid of it, it isn't worth it.

Quoted 3 lines
Quoted 1 line

Is struct thermal_cooling_device defined if CONFIG_THERMAL is unset?

No and it is defined in thermal.h without any ifdef stuff.

I meant it is always available and doesn't depend on CONFIG_THERMAL.

Signature · 1 line
viresh
#10 lore raw

Re: [PATCH v2 2/9] cpufreq: Auto-register the driver as a thermal cooling device if asked

Quoted 52 lines · Viresh Kumar
Quoted 50 lines · Viresh Kumar
Quoted 49 lines · Rafael J. Wysocki
Quoted 47 lines · Viresh Kumar
Quoted 32 lines · Amit Kucheria
include/linux/cpufreq.h +14 -0
@@ -151,6 +152,11 @@ struct cpufreq_policy {
 
      /* For cpufreq driver's internal use */
      void                    *driver_data;
+
+#ifdef CONFIG_CPU_THERMAL
+     /* Pointer to the cooling device if used for thermal mitigation */
+     struct thermal_cooling_device *cdev;
+#endif
 };
 
 /* Only for ACPI */
@@ -386,6 +392,12 @@ struct cpufreq_driver {
  */
 #define CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING    BIT(6)
 
+/*
+ * Set by drivers that want the core to automatically register the cpufreq
+ * driver as a thermal cooling device.
+ */
+#define CPUFREQ_AUTO_REGISTER_COOLING_DEV    BIT(7)
+
 int cpufreq_register_driver(struct cpufreq_driver *driver_data);
 int cpufreq_unregister_driver(struct cpufreq_driver *driver_data);
 
@@ -415,6 +427,19 @@ cpufreq_verify_within_cpu_limits(struct cpufreq_policy *policy)
                      policy->cpuinfo.max_freq);
 }
 
+#ifdef CONFIG_CPU_THERMAL
+static inline void register_cooling_device(struct cpufreq_policy *policy) {
+     policy->cdev = of_cpufreq_cooling_register(policy);

include/linux/cpufreq.h cpufreq_verify_within_cpu_limits(struct cpufreq_policy *policy) · line 442

Quoted 10 lines
include/linux/cpufreq.h +10 -0
+}
+
+static inline void unregister_cooling_device(struct cpufreq_policy *policy) {
+     cpufreq_cooling_unregister(policy->cdev);
+     policy->cdev = NULL;
+}
+#else
+static inline void register_cooling_device(struct cpufreq_policy *policy) {}
+static inline void unregister_cooling_device(struct cpufreq_policy *policy) {}
+#endif

The whole ifdef hackery here saves space for a pointer per policy. Just get rid of it, it isn't worth it.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Is struct thermal_cooling_device defined if CONFIG_THERMAL is unset?

No and it is defined in thermal.h without any ifdef stuff.

I meant it is always available and doesn't depend on CONFIG_THERMAL.

OK

I guess we can live with an extra unused pointer per policy on platforms with CONFIG_CPU_THERMAL unset.

#11 lore raw

PATCH v2 3/9 cpufreq: qcom-hw: Register as a cpufreq cooling device

In reply to Amit Kucheria

Add the CPUFREQ_AUTO_REGISTER_COOLING_DEV flag to allow the cpufreq core to auto-register the driver as a cooling device.

---
 drivers/cpufreq/qcom-cpufreq-hw.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/cpufreq/qcom-cpufreq-hw.c +2 -1
index d83939a1b3d4..ed32849a3d40 100644
@@ -231,7 +231,8 @@ static struct freq_attr *qcom_cpufreq_hw_attr[] = {
 
 static struct cpufreq_driver cpufreq_qcom_hw_driver = {
 	.flags		= CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK |
-			  CPUFREQ_HAVE_GOVERNOR_PER_POLICY,
+			  CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
+			  CPUFREQ_AUTO_REGISTER_COOLING_DEV,
 	.verify		= cpufreq_generic_frequency_table_verify,
 	.target_index	= qcom_cpufreq_hw_target_index,
 	.get		= qcom_cpufreq_hw_get,
Signature · 1 line
2.17.1
#12 lore raw

PATCH v2 4/9 cpufreq: imx6q: Use auto-registration of thermal cooling device

In reply to Amit Kucheria

Use the CPUFREQ_AUTO_REGISTER_COOLING_DEV flag to allow cpufreq core to automatically register as a thermal cooling device.

This allows removal of boiler plate code from the driver.

---
 drivers/cpufreq/imx6q-cpufreq.c | 24 ++----------------------
 1 file changed, 2 insertions(+), 22 deletions(-)
drivers/cpufreq/imx6q-cpufreq.c +2 -22
index 9fedf627e000..9935df234fa1 100644
@@ -9,7 +9,6 @@
 #include <linux/clk.h>
 #include <linux/cpu.h>
 #include <linux/cpufreq.h>
-#include <linux/cpu_cooling.h>
 #include <linux/err.h>
 #include <linux/module.h>
 #include <linux/nvmem-consumer.h>
@@ -52,7 +51,6 @@ static struct clk_bulk_data clks[] = {
 };
 
 static struct device *cpu_dev;
-static struct thermal_cooling_device *cdev;
 static bool free_opp;
 static struct cpufreq_frequency_table *freq_table;
 static unsigned int max_freq;
@@ -193,16 +191,6 @@ static int imx6q_set_target(struct cpufreq_policy *policy, unsigned int index)
 	return 0;
 }
 
-static void imx6q_cpufreq_ready(struct cpufreq_policy *policy)
-{
-	cdev = of_cpufreq_cooling_register(policy);
-
-	if (!cdev)
-		dev_err(cpu_dev,
-			"running cpufreq without cooling device: %ld\n",
-			PTR_ERR(cdev));
-}
-
 static int imx6q_cpufreq_init(struct cpufreq_policy *policy)
 {
 	int ret;
@@ -214,22 +202,14 @@ static int imx6q_cpufreq_init(struct cpufreq_policy *policy)
 	return ret;
 }
 
-static int imx6q_cpufreq_exit(struct cpufreq_policy *policy)
-{
-	cpufreq_cooling_unregister(cdev);
-
-	return 0;
-}
-
 static struct cpufreq_driver imx6q_cpufreq_driver = {
-	.flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK,
+	.flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK |
+		 CPUFREQ_AUTO_REGISTER_COOLING_DEV,
 	.verify = cpufreq_generic_frequency_table_verify,
 	.target_index = imx6q_set_target,
 	.get = cpufreq_generic_get,
 	.init = imx6q_cpufreq_init,
-	.exit = imx6q_cpufreq_exit,
 	.name = "imx6q-cpufreq",
-	.ready = imx6q_cpufreq_ready,
 	.attr = cpufreq_generic_attr,
 	.suspend = cpufreq_generic_suspend,
 };
Signature · 1 line
2.17.1
#13 lore raw

PATCH v2 5/9 cpufreq: cpufreq-dt: Use auto-registration of thermal cooling device

In reply to Amit Kucheria

Use the CPUFREQ_AUTO_REGISTER_COOLING_DEV flag to allow cpufreq core to automatically register as a thermal cooling device.

This allows removal of boiler plate code from the driver.

---
 drivers/cpufreq/cpufreq-dt.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)
drivers/cpufreq/cpufreq-dt.c +2 -12
index e58bfcb1169e..2a4c4ea7980b 100644
@@ -13,7 +13,6 @@
 
 #include <linux/clk.h>
 #include <linux/cpu.h>
-#include <linux/cpu_cooling.h>
 #include <linux/cpufreq.h>
 #include <linux/cpumask.h>
 #include <linux/err.h>
@@ -30,7 +29,6 @@
 struct private_data {
 	struct opp_table *opp_table;
 	struct device *cpu_dev;
-	struct thermal_cooling_device *cdev;
 	const char *reg_name;
 	bool have_static_opps;
 };
@@ -301,7 +299,6 @@ static int cpufreq_exit(struct cpufreq_policy *policy)
 {
 	struct private_data *priv = policy->driver_data;
 
-	cpufreq_cooling_unregister(priv->cdev);
 	dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table);
 	if (priv->have_static_opps)
 		dev_pm_opp_of_cpumask_remove_table(policy->related_cpus);
@@ -314,21 +311,14 @@ static int cpufreq_exit(struct cpufreq_policy *policy)
 	return 0;
 }
 
-static void cpufreq_ready(struct cpufreq_policy *policy)
-{
-	struct private_data *priv = policy->driver_data;
-
-	priv->cdev = of_cpufreq_cooling_register(policy);
-}
-
 static struct cpufreq_driver dt_cpufreq_driver = {
-	.flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK,
+	.flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK |
+		 CPUFREQ_AUTO_REGISTER_COOLING_DEV,
 	.verify = cpufreq_generic_frequency_table_verify,
 	.target_index = set_target,
 	.get = cpufreq_generic_get,
 	.init = cpufreq_init,
 	.exit = cpufreq_exit,
-	.ready = cpufreq_ready,
 	.name = "cpufreq-dt",
 	.attr = cpufreq_dt_attr,
 	.suspend = cpufreq_generic_suspend,
Signature · 1 line
2.17.1
#14 lore raw

PATCH v2 6/9 cpufreq: mediatek: Use auto-registration of thermal cooling device

In reply to Amit Kucheria

Use the CPUFREQ_AUTO_REGISTER_COOLING_DEV flag to allow cpufreq core to automatically register as a thermal cooling device.

This allows removal of boiler plate code from the driver.

---
 drivers/cpufreq/mediatek-cpufreq.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)
drivers/cpufreq/mediatek-cpufreq.c +2 -12
index eb8920d39818..9a937f4c63e7 100644
@@ -14,7 +14,6 @@
 
 #include <linux/clk.h>
 #include <linux/cpu.h>
-#include <linux/cpu_cooling.h>
 #include <linux/cpufreq.h>
 #include <linux/cpumask.h>
 #include <linux/module.h>
@@ -48,7 +47,6 @@ struct mtk_cpu_dvfs_info {
 	struct regulator *sram_reg;
 	struct clk *cpu_clk;
 	struct clk *inter_clk;
-	struct thermal_cooling_device *cdev;
 	struct list_head list_head;
 	int intermediate_voltage;
 	bool need_voltage_tracking;
@@ -307,13 +305,6 @@ static int mtk_cpufreq_set_target(struct cpufreq_policy *policy,
 
 #define DYNAMIC_POWER "dynamic-power-coefficient"
 
-static void mtk_cpufreq_ready(struct cpufreq_policy *policy)
-{
-	struct mtk_cpu_dvfs_info *info = policy->driver_data;
-
-	info->cdev = of_cpufreq_cooling_register(policy);
-}
-
 static int mtk_cpu_dvfs_info_init(struct mtk_cpu_dvfs_info *info, int cpu)
 {
 	struct device *cpu_dev;
@@ -472,7 +463,6 @@ static int mtk_cpufreq_exit(struct cpufreq_policy *policy)
 {
 	struct mtk_cpu_dvfs_info *info = policy->driver_data;
 
-	cpufreq_cooling_unregister(info->cdev);
 	dev_pm_opp_free_cpufreq_table(info->cpu_dev, &policy->freq_table);
 
 	return 0;
@@ -480,13 +470,13 @@ static int mtk_cpufreq_exit(struct cpufreq_policy *policy)
 
 static struct cpufreq_driver mtk_cpufreq_driver = {
 	.flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK |
-		 CPUFREQ_HAVE_GOVERNOR_PER_POLICY,
+		 CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
+		 CPUFREQ_AUTO_REGISTER_COOLING_DEV,
 	.verify = cpufreq_generic_frequency_table_verify,
 	.target_index = mtk_cpufreq_set_target,
 	.get = cpufreq_generic_get,
 	.init = mtk_cpufreq_init,
 	.exit = mtk_cpufreq_exit,
-	.ready = mtk_cpufreq_ready,
 	.name = "mtk-cpufreq",
 	.attr = cpufreq_generic_attr,
 };
Signature · 1 line
2.17.1
#15 lore raw

PATCH v2 7/9 cpufreq: qoriq: Use auto-registration of thermal cooling device

In reply to Amit Kucheria

Use the CPUFREQ_AUTO_REGISTER_COOLING_DEV flag to allow cpufreq core to automatically register as a thermal cooling device.

This allows removal of boiler plate code from the driver.

---
 drivers/cpufreq/qoriq-cpufreq.c | 15 ++-------------
 1 file changed, 2 insertions(+), 13 deletions(-)
drivers/cpufreq/qoriq-cpufreq.c +2 -13
index 3d773f64b4df..b206e6cb55f0 100644
@@ -13,7 +13,6 @@
 #include <linux/clk.h>
 #include <linux/clk-provider.h>
 #include <linux/cpufreq.h>
-#include <linux/cpu_cooling.h>
 #include <linux/errno.h>
 #include <linux/init.h>
 #include <linux/kernel.h>
@@ -31,7 +30,6 @@
 struct cpu_data {
 	struct clk **pclk;
 	struct cpufreq_frequency_table *table;
-	struct thermal_cooling_device *cdev;
 };
 
 /*
@@ -239,7 +237,6 @@ static int qoriq_cpufreq_cpu_exit(struct cpufreq_policy *policy)
 {
 	struct cpu_data *data = policy->driver_data;
 
-	cpufreq_cooling_unregister(data->cdev);
 	kfree(data->pclk);
 	kfree(data->table);
 	kfree(data);
@@ -258,23 +255,15 @@ static int qoriq_cpufreq_target(struct cpufreq_policy *policy,
 	return clk_set_parent(policy->clk, parent);
 }
 
-
-static void qoriq_cpufreq_ready(struct cpufreq_policy *policy)
-{
-	struct cpu_data *cpud = policy->driver_data;
-
-	cpud->cdev = of_cpufreq_cooling_register(policy);
-}
-
 static struct cpufreq_driver qoriq_cpufreq_driver = {
 	.name		= "qoriq_cpufreq",
-	.flags		= CPUFREQ_CONST_LOOPS,
+	.flags		= CPUFREQ_CONST_LOOPS |
+			  CPUFREQ_AUTO_REGISTER_COOLING_DEV,
 	.init		= qoriq_cpufreq_cpu_init,
 	.exit		= qoriq_cpufreq_cpu_exit,
 	.verify		= cpufreq_generic_frequency_table_verify,
 	.target_index	= qoriq_cpufreq_target,
 	.get		= cpufreq_generic_get,
-	.ready		= qoriq_cpufreq_ready,
 	.attr		= cpufreq_generic_attr,
 };
 
Signature · 1 line
2.17.1
#16 lore raw

PATCH v2 8/9 cpufreq: scmi: Use auto-registration of thermal cooling device

In reply to Amit Kucheria

Use the CPUFREQ_AUTO_REGISTER_COOLING_DEV flag to allow cpufreq core to automatically register as a thermal cooling device.

This allows removal of boiler plate code from the driver.

---
 drivers/cpufreq/scmi-cpufreq.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)
drivers/cpufreq/scmi-cpufreq.c +2 -12
index 242c3370544e..b19e9d129f8f 100644
@@ -11,7 +11,6 @@
 #include <linux/cpu.h>
 #include <linux/cpufreq.h>
 #include <linux/cpumask.h>
-#include <linux/cpu_cooling.h>
 #include <linux/export.h>
 #include <linux/module.h>
 #include <linux/pm_opp.h>
@@ -22,7 +21,6 @@
 struct scmi_data {
 	int domain_id;
 	struct device *cpu_dev;
-	struct thermal_cooling_device *cdev;
 };
 
 static const struct scmi_handle *handle;
@@ -185,7 +183,6 @@ static int scmi_cpufreq_exit(struct cpufreq_policy *policy)
 {
 	struct scmi_data *priv = policy->driver_data;
 
-	cpufreq_cooling_unregister(priv->cdev);
 	dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table);
 	kfree(priv);
 	dev_pm_opp_remove_all_dynamic(priv->cpu_dev);
@@ -193,17 +190,11 @@ static int scmi_cpufreq_exit(struct cpufreq_policy *policy)
 	return 0;
 }
 
-static void scmi_cpufreq_ready(struct cpufreq_policy *policy)
-{
-	struct scmi_data *priv = policy->driver_data;
-
-	priv->cdev = of_cpufreq_cooling_register(policy);
-}
-
 static struct cpufreq_driver scmi_cpufreq_driver = {
 	.name	= "scmi",
 	.flags	= CPUFREQ_STICKY | CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
-		  CPUFREQ_NEED_INITIAL_FREQ_CHECK,
+		  CPUFREQ_NEED_INITIAL_FREQ_CHECK |
+		  CPUFREQ_AUTO_REGISTER_COOLING_DEV,
 	.verify	= cpufreq_generic_frequency_table_verify,
 	.attr	= cpufreq_generic_attr,
 	.target_index	= scmi_cpufreq_set_target,
@@ -211,7 +202,6 @@ static struct cpufreq_driver scmi_cpufreq_driver = {
 	.get	= scmi_cpufreq_get_rate,
 	.init	= scmi_cpufreq_init,
 	.exit	= scmi_cpufreq_exit,
-	.ready	= scmi_cpufreq_ready,
 };
 
 static int scmi_cpufreq_probe(struct scmi_device *sdev)
Signature · 1 line
2.17.1
#17 lore raw

PATCH v2 9/9 cpufreq: scpi: Use auto-registration of thermal cooling device

In reply to Amit Kucheria

Use the CPUFREQ_AUTO_REGISTER_COOLING_DEV flag to allow cpufreq core to automatically register as a thermal cooling device.

This allows removal of boiler plate code from the driver.

---
 drivers/cpufreq/scpi-cpufreq.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)
drivers/cpufreq/scpi-cpufreq.c +2 -12
index 99449738faa4..82420e8e5f0d 100644
@@ -22,7 +22,6 @@
 #include <linux/cpu.h>
 #include <linux/cpufreq.h>
 #include <linux/cpumask.h>
-#include <linux/cpu_cooling.h>
 #include <linux/export.h>
 #include <linux/module.h>
 #include <linux/of_platform.h>
@@ -34,7 +33,6 @@
 struct scpi_data {
 	struct clk *clk;
 	struct device *cpu_dev;
-	struct thermal_cooling_device *cdev;
 };
 
 static struct scpi_ops *scpi_ops;
@@ -186,7 +184,6 @@ static int scpi_cpufreq_exit(struct cpufreq_policy *policy)
 {
 	struct scpi_data *priv = policy->driver_data;
 
-	cpufreq_cooling_unregister(priv->cdev);
 	clk_put(priv->clk);
 	dev_pm_opp_free_cpufreq_table(priv->cpu_dev, &policy->freq_table);
 	kfree(priv);
@@ -195,23 +192,16 @@ static int scpi_cpufreq_exit(struct cpufreq_policy *policy)
 	return 0;
 }
 
-static void scpi_cpufreq_ready(struct cpufreq_policy *policy)
-{
-	struct scpi_data *priv = policy->driver_data;
-
-	priv->cdev = of_cpufreq_cooling_register(policy);
-}
-
 static struct cpufreq_driver scpi_cpufreq_driver = {
 	.name	= "scpi-cpufreq",
 	.flags	= CPUFREQ_STICKY | CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
-		  CPUFREQ_NEED_INITIAL_FREQ_CHECK,
+		  CPUFREQ_NEED_INITIAL_FREQ_CHECK |
+		  CPUFREQ_AUTO_REGISTER_COOLING_DEV,
 	.verify	= cpufreq_generic_frequency_table_verify,
 	.attr	= cpufreq_generic_attr,
 	.get	= scpi_cpufreq_get_rate,
 	.init	= scpi_cpufreq_init,
 	.exit	= scpi_cpufreq_exit,
-	.ready	= scpi_cpufreq_ready,
 	.target_index	= scpi_cpufreq_set_target,
 };
 
Signature · 1 line
2.17.1