diff mbox series

[1/7] uclass: cpu: Add new API to get udevice for current CPU

Message ID 20200429021720.6653-1-peng.fan@nxp.com
State New
Headers show
Series [1/7] uclass: cpu: Add new API to get udevice for current CPU | expand

Commit Message

Peng Fan April 29, 2020, 2:17 a.m. UTC
When running on SoC with multiple clusters, the boot CPU may
not be fixed, saying booting from cluster A or cluster B.
Add a API that can return the udevice for current boot CPU.
Cpu driver needs to implement is_current_cpu interface for this
feature, otherwise the API only returns the first udevice in
cpu uclass.

Signed-off-by: Peng Fan <peng.fan at nxp.com>
Signed-off-by: Ye Li <ye.li at nxp.com>
---
 drivers/cpu/cpu-uclass.c | 31 +++++++++++++++++++++++++++++++
 include/cpu.h            | 15 +++++++++++++++
 2 files changed, 46 insertions(+)

Comments

Simon Glass April 29, 2020, 6:03 p.m. UTC | #1
Hi Peng,

On Tue, 28 Apr 2020 at 19:54, Peng Fan <peng.fan at nxp.com> wrote:
>
> When running on SoC with multiple clusters, the boot CPU may
> not be fixed, saying booting from cluster A or cluster B.
> Add a API that can return the udevice for current boot CPU.
> Cpu driver needs to implement is_current_cpu interface for this
> feature, otherwise the API only returns the first udevice in
> cpu uclass.
>
> Signed-off-by: Peng Fan <peng.fan at nxp.com>
> Signed-off-by: Ye Li <ye.li at nxp.com>
> ---
>  drivers/cpu/cpu-uclass.c | 31 +++++++++++++++++++++++++++++++
>  include/cpu.h            | 15 +++++++++++++++
>  2 files changed, 46 insertions(+)
>

This looks reasonable but please add it to the test and I have some
comments below.


> diff --git a/drivers/cpu/cpu-uclass.c b/drivers/cpu/cpu-uclass.c
> index 457f77b7c8..488b1f5050 100644
> --- a/drivers/cpu/cpu-uclass.c
> +++ b/drivers/cpu/cpu-uclass.c
> @@ -10,6 +10,7 @@
>  #include <errno.h>
>  #include <dm/lists.h>
>  #include <dm/root.h>
> +#include <linux/err.h>
>
>  int cpu_probe_all(void)
>  {
> @@ -34,6 +35,36 @@ int cpu_probe_all(void)
>         return 0;
>  }
>
> +struct udevice *cpu_get_current_dev(void)
> +{
> +       struct udevice *cpu;
> +       struct uclass *uc;
> +       struct cpu_ops *ops;
> +       int ret;
> +
> +       ret = uclass_get(UCLASS_CPU, &uc);
> +       if (ret)
> +               return ERR_PTR(ret);
> +
> +       uclass_foreach_dev(cpu, uc) {

Do you want to probe the CPU? See uclass_foreach_dev_probe() if you do.

> +               ops = cpu_get_ops(cpu);
> +               if (ops->is_current_cpu) {

Can you please create a cpu_is_current() function which just does the
ops call and needs to handle -ENOSYS. THen call it here.

> +                       if (ops->is_current_cpu(cpu))
> +                               return cpu;
> +               }
> +       }
> +
> +       /* If can't find current cpu device, use the first dev instead */
> +       ret = uclass_first_device_err(UCLASS_CPU, &cpu);
> +       if (ret) {
> +               debug("%s: Could not get CPU device (err = %d)\n",
> +                     __func__, ret);
> +               return NULL;
> +       }
> +
> +       return cpu;
> +}
> +
>  int cpu_get_desc(struct udevice *dev, char *buf, int size)
>  {
>         struct cpu_ops *ops = cpu_get_ops(dev);
> diff --git a/include/cpu.h b/include/cpu.h
> index 6b1b6b37b3..7ffe412b18 100644
> --- a/include/cpu.h
> +++ b/include/cpu.h
> @@ -89,6 +89,14 @@ struct cpu_ops {
>          * @return 0 if OK, -ENOSPC if buffer is too small, other -ve on error
>          */
>         int (*get_vendor)(struct udevice *dev, char *buf, int size);
> +
> +       /**
> +        * is_current_cpu() - Check if the device is for current CPU
> +        *

Can you expand that a little bit, and below? It is the CPU that U-Boot
is currently running from, right?

> +        * @dev:        Device to check (UCLASS_CPU)
> +        * @return true if the device is current CPU, false if the device is not.
> +        */
> +       bool (*is_current_cpu)(struct udevice *dev);

is_current() since it is always a cpu.

Also please use int instead of bool as the return value, since we need
to support an error, when we cannot tell, for example.

>  };
>
>  #define cpu_get_ops(dev)        ((struct cpu_ops *)(dev)->driver->ops)
> @@ -137,4 +145,11 @@ int cpu_get_vendor(struct udevice *dev, char *buf, int size);
>   */
>  int cpu_probe_all(void);
>
> +/**
> + * cpu_get_current_dev() - Get CPU udevice for current CPU
> + *
> + * Return: udevice if OK, - NULL on error
> + */
> +struct udevice *cpu_get_current_dev(void);
> +
>  #endif
> --
> 2.16.4

Regards,
Simon
diff mbox series

Patch

diff --git a/drivers/cpu/cpu-uclass.c b/drivers/cpu/cpu-uclass.c
index 457f77b7c8..488b1f5050 100644
--- a/drivers/cpu/cpu-uclass.c
+++ b/drivers/cpu/cpu-uclass.c
@@ -10,6 +10,7 @@ 
 #include <errno.h>
 #include <dm/lists.h>
 #include <dm/root.h>
+#include <linux/err.h>
 
 int cpu_probe_all(void)
 {
@@ -34,6 +35,36 @@  int cpu_probe_all(void)
 	return 0;
 }
 
+struct udevice *cpu_get_current_dev(void)
+{
+	struct udevice *cpu;
+	struct uclass *uc;
+	struct cpu_ops *ops;
+	int ret;
+
+	ret = uclass_get(UCLASS_CPU, &uc);
+	if (ret)
+		return ERR_PTR(ret);
+
+	uclass_foreach_dev(cpu, uc) {
+		ops = cpu_get_ops(cpu);
+		if (ops->is_current_cpu) {
+			if (ops->is_current_cpu(cpu))
+				return cpu;
+		}
+	}
+
+	/* If can't find current cpu device, use the first dev instead */
+	ret = uclass_first_device_err(UCLASS_CPU, &cpu);
+	if (ret) {
+		debug("%s: Could not get CPU device (err = %d)\n",
+		      __func__, ret);
+		return NULL;
+	}
+
+	return cpu;
+}
+
 int cpu_get_desc(struct udevice *dev, char *buf, int size)
 {
 	struct cpu_ops *ops = cpu_get_ops(dev);
diff --git a/include/cpu.h b/include/cpu.h
index 6b1b6b37b3..7ffe412b18 100644
--- a/include/cpu.h
+++ b/include/cpu.h
@@ -89,6 +89,14 @@  struct cpu_ops {
 	 * @return 0 if OK, -ENOSPC if buffer is too small, other -ve on error
 	 */
 	int (*get_vendor)(struct udevice *dev, char *buf, int size);
+
+	/**
+	 * is_current_cpu() - Check if the device is for current CPU
+	 *
+	 * @dev:	Device to check (UCLASS_CPU)
+	 * @return true if the device is current CPU, false if the device is not.
+	 */
+	bool (*is_current_cpu)(struct udevice *dev);
 };
 
 #define cpu_get_ops(dev)        ((struct cpu_ops *)(dev)->driver->ops)
@@ -137,4 +145,11 @@  int cpu_get_vendor(struct udevice *dev, char *buf, int size);
  */
 int cpu_probe_all(void);
 
+/**
+ * cpu_get_current_dev() - Get CPU udevice for current CPU
+ *
+ * Return: udevice if OK, - NULL on error
+ */
+struct udevice *cpu_get_current_dev(void);
+
 #endif