Skip to contentSkip to conversation

cpuidle : use percpu cpuidle in the core code

PATCH In review Daniel Lezcano 2 participants 3 messages Apr 2, 2012 – May 30, 2012 CPU idle time management framework

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 cpuidle : use percpu cpuidle in the core code

The usual cpuidle initialization routines register the driver and then register a cpuidle device per cpu.

By default, most drivers initialize the device state count with the driver state count.

We can then add a new function 'cpuidle_register' where we register the driver and the devices. These devices can be defined in a global static variable in cpuidle.c. We will be able to factor out and remove a lot of duplicate lines of code.

As we still have some drivers, with different initialization routines, we keep 'cpuidle_register_driver' and 'cpuidle_register_device' as low level initialization routines to do some specific operations on the cpuidle devices.

---
 drivers/cpuidle/cpuidle.c |   42 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/cpuidle.h   |    3 +++
 2 files changed, 45 insertions(+), 0 deletions(-)
drivers/cpuidle/cpuidle.c +42 -0
index 87411ce..4d1f79b 100644
@@ -23,6 +23,7 @@
 #include "cpuidle.h"
 
 DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
+DEFINE_PER_CPU(struct cpuidle_device, cpuidle_device);
 
 DEFINE_MUTEX(cpuidle_lock);
 LIST_HEAD(cpuidle_detected_devices);
@@ -419,6 +420,47 @@ int cpuidle_register_device(struct cpuidle_device *dev)
 
 EXPORT_SYMBOL_GPL(cpuidle_register_device);
 
+/*
+ * cpuidle_register : register cpuidle driver and devices
+ * Note this function must be called after smp_init.
+ * @drv : the cpuidle driver
+ * Returns 0 on success, < 0 otherwise
+ */
+int cpuidle_register(struct cpuidle_driver *drv)
+{
+	int ret, cpu, i;
+	struct cpuidle_device *dev;
+
+	ret = cpuidle_register_driver(drv);
+	if (ret)
+		return ret;
+
+	for_each_online_cpu(cpu) {
+		dev = &per_cpu(cpuidle_device, cpu);
+		dev->cpu = cpu;
+
+		ret = cpuidle_register_device(dev);
+		if (ret)
+			goto out_unregister;
+	}
+
+out:
+	return ret;
+
+out_unregister:
+	for_each_online_cpu(i) {
+		if (i == cpu)
+			break;
+		dev = &per_cpu(cpuidle_device, i);
+		cpuidle_unregister_device(dev);
+	}
+
+	cpuidle_unregister_driver(drv);
+
+	goto out;
+}
+EXPORT_SYMBOL_GPL(cpuidle_register);
+
 /**
  * cpuidle_unregister_device - unregisters a CPU's idle PM feature
  * @dev: the cpu
include/linux/cpuidle.h +3 -0
index 6c26a3d..3475294 100644
@@ -135,6 +135,7 @@ struct cpuidle_driver {
 #ifdef CONFIG_CPU_IDLE
 extern void disable_cpuidle(void);
 extern int cpuidle_idle_call(void);
+extern int cpuidle_register(struct cpuidle_driver *drv);
 extern int cpuidle_register_driver(struct cpuidle_driver *drv);
 struct cpuidle_driver *cpuidle_get_driver(void);
 extern void cpuidle_unregister_driver(struct cpuidle_driver *drv);
@@ -154,6 +155,8 @@ extern int cpuidle_play_dead(void);
 #else
 static inline void disable_cpuidle(void) { }
 static inline int cpuidle_idle_call(void) { return -ENODEV; }
+static inline int cpuidle_register(struct cpuidle_driver *drv)
+{return -ENODEV; }
 static inline int cpuidle_register_driver(struct cpuidle_driver *drv)
 {return -ENODEV; }
 static inline struct cpuidle_driver *cpuidle_get_driver(void) {return NULL; }
Signature · 1 line
1.7.5.4
Acked-by on v3 1/1
#1 lore raw
Quoted 11 lines · Daniel Lezcano

The usual cpuidle initialization routines register the driver and then register a cpuidle device per cpu.

By default, most drivers initialize the device state count with the driver state count.

We can then add a new function 'cpuidle_register' where we register the driver and the devices. These devices can be defined in a global static variable in cpuidle.c. We will be able to factor out and remove a lot of duplicate lines of code.

Quoted 6 lines

As we still have some drivers, with different initialization routines, we keep 'cpuidle_register_driver' and 'cpuidle_register_device' as low level initialization routines to do some specific operations on the cpuidle devices.

Looks good to me now.

Regards,
Srivatsa S. Bhat

Quoted 86 lines
---
 drivers/cpuidle/cpuidle.c |   42 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/cpuidle.h   |    3 +++
 2 files changed, 45 insertions(+), 0 deletions(-)
drivers/cpuidle/cpuidle.c +42 -0
index 87411ce..4d1f79b 100644
@@ -23,6 +23,7 @@
 #include "cpuidle.h"
 
 DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
+DEFINE_PER_CPU(struct cpuidle_device, cpuidle_device);
 
 DEFINE_MUTEX(cpuidle_lock);
 LIST_HEAD(cpuidle_detected_devices);
@@ -419,6 +420,47 @@ int cpuidle_register_device(struct cpuidle_device *dev)
 
 EXPORT_SYMBOL_GPL(cpuidle_register_device);
 
+/*
+ * cpuidle_register : register cpuidle driver and devices
+ * Note this function must be called after smp_init.
+ * @drv : the cpuidle driver
+ * Returns 0 on success, < 0 otherwise
+ */
+int cpuidle_register(struct cpuidle_driver *drv)
+{
+	int ret, cpu, i;
+	struct cpuidle_device *dev;
+
+	ret = cpuidle_register_driver(drv);
+	if (ret)
+		return ret;
+
+	for_each_online_cpu(cpu) {
+		dev = &per_cpu(cpuidle_device, cpu);
+		dev->cpu = cpu;
+
+		ret = cpuidle_register_device(dev);
+		if (ret)
+			goto out_unregister;
+	}
+
+out:
+	return ret;
+
+out_unregister:
+	for_each_online_cpu(i) {
+		if (i == cpu)
+			break;
+		dev = &per_cpu(cpuidle_device, i);
+		cpuidle_unregister_device(dev);
+	}
+
+	cpuidle_unregister_driver(drv);
+
+	goto out;
+}
+EXPORT_SYMBOL_GPL(cpuidle_register);
+
 /**
  * cpuidle_unregister_device - unregisters a CPU's idle PM feature
  * @dev: the cpu
include/linux/cpuidle.h +3 -0
index 6c26a3d..3475294 100644
@@ -135,6 +135,7 @@ struct cpuidle_driver {
 #ifdef CONFIG_CPU_IDLE
 extern void disable_cpuidle(void);
 extern int cpuidle_idle_call(void);
+extern int cpuidle_register(struct cpuidle_driver *drv);
 extern int cpuidle_register_driver(struct cpuidle_driver *drv);
 struct cpuidle_driver *cpuidle_get_driver(void);
 extern void cpuidle_unregister_driver(struct cpuidle_driver *drv);
@@ -154,6 +155,8 @@ extern int cpuidle_play_dead(void);
 #else
 static inline void disable_cpuidle(void) { }
 static inline int cpuidle_idle_call(void) { return -ENODEV; }
+static inline int cpuidle_register(struct cpuidle_driver *drv)
+{return -ENODEV; }
 static inline int cpuidle_register_driver(struct cpuidle_driver *drv)
 {return -ENODEV; }
 static inline struct cpuidle_driver *cpuidle_get_driver(void) {return NULL; }
#2 lore raw
Quoted 18 lines · Srivatsa S. Bhat
Quoted 16 lines · Daniel Lezcano

The usual cpuidle initialization routines register the driver and then register a cpuidle device per cpu.

By default, most drivers initialize the device state count with the driver state count.

We can then add a new function 'cpuidle_register' where we register the driver and the devices. These devices can be defined in a global static variable in cpuidle.c. We will be able to factor out and remove a lot of duplicate lines of code.

As we still have some drivers, with different initialization routines, we keep 'cpuidle_register_driver' and 'cpuidle_register_device' as low level initialization routines to do some specific operations on the cpuidle devices.

Quoted 5 lines
Quoted 1 line

Looks good to me now.

Hi Len,

could you consider this patch for merging ?

Thanks
-- Daniel

Quoted 93 lines

Regards,
Srivatsa S. Bhat

Quoted 86 lines
---
 drivers/cpuidle/cpuidle.c |   42 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/cpuidle.h   |    3 +++
 2 files changed, 45 insertions(+), 0 deletions(-)
drivers/cpuidle/cpuidle.c +42 -0
index 87411ce..4d1f79b 100644
@@ -23,6 +23,7 @@
 #include "cpuidle.h"
 
 DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
+DEFINE_PER_CPU(struct cpuidle_device, cpuidle_device);
 
 DEFINE_MUTEX(cpuidle_lock);
 LIST_HEAD(cpuidle_detected_devices);
@@ -419,6 +420,47 @@ int cpuidle_register_device(struct cpuidle_device *dev)
 
 EXPORT_SYMBOL_GPL(cpuidle_register_device);
 
+/*
+ * cpuidle_register : register cpuidle driver and devices
+ * Note this function must be called after smp_init.
+ * @drv : the cpuidle driver
+ * Returns 0 on success, < 0 otherwise
+ */
+int cpuidle_register(struct cpuidle_driver *drv)
+{
+	int ret, cpu, i;
+	struct cpuidle_device *dev;
+
+	ret = cpuidle_register_driver(drv);
+	if (ret)
+		return ret;
+
+	for_each_online_cpu(cpu) {
+		dev = &per_cpu(cpuidle_device, cpu);
+		dev->cpu = cpu;
+
+		ret = cpuidle_register_device(dev);
+		if (ret)
+			goto out_unregister;
+	}
+
+out:
+	return ret;
+
+out_unregister:
+	for_each_online_cpu(i) {
+		if (i == cpu)
+			break;
+		dev = &per_cpu(cpuidle_device, i);
+		cpuidle_unregister_device(dev);
+	}
+
+	cpuidle_unregister_driver(drv);
+
+	goto out;
+}
+EXPORT_SYMBOL_GPL(cpuidle_register);
+
 /**
  * cpuidle_unregister_device - unregisters a CPU's idle PM feature
  * @dev: the cpu
include/linux/cpuidle.h +3 -0
index 6c26a3d..3475294 100644
@@ -135,6 +135,7 @@ struct cpuidle_driver {
 #ifdef CONFIG_CPU_IDLE
 extern void disable_cpuidle(void);
 extern int cpuidle_idle_call(void);
+extern int cpuidle_register(struct cpuidle_driver *drv);
 extern int cpuidle_register_driver(struct cpuidle_driver *drv);
 struct cpuidle_driver *cpuidle_get_driver(void);
 extern void cpuidle_unregister_driver(struct cpuidle_driver *drv);
@@ -154,6 +155,8 @@ extern int cpuidle_play_dead(void);
 #else
 static inline void disable_cpuidle(void) { }
 static inline int cpuidle_idle_call(void) { return -ENODEV; }
+static inline int cpuidle_register(struct cpuidle_driver *drv)
+{return -ENODEV; }
 static inline int cpuidle_register_driver(struct cpuidle_driver *drv)
 {return -ENODEV; }
 static inline struct cpuidle_driver *cpuidle_get_driver(void) {return NULL; }
Signature · 10 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

_______________________________________________
linux-pm mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-pm