[PATCH v2 1/2] cpuidle: Add interface to get cpuidle_driver by CPU ID

Yaxiong Tian posted 2 patches 1 month, 3 weeks ago
[PATCH v2 1/2] cpuidle: Add interface to get cpuidle_driver by CPU ID
Posted by Yaxiong Tian 1 month, 3 weeks ago
Some drivers need to obtain the cpuidle_driver via CPU ID before the
cpuidle_device is registered. Therefore, this interface is added.

Tested-by: Shaobo Huang <huangshaobo2075@phytium.com.cn>
Signed-off-by: Yaxiong Tian <tianyaxiong@kylinos.cn>
Signed-off-by: Shaobo Huang <huangshaobo2075@phytium.com.cn>
Signed-off-by: Yinfeng Wang <wangyinfeng@phytium.com.cn>
Signed-off-by: Xu Wang<wangxu@phytium.com.cn>
---
 drivers/cpuidle/driver.c | 16 ++++++++++++++++
 include/linux/cpuidle.h  |  4 ++++
 2 files changed, 20 insertions(+)

diff --git a/drivers/cpuidle/driver.c b/drivers/cpuidle/driver.c
index 9bbfa594c442..8647f8165863 100644
--- a/drivers/cpuidle/driver.c
+++ b/drivers/cpuidle/driver.c
@@ -353,6 +353,22 @@ struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev)
 }
 EXPORT_SYMBOL_GPL(cpuidle_get_cpu_driver);
 
+/**
+ * cpuidle_get_cpu_driver_by_cpu - return the driver registered for a CPU.
+ * @cpu_num: a valid cpu num
+ *
+ * Returns a struct cpuidle_driver pointer, or NULL if no driver is registered
+ * for the CPU associated with @cpu.
+ */
+struct cpuidle_driver *cpuidle_get_cpu_driver_by_cpu(int cpu_num)
+{
+	if (cpu_num < 0 || cpu_num >= nr_cpu_ids)
+		return NULL;
+
+	return __cpuidle_get_cpu_driver(cpu_num);
+}
+EXPORT_SYMBOL_GPL(cpuidle_get_cpu_driver_by_cpu);
+
 /**
  * cpuidle_driver_state_disabled - Disable or enable an idle state
  * @drv: cpuidle driver owning the state
diff --git a/include/linux/cpuidle.h b/include/linux/cpuidle.h
index a9ee4fe55dcf..aebbaae6a501 100644
--- a/include/linux/cpuidle.h
+++ b/include/linux/cpuidle.h
@@ -200,6 +200,7 @@ extern void cpuidle_disable_device(struct cpuidle_device *dev);
 extern int cpuidle_play_dead(void);
 
 extern struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev);
+extern struct cpuidle_driver *cpuidle_get_cpu_driver_by_cpu(int cpu_num);
 static inline struct cpuidle_device *cpuidle_get_device(void)
 {return __this_cpu_read(cpuidle_devices); }
 #else
@@ -240,6 +241,9 @@ static inline void cpuidle_disable_device(struct cpuidle_device *dev) { }
 static inline int cpuidle_play_dead(void) {return -ENODEV; }
 static inline struct cpuidle_driver *cpuidle_get_cpu_driver(
 	struct cpuidle_device *dev) {return NULL; }
+
+static inline struct cpuidle_driver *cpuidle_get_cpu_driver_by_cpu(
+	int cpu_num) {return NULL; };
 static inline struct cpuidle_device *cpuidle_get_device(void) {return NULL; }
 #endif
 
-- 
2.25.1
Re: [PATCH v2 1/2] cpuidle: Add interface to get cpuidle_driver by CPU ID
Posted by Rafael J. Wysocki 1 month, 1 week ago
On Thu, Aug 14, 2025 at 9:32 AM Yaxiong Tian <tianyaxiong@kylinos.cn> wrote:
>
> Some drivers need to obtain the cpuidle_driver via CPU ID before the
> cpuidle_device is registered. Therefore, this interface is added.
>
> Tested-by: Shaobo Huang <huangshaobo2075@phytium.com.cn>
> Signed-off-by: Yaxiong Tian <tianyaxiong@kylinos.cn>
> Signed-off-by: Shaobo Huang <huangshaobo2075@phytium.com.cn>
> Signed-off-by: Yinfeng Wang <wangyinfeng@phytium.com.cn>
> Signed-off-by: Xu Wang<wangxu@phytium.com.cn>
> ---
>  drivers/cpuidle/driver.c | 16 ++++++++++++++++
>  include/linux/cpuidle.h  |  4 ++++
>  2 files changed, 20 insertions(+)
>
> diff --git a/drivers/cpuidle/driver.c b/drivers/cpuidle/driver.c
> index 9bbfa594c442..8647f8165863 100644
> --- a/drivers/cpuidle/driver.c
> +++ b/drivers/cpuidle/driver.c
> @@ -353,6 +353,22 @@ struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev)
>  }
>  EXPORT_SYMBOL_GPL(cpuidle_get_cpu_driver);
>
> +/**
> + * cpuidle_get_cpu_driver_by_cpu - return the driver registered for a CPU.
> + * @cpu_num: a valid cpu num
> + *
> + * Returns a struct cpuidle_driver pointer, or NULL if no driver is registered
> + * for the CPU associated with @cpu.
> + */
> +struct cpuidle_driver *cpuidle_get_cpu_driver_by_cpu(int cpu_num)
> +{
> +       if (cpu_num < 0 || cpu_num >= nr_cpu_ids)
> +               return NULL;

What about changing the __cpuidle_get_cpu_driver() parameter type to
unsigned int and also passing unsigned int to this function?  The
first check above can go away then.

> +
> +       return __cpuidle_get_cpu_driver(cpu_num);
> +}
> +EXPORT_SYMBOL_GPL(cpuidle_get_cpu_driver_by_cpu);
> +
>  /**
>   * cpuidle_driver_state_disabled - Disable or enable an idle state
>   * @drv: cpuidle driver owning the state
> diff --git a/include/linux/cpuidle.h b/include/linux/cpuidle.h
> index a9ee4fe55dcf..aebbaae6a501 100644
> --- a/include/linux/cpuidle.h
> +++ b/include/linux/cpuidle.h
> @@ -200,6 +200,7 @@ extern void cpuidle_disable_device(struct cpuidle_device *dev);
>  extern int cpuidle_play_dead(void);
>
>  extern struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev);
> +extern struct cpuidle_driver *cpuidle_get_cpu_driver_by_cpu(int cpu_num);
>  static inline struct cpuidle_device *cpuidle_get_device(void)
>  {return __this_cpu_read(cpuidle_devices); }
>  #else
> @@ -240,6 +241,9 @@ static inline void cpuidle_disable_device(struct cpuidle_device *dev) { }
>  static inline int cpuidle_play_dead(void) {return -ENODEV; }
>  static inline struct cpuidle_driver *cpuidle_get_cpu_driver(
>         struct cpuidle_device *dev) {return NULL; }
> +
> +static inline struct cpuidle_driver *cpuidle_get_cpu_driver_by_cpu(
> +       int cpu_num) {return NULL; };
>  static inline struct cpuidle_device *cpuidle_get_device(void) {return NULL; }
>  #endif
>
> --
> 2.25.1
>