Skip to content

Commit f1f207e

Browse files
committed
of: Add cpu node iterator for_each_of_cpu_node()
Iterating thru cpu nodes is a common pattern. Create a common iterator which can find child nodes either by node name or device_type == cpu. Using the former will allow for eventually dropping device_type properties which are deprecated for FDT. Cc: Frank Rowand <frowand.list@gmail.com> Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be> Signed-off-by: Rob Herring <robh@kernel.org>
1 parent f6707fd commit f1f207e

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

drivers/of/base.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,45 @@ struct device_node *of_get_next_available_child(const struct device_node *node,
741741
}
742742
EXPORT_SYMBOL(of_get_next_available_child);
743743

744+
/**
745+
* of_get_next_cpu_node - Iterate on cpu nodes
746+
* @prev: previous child of the /cpus node, or NULL to get first
747+
*
748+
* Returns a cpu node pointer with refcount incremented, use of_node_put()
749+
* on it when done. Returns NULL when prev is the last child. Decrements
750+
* the refcount of prev.
751+
*/
752+
struct device_node *of_get_next_cpu_node(struct device_node *prev)
753+
{
754+
struct device_node *next = NULL;
755+
unsigned long flags;
756+
struct device_node *node;
757+
758+
if (!prev)
759+
node = of_find_node_by_path("/cpus");
760+
761+
raw_spin_lock_irqsave(&devtree_lock, flags);
762+
if (prev)
763+
next = prev->sibling;
764+
else if (node) {
765+
next = node->child;
766+
of_node_put(node);
767+
}
768+
for (; next; next = next->sibling) {
769+
if (!(of_node_name_eq(next, "cpu") ||
770+
(next->type && !of_node_cmp(next->type, "cpu"))))
771+
continue;
772+
if (!__of_device_is_available(next))
773+
continue;
774+
if (of_node_get(next))
775+
break;
776+
}
777+
of_node_put(prev);
778+
raw_spin_unlock_irqrestore(&devtree_lock, flags);
779+
return next;
780+
}
781+
EXPORT_SYMBOL(of_get_next_cpu_node);
782+
744783
/**
745784
* of_get_compatible_child - Find compatible child node
746785
* @parent: parent node

include/linux/of.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,8 @@ extern const void *of_get_property(const struct device_node *node,
353353
const char *name,
354354
int *lenp);
355355
extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread);
356+
extern struct device_node *of_get_next_cpu_node(struct device_node *prev);
357+
356358
#define for_each_property_of_node(dn, pp) \
357359
for (pp = dn->properties; pp != NULL; pp = pp->next)
358360

@@ -754,6 +756,11 @@ static inline struct device_node *of_get_cpu_node(int cpu,
754756
return NULL;
755757
}
756758

759+
static inline struct device_node *of_get_next_cpu_node(struct device_node *prev)
760+
{
761+
return NULL;
762+
}
763+
757764
static inline int of_n_addr_cells(struct device_node *np)
758765
{
759766
return 0;
@@ -1217,6 +1224,10 @@ static inline int of_property_read_s32(const struct device_node *np,
12171224
for (child = of_get_next_available_child(parent, NULL); child != NULL; \
12181225
child = of_get_next_available_child(parent, child))
12191226

1227+
#define for_each_of_cpu_node(cpu) \
1228+
for (cpu = of_get_next_cpu_node(NULL); cpu != NULL; \
1229+
cpu = of_get_next_cpu_node(cpu))
1230+
12201231
#define for_each_node_with_property(dn, prop_name) \
12211232
for (dn = of_find_node_with_property(NULL, prop_name); dn; \
12221233
dn = of_find_node_with_property(dn, prop_name))

0 commit comments

Comments
 (0)