Skip to contentSkip to conversation

Devfreq cooling device

PATCH Superseded Javi Merino 1 participant 5 messages Jul 13, 2015

Subsystems (3)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 cover Devfreq cooling device

This series introduces a devfreq cooling device in the thermal framework. Devfreq is used for DVFS for devices other than the CPUs. With a devfreq cooling device, the thermal framework can throttle them to control temperature. The cooling device has the power extensions, so it can be used by all governors in the thermal framework, including the power allocator governor.

Changes since v1:
- Rename devfreq_qos_set_[max|min] to devfreq_set_[max|min] as suggested by MyungJoo Ham
- Calculate devfreq load in the tracepoint so that it only happens when the trace is enabled. Thanks Steven Rostedt.

Javi Merino (2):
PM / devfreq: cache the last call to get_dev_status()
devfreq_cooling: add trace information

Ørjan Eide (2):
PM / devfreq: Add function to set max/min frequency
thermal: Add devfreq cooling

 drivers/devfreq/devfreq.c                 |  77 ++++--
 drivers/devfreq/governor_simpleondemand.c |  33 +--
 drivers/thermal/Kconfig                   |  10 +
 drivers/thermal/Makefile                  |   3 +
 drivers/thermal/devfreq_cooling.c         | 394 ++++++++++++++++++++++++++++++
 include/linux/devfreq.h                   |  20 ++
 include/linux/devfreq_cooling.h           |  90 +++++++
 include/trace/events/thermal.h            |  53 ++++
 8 files changed, 641 insertions(+), 39 deletions(-)
 create mode 100644 drivers/thermal/devfreq_cooling.c
 create mode 100644 include/linux/devfreq_cooling.h
Signature · 1 line
1.9.1
#1 lore raw

PATCH v2 1/4 PM / devfreq: Add function to set max/min frequency

From: Ørjan Eide <[email protected]>

Factor out the logic to set minimum and maximum frequency for the device so that it can be used by other parts of the kernel.

Cc MyungJoo Ham <[email protected]>
Cc Kyungmin Park <[email protected]>

---
Changes since v1:
- Rename devfreq_qos_set_[max|min] to devfreq_set_[max|min] as suggested by MyungJoo Ham

 drivers/devfreq/devfreq.c | 72 +++++++++++++++++++++++++++++++----------------
 include/linux/devfreq.h   | 13 +++++++++
 2 files changed, 61 insertions(+), 24 deletions(-)
drivers/devfreq/devfreq.c +48 -24
index ca1b362d77e2..5b3f2f32f1e5 100644
@@ -768,6 +768,48 @@ err_out:
 }
 EXPORT_SYMBOL(devfreq_remove_governor);
 
+int devfreq_set_max(struct devfreq *df, unsigned long value)
+{
+	unsigned long min;
+	int ret = 0;
+
+	mutex_lock(&df->lock);
+	min = df->min_freq;
+	if (value && min && value < min) {
+		ret = -EINVAL;
+		goto unlock;
+	}
+
+	df->max_freq = value;
+	update_devfreq(df);
+unlock:
+	mutex_unlock(&df->lock);
+
+	return ret;
+}
+EXPORT_SYMBOL(devfreq_set_max);
+
+int devfreq_set_min(struct devfreq *df, unsigned long value)
+{
+	unsigned long max;
+	int ret = 0;
+
+	mutex_lock(&df->lock);
+	max = df->max_freq;
+	if (value && max && value > max) {
+		ret = -EINVAL;
+		goto unlock;
+	}
+
+	df->min_freq = value;
+	update_devfreq(df);
+unlock:
+	mutex_unlock(&df->lock);
+
+	return ret;
+}
+EXPORT_SYMBOL(devfreq_set_min);
+
 static ssize_t governor_show(struct device *dev,
 			     struct device_attribute *attr, char *buf)
 {
@@ -899,24 +941,15 @@ static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
 	struct devfreq *df = to_devfreq(dev);
 	unsigned long value;
 	int ret;
-	unsigned long max;
 
 	ret = sscanf(buf, "%lu", &value);
 	if (ret != 1)
 		return -EINVAL;
 
-	mutex_lock(&df->lock);
-	max = df->max_freq;
-	if (value && max && value > max) {
-		ret = -EINVAL;
-		goto unlock;
-	}
+	ret = devfreq_set_min(df, value);
+	if (!ret)
+		ret = count;
 
-	df->min_freq = value;
-	update_devfreq(df);
-	ret = count;
-unlock:
-	mutex_unlock(&df->lock);
 	return ret;
 }
 
@@ -932,24 +965,15 @@ static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
 	struct devfreq *df = to_devfreq(dev);
 	unsigned long value;
 	int ret;
-	unsigned long min;
 
 	ret = sscanf(buf, "%lu", &value);
 	if (ret != 1)
 		return -EINVAL;
 
-	mutex_lock(&df->lock);
-	min = df->min_freq;
-	if (value && min && value < min) {
-		ret = -EINVAL;
-		goto unlock;
-	}
+	ret = devfreq_set_max(df, value);
+	if (!ret)
+		ret = count;
 
-	df->max_freq = value;
-	update_devfreq(df);
-	ret = count;
-unlock:
-	mutex_unlock(&df->lock);
 	return ret;
 }
 static DEVICE_ATTR_RW(min_freq);
include/linux/devfreq.h +13 -0
index ce447f0f1bad..dce82c695955 100644
@@ -204,6 +204,9 @@ extern int devm_devfreq_register_opp_notifier(struct device *dev,
 extern void devm_devfreq_unregister_opp_notifier(struct device *dev,
 						struct devfreq *devfreq);
 
+int devfreq_set_min(struct devfreq *df, unsigned long value);
+int devfreq_set_max(struct devfreq *df, unsigned long value);
+
 #if IS_ENABLED(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND)
 /**
  * struct devfreq_simple_ondemand_data - void *data fed to struct devfreq
@@ -289,6 +292,16 @@ static inline void devm_devfreq_unregister_opp_notifier(struct device *dev,
 							struct devfreq *devfreq)
 {
 }
+
+static inline int devfreq_set_min(struct devfreq *df, unsigned long value)
+{
+	return -EINVAL;
+}
+
+static inline int devfreq_set_max(struct devfreq *df, unsigned long value)
+{
+	return -EINVAL;
+}
 #endif /* CONFIG_PM_DEVFREQ */
 
 #endif /* __LINUX_DEVFREQ_H__ */
Signature · 1 line
1.9.1
#2 lore raw

PATCH v2 2/4 PM / devfreq: cache the last call to get_dev_status()

In reply to Javi Merino

The return value of get_dev_status() can be reused. Cache it so that other parts of the kernel can reuse it instead of having to call the same function again.

Cc MyungJoo Ham <[email protected]>
Cc Kyungmin Park <[email protected]>

---

This patch tries to let multiple components. To summarize the discussion in v1[1] I can think of three alternatives:

1) Change get_dev_status() to return absolute values for busy_time and total_time
2) Make core devfreq call get_dev_status() periodically (for example, before calling the governor) and all the entities that want access can do so via a pointer in devfreq
3) Make the simple ondemand governor call get_dev_status() periodically and cache it, forcing all the other entities to rely on that governor being active

[1] http://thread.gmane.org/gmane.linux.power-management.general/61936/focus=61993

This patch implements option 3)

 drivers/devfreq/devfreq.c                 |  5 +++++
 drivers/devfreq/governor_simpleondemand.c | 33 +++++++++++++++++--------------
 include/linux/devfreq.h                   |  7 +++++++
 3 files changed, 30 insertions(+), 15 deletions(-)
drivers/devfreq/devfreq.c +5 -0
index 5b3f2f32f1e5..83041931910f 100644
@@ -810,6 +810,11 @@ unlock:
 }
 EXPORT_SYMBOL(devfreq_set_min);
 
+int devfreq_update_stats(struct devfreq *df)
+{
+	return df->profile->get_dev_status(df->dev.parent, &df->last_status);
+}
+
 static ssize_t governor_show(struct device *dev,
 			     struct device_attribute *attr, char *buf)
 {
drivers/devfreq/governor_simpleondemand.c +18 -15
index 0720ba84ca92..ae72ba5e78df 100644
@@ -21,17 +21,20 @@
 static int devfreq_simple_ondemand_func(struct devfreq *df,
 					unsigned long *freq)
 {
-	struct devfreq_dev_status stat;
-	int err = df->profile->get_dev_status(df->dev.parent, &stat);
+	int err;
+	struct devfreq_dev_status *stat;
 	unsigned long long a, b;
 	unsigned int dfso_upthreshold = DFSO_UPTHRESHOLD;
 	unsigned int dfso_downdifferential = DFSO_DOWNDIFFERENCTIAL;
 	struct devfreq_simple_ondemand_data *data = df->data;
 	unsigned long max = (df->max_freq) ? df->max_freq : UINT_MAX;
 
+	err = devfreq_update_stats(df);
 	if (err)
 		return err;
 
+	stat = &df->last_status;
+
 	if (data) {
 		if (data->upthreshold)
 			dfso_upthreshold = data->upthreshold;
@@ -43,41 +46,41 @@ static int devfreq_simple_ondemand_func(struct devfreq *df,
 		return -EINVAL;
 
 	/* Assume MAX if it is going to be divided by zero */
-	if (stat.total_time == 0) {
+	if (stat->total_time == 0) {
 		*freq = max;
 		return 0;
 	}
 
 	/* Prevent overflow */
-	if (stat.busy_time >= (1 << 24) || stat.total_time >= (1 << 24)) {
-		stat.busy_time >>= 7;
-		stat.total_time >>= 7;
+	if (stat->busy_time >= (1 << 24) || stat->total_time >= (1 << 24)) {
+		stat->busy_time >>= 7;
+		stat->total_time >>= 7;
 	}
 
 	/* Set MAX if it's busy enough */
-	if (stat.busy_time * 100 >
-	    stat.total_time * dfso_upthreshold) {
+	if (stat->busy_time * 100 >
+	    stat->total_time * dfso_upthreshold) {
 		*freq = max;
 		return 0;
 	}
 
 	/* Set MAX if we do not know the initial frequency */
-	if (stat.current_frequency == 0) {
+	if (stat->current_frequency == 0) {
 		*freq = max;
 		return 0;
 	}
 
 	/* Keep the current frequency */
-	if (stat.busy_time * 100 >
-	    stat.total_time * (dfso_upthreshold - dfso_downdifferential)) {
-		*freq = stat.current_frequency;
+	if (stat->busy_time * 100 >
+	    stat->total_time * (dfso_upthreshold - dfso_downdifferential)) {
+		*freq = stat->current_frequency;
 		return 0;
 	}
 
 	/* Set the desired frequency based on the load */
-	a = stat.busy_time;
-	a *= stat.current_frequency;
-	b = div_u64(a, stat.total_time);
+	a = stat->busy_time;
+	a *= stat->current_frequency;
+	b = div_u64(a, stat->total_time);
 	b *= 100;
 	b = div_u64(b, (dfso_upthreshold - dfso_downdifferential / 2));
 	*freq = (unsigned long) b;
include/linux/devfreq.h +7 -0
index dce82c695955..1bad6fecf285 100644
@@ -161,6 +161,7 @@ struct devfreq {
 	struct delayed_work work;
 
 	unsigned long previous_freq;
+	struct devfreq_dev_status last_status;
 
 	void *data; /* private data for governors */
 
@@ -206,6 +207,7 @@ extern void devm_devfreq_unregister_opp_notifier(struct device *dev,
 
 int devfreq_set_min(struct devfreq *df, unsigned long value);
 int devfreq_set_max(struct devfreq *df, unsigned long value);
+int devfreq_update_stats(struct devfreq *df);
 
 #if IS_ENABLED(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND)
 /**
@@ -302,6 +304,11 @@ static inline int devfreq_set_max(struct devfreq *df, unsigned long value)
 {
 	return -EINVAL;
 }
+
+static inline int devfreq_update_stats(struct devfreq *df)
+{
+	return -EINVAL;
+}
 #endif /* CONFIG_PM_DEVFREQ */
 
 #endif /* __LINUX_DEVFREQ_H__ */
Signature · 1 line
1.9.1
#3 lore raw

PATCH v2 3/4 thermal: Add devfreq cooling

In reply to Javi Merino

From: Ørjan Eide <[email protected]>

Add a generic thermal cooling device for devfreq, that is similar to cpu_cooling.

The device must use devfreq. In order to use the power extension of the cooling device, it must have registered its OPPs using the OPP library.

Cc Zhang Rui <[email protected]>
Cc Eduardo Valentin <[email protected]>
---
 drivers/thermal/Kconfig           |  10 +
 drivers/thermal/Makefile          |   3 +
 drivers/thermal/devfreq_cooling.c | 388 ++++++++++++++++++++++++++++++++++++++
 include/linux/devfreq_cooling.h   |  90 +++++++++
 4 files changed, 491 insertions(+)
 create mode 100644 drivers/thermal/devfreq_cooling.c
 create mode 100644 include/linux/devfreq_cooling.h
drivers/thermal/Kconfig +10 -0
index 118938ee8552..da339ff7c3c9 100644
@@ -147,6 +147,16 @@ config CLOCK_THERMAL
 	  device that is configured to use this cooling mechanism will be
 	  controlled to reduce clock frequency whenever temperature is high.
 
+config DEVFREQ_THERMAL
+	bool "Generic device cooling support"
+	depends on PM_DEVFREQ
+	help
+	  This implements the generic devfreq cooling mechanism through
+	  frequency reduction for devices using devfreq.
+
+	  This will throttle the device by limiting the maximum allowed DVFS
+	  frequency corresponding to the cooling level.
+
 	  If you want this support, you should say Y here.
 
 config THERMAL_EMULATION
drivers/thermal/Makefile +3 -0
index 535dfee1496f..45f26978ff74 100644
@@ -22,6 +22,9 @@ thermal_sys-$(CONFIG_CPU_THERMAL)	+= cpu_cooling.o
 # clock cooling
 thermal_sys-$(CONFIG_CLOCK_THERMAL)	+= clock_cooling.o
 
+# devfreq cooling
+thermal_sys-$(CONFIG_DEVFREQ_THERMAL) += devfreq_cooling.o
+
 # platform thermal drivers
 obj-$(CONFIG_QCOM_SPMI_TEMP_ALARM)	+= qcom-spmi-temp-alarm.o
 obj-$(CONFIG_SPEAR_THERMAL)	+= spear_thermal.o
drivers/thermal/devfreq_cooling.c new file +388 -0

388 lines not on this page · raw message

include/linux/devfreq_cooling.h new file +90 -0

90 lines not on this page · raw message

Signature · 1 line
1.9.1
#4 lore raw

PATCH v2 4/4 devfreq_cooling: add trace information

In reply to Javi Merino

Tracing is useful for debugging and performance tuning. Add similar traces to what's present in the cpu cooling device.

Cc Zhang Rui <[email protected]>
Cc Eduardo Valentin <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Ingo Molnar <[email protected]>

---
Changes since v1:
- Calculate devfreq load in the tracepoint so that it only happens when the trace is enabled. Thanks Steven Rostedt.

 drivers/thermal/devfreq_cooling.c |  6 +++++
 include/trace/events/thermal.h    | 53 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+)
drivers/thermal/devfreq_cooling.c +6 -0
index 69f64474908a..649386a6ce7a 100644
@@ -21,6 +21,8 @@
 #include <linux/pm_opp.h>
 #include <linux/thermal.h>
 
+#include <trace/events/thermal.h>
+
 static int devfreq_cooling_get_max_state(struct thermal_cooling_device *cdev,
 					 unsigned long *state)
 {
@@ -142,6 +144,9 @@ static int devfreq_cooling_get_requested_power(struct thermal_cooling_device *cd
 	/* Get static power */
 	static_power = get_static_power(dfc, freq);
 
+	trace_thermal_power_devfreq_get_power(cdev, status, freq, dyn_power,
+					      static_power);
+
 	*power = dyn_power + static_power;
 
 	return 0;
@@ -195,6 +200,7 @@ static int devfreq_cooling_power2state(struct thermal_cooling_device *cdev,
 			break;
 
 	*state = i;
+	trace_thermal_power_devfreq_limit(cdev, freq, *state, power);
 	return 0;
 }
 
include/trace/events/thermal.h +53 -0

65 lines not on this page · raw message

Signature · 1 line
1.9.1