This series refactor the way CPU IDs are retrieved from the device tree.
Usually, there is a for loop that goes over every single CPU that can be avoided. This also reduces the amount of NULL pointer checks in drivers. I have abstracted away that loop and introduced a new function (of_cpu_node_to_id) for this.
This patchset is a subset of [1], where I removed content and patches relevant to hyper-threaded cores for DT. Based on the discussion, the code refactor is still useful, hence this patchset.
[1] https://lore.kernel.org/all/20250512080715.82-1-alireza.sanaee@huawei.co
Changes since v2: - Addressed Jonathan Cameron's comments. - Added reviewed-by tags. - Added a new commit where I suggest simplfing the code in arch_topology.c to remove the use of cpu_node on failure path. - Improve documentation. - Caught a bug in patch 1. - Commit message fixed for patch 2.
Changes since v1: - Rebased on top of the latest mainline. - Addressed Krzysztof Kozlowski's comments -- Hopefully :-) - Addressed Jonathan Cameron's comments.
Alireza Sanaee (6): of: add infra for finding CPU id from phandle arch_topology: drop the use of cpu_node in the pr_info arch_topology: update CPU map to use of_cpu_phandle_to_id coresight: cti: Use of_cpu_phandle_to_id for grabbing CPU id coresight: Use of_cpu_phandle_to_id for grabbing CPU ID perf/arm-dsu: refactor cpu id retrieval via new API of_cpu_phandle_to_id
drivers/base/arch_topology.c | 22 ++++----- .../coresight/coresight-cti-platform.c | 13 +---- .../hwtracing/coresight/coresight-platform.c | 15 +----- drivers/of/cpu.c | 49 +++++++++++++++++++ drivers/perf/arm_dsu_pmu.c | 7 +-- include/linux/of.h | 9 ++++ 6 files changed, 72 insertions(+), 43 deletions(-)
Get CPU ID from phandle. Some drivers such as coresight, cti-coresight, or arm-dsu use this API for finding CPU node in DT. In particular, drivers do this by getting the CPU device_node through a phandle and then find the CPU ID using of_cpu_node_to_id(). This commit encapsulates CPU node finding and improves readability.
The API interface takes three parameters, 1) node, 2) pointer to pointer of CPU node, 3) CPU node index. API sets the pointer to the CPU node and allows the driver to work with the CPU node, for logging purposes for instance.
Signed-off-by: Alireza Sanaee alireza.sanaee@huawei.com --- drivers/of/cpu.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++ include/linux/of.h | 9 +++++++++ 2 files changed, 58 insertions(+)
diff --git a/drivers/of/cpu.c b/drivers/of/cpu.c index 5214dc3d05ae..86e4e24c6030 100644 --- a/drivers/of/cpu.c +++ b/drivers/of/cpu.c @@ -173,6 +173,55 @@ int of_cpu_node_to_id(struct device_node *cpu_node) } EXPORT_SYMBOL(of_cpu_node_to_id);
+/** + * of_cpu_phandle_to_id: Get the logical CPU number for a given device_node + * + * @node: Pointer to the device_node containing CPU phandle. + * @cpu_np: Pointer to the device_node for CPU. + * @cpu_idx: The index of the CPU in the list of CPUs. + * + * *cpu_np is only set when cpu_np is not NULL. Use of_node_put() to release + * the node reference if cpu_np != NULL and this function succeeds. + * + * Return: This function has three different possible return situations: + * 1) returns -EINVAL if the node is NULL. + * 2) returns -ENODEV if the CPU node is not found or of_cpu_node_to_id is + * not successful. + * 3) returns the logical CPU number if the CPU node is found. + */ + +int of_cpu_phandle_to_id(const struct device_node *node, + struct device_node **cpu_np, + uint8_t cpu_idx) +{ + struct device_node *local_cpu_node = NULL; + int cpu; + + if (!node) + return -EINVAL; + + if (cpu_idx == 0) + local_cpu_node = of_parse_phandle(node, "cpu", 0); + if (!local_cpu_node) + local_cpu_node = of_parse_phandle(node, "cpus", cpu_idx); + if (!local_cpu_node) + return -ENODEV; + + cpu = of_cpu_node_to_id(local_cpu_node); + if (cpu < 0) { + of_node_put(local_cpu_node); + return cpu; + } + + if (cpu_np) + *cpu_np = local_cpu_node; + else + of_node_put(local_cpu_node); + + return cpu; +} +EXPORT_SYMBOL(of_cpu_phandle_to_id); + /** * of_get_cpu_state_node - Get CPU's idle state node at the given index * diff --git a/include/linux/of.h b/include/linux/of.h index a62154aeda1b..717e55065d99 100644 --- a/include/linux/of.h +++ b/include/linux/of.h @@ -365,6 +365,8 @@ extern const void *of_get_property(const struct device_node *node, extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread); extern struct device_node *of_cpu_device_node_get(int cpu); extern int of_cpu_node_to_id(struct device_node *np); +extern int of_cpu_phandle_to_id(const struct device_node *np, + struct device_node **cpu_np, uint8_t cpu_idx); extern struct device_node *of_get_next_cpu_node(struct device_node *prev); extern struct device_node *of_get_cpu_state_node(const struct device_node *cpu_node, int index); @@ -680,6 +682,13 @@ static inline int of_cpu_node_to_id(struct device_node *np) return -ENODEV; }
+static inline int of_cpu_phandle_to_id(const struct device_node *np, + struct device_node **cpu_np, + uint8_t cpu_idx) +{ + return -ENODEV; +} + static inline struct device_node *of_get_next_cpu_node(struct device_node *prev) { return NULL;
Remove the use of cpu_node in the pr_info. When of_cpu_node_to_id fails, it may set a pointer, cpu_node, and the get_cpu_for_node function uses that pointer to log further in the fail scenario.
Also, change the structure to exit early in fail scenarios which will help enabling code unification that follows in this series.
Signed-off-by: Alireza Sanaee alireza.sanaee@huawei.com --- drivers/base/arch_topology.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/drivers/base/arch_topology.c b/drivers/base/arch_topology.c index 1037169abb45..6fafd86f608a 100644 --- a/drivers/base/arch_topology.c +++ b/drivers/base/arch_topology.c @@ -481,12 +481,13 @@ static int __init get_cpu_for_node(struct device_node *node) return -1;
cpu = of_cpu_node_to_id(cpu_node); - if (cpu >= 0) - topology_parse_cpu_capacity(cpu_node, cpu); - else - pr_info("CPU node for %pOF exist but the possible cpu range is :%*pbl\n", - cpu_node, cpumask_pr_args(cpu_possible_mask)); + if (cpu < 0) { + pr_info("CPU node exist but the possible cpu range is :%*pbl\n", + cpumask_pr_args(cpu_possible_mask)); + return cpu; + }
+ topology_parse_cpu_capacity(cpu_node, cpu); return cpu; }
Refactor get_cpu_for_node to use of_cpu_phandle_to_id instead of of_cpu_node_to_id.
Signed-off-by: Alireza Sanaee alireza.sanaee@huawei.com --- drivers/base/arch_topology.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
diff --git a/drivers/base/arch_topology.c b/drivers/base/arch_topology.c index 6fafd86f608a..72bf23cdf469 100644 --- a/drivers/base/arch_topology.c +++ b/drivers/base/arch_topology.c @@ -474,20 +474,19 @@ static unsigned int max_smt_thread_num = 1; static int __init get_cpu_for_node(struct device_node *node) { int cpu; - struct device_node *cpu_node __free(device_node) = - of_parse_phandle(node, "cpu", 0); + struct device_node *cpu_node = NULL;
- if (!cpu_node) - return -1; - - cpu = of_cpu_node_to_id(cpu_node); - if (cpu < 0) { + cpu = of_cpu_phandle_to_id(node, &cpu_node, 0); + if (cpu == -ENODEV) { pr_info("CPU node exist but the possible cpu range is :%*pbl\n", cpumask_pr_args(cpu_possible_mask)); return cpu; + } else if (cpu < 0) { + return -1; }
topology_parse_cpu_capacity(cpu_node, cpu); + of_node_put(cpu_node); return cpu; }
Use the newly created API (of_cpu_phandle_to_id) to grab CPU ID.
Reviewed-by: Mike Leach mike.leach@linaro.org Signed-off-by: Alireza Sanaee alireza.sanaee@huawei.com --- .../hwtracing/coresight/coresight-cti-platform.c | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-)
diff --git a/drivers/hwtracing/coresight/coresight-cti-platform.c b/drivers/hwtracing/coresight/coresight-cti-platform.c index d0ae10bf6128..2629962dbe3e 100644 --- a/drivers/hwtracing/coresight/coresight-cti-platform.c +++ b/drivers/hwtracing/coresight/coresight-cti-platform.c @@ -41,18 +41,7 @@ */ static int of_cti_get_cpu_at_node(const struct device_node *node) { - int cpu; - struct device_node *dn; - - if (node == NULL) - return -1; - - dn = of_parse_phandle(node, "cpu", 0); - /* CTI affinity defaults to no cpu */ - if (!dn) - return -1; - cpu = of_cpu_node_to_id(dn); - of_node_put(dn); + int cpu = of_cpu_phandle_to_id(node, NULL, 0);
/* No Affinity if no cpu nodes are found */ return (cpu < 0) ? -1 : cpu;
Use the newly created API (of_cpu_phandle_to_id) to grab CPU ID.
Reviewed-by: Jonathan Cameron jonathan.cameron@huawei.com Signed-off-by: Alireza Sanaee alireza.sanaee@huawei.com --- drivers/hwtracing/coresight/coresight-platform.c | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-)
diff --git a/drivers/hwtracing/coresight/coresight-platform.c b/drivers/hwtracing/coresight/coresight-platform.c index 0db64c5f4995..95d46ea08936 100644 --- a/drivers/hwtracing/coresight/coresight-platform.c +++ b/drivers/hwtracing/coresight/coresight-platform.c @@ -167,20 +167,7 @@ of_coresight_get_output_ports_node(const struct device_node *node)
static int of_coresight_get_cpu(struct device *dev) { - int cpu; - struct device_node *dn; - - if (!dev->of_node) - return -ENODEV; - - dn = of_parse_phandle(dev->of_node, "cpu", 0); - if (!dn) - return -ENODEV; - - cpu = of_cpu_node_to_id(dn); - of_node_put(dn); - - return cpu; + return of_cpu_phandle_to_id(dev->of_node, NULL, 0); }
/*
On Fri, 18 Jul 2025 at 10:51, Alireza Sanaee alireza.sanaee@huawei.com wrote:
Use the newly created API (of_cpu_phandle_to_id) to grab CPU ID.
Reviewed-by: Jonathan Cameron jonathan.cameron@huawei.com Signed-off-by: Alireza Sanaee alireza.sanaee@huawei.com
drivers/hwtracing/coresight/coresight-platform.c | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-)
diff --git a/drivers/hwtracing/coresight/coresight-platform.c b/drivers/hwtracing/coresight/coresight-platform.c index 0db64c5f4995..95d46ea08936 100644 --- a/drivers/hwtracing/coresight/coresight-platform.c +++ b/drivers/hwtracing/coresight/coresight-platform.c @@ -167,20 +167,7 @@ of_coresight_get_output_ports_node(const struct device_node *node)
static int of_coresight_get_cpu(struct device *dev) {
int cpu;
struct device_node *dn;
if (!dev->of_node)
return -ENODEV;
dn = of_parse_phandle(dev->of_node, "cpu", 0);
if (!dn)
return -ENODEV;
cpu = of_cpu_node_to_id(dn);
of_node_put(dn);
return cpu;
return of_cpu_phandle_to_id(dev->of_node, NULL, 0);
}
/*
2.43.0
Reviewed-by: Mike Leach mike.leach@linaro.org
-- Mike Leach Principal Engineer, ARM Ltd. Manchester Design Centre. UK
Update arm-dsu to use the new API (of_cpu_phandle_to_id).
Signed-off-by: Alireza Sanaee alireza.sanaee@huawei.com --- drivers/perf/arm_dsu_pmu.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)
diff --git a/drivers/perf/arm_dsu_pmu.c b/drivers/perf/arm_dsu_pmu.c index cb4fb59fe04b..8f76bd94349c 100644 --- a/drivers/perf/arm_dsu_pmu.c +++ b/drivers/perf/arm_dsu_pmu.c @@ -591,17 +591,12 @@ static struct dsu_pmu *dsu_pmu_alloc(struct platform_device *pdev) static int dsu_pmu_dt_get_cpus(struct device *dev, cpumask_t *mask) { int i = 0, n, cpu; - struct device_node *cpu_node;
n = of_count_phandle_with_args(dev->of_node, "cpus", NULL); if (n <= 0) return -ENODEV; for (; i < n; i++) { - cpu_node = of_parse_phandle(dev->of_node, "cpus", i); - if (!cpu_node) - break; - cpu = of_cpu_node_to_id(cpu_node); - of_node_put(cpu_node); + cpu = of_cpu_phandle_to_id(dev->of_node, NULL, i); /* * We have to ignore the failures here and continue scanning * the list to handle cases where the nr_cpus could be capped