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....@nxp.com>
Signed-off-by: Ye Li <ye...@nxp.com>
---
 drivers/cpu/cpu-uclass.c | 31 +++++++++++++++++++++++++++++++
 include/cpu.h            | 15 +++++++++++++++
 2 files changed, 46 insertions(+)

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
-- 
2.16.4

Reply via email to