[PATCH 5/6] memory: add helpers for iterating over memory regions

Ahmad Fatoum a.fatoum at pengutronix.de
Thu Dec 11 12:50:07 PST 2025


From: Ahmad Fatoum <a.fatoum at barebox.org>

Now that we have the resource_iter_* functions, add helpers for
conveniently traversing requested regions and the gaps between them.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 include/linux/ioport.h | 18 ++++++++++++++++++
 include/memory.h       |  6 ++++++
 2 files changed, 24 insertions(+)

diff --git a/include/linux/ioport.h b/include/linux/ioport.h
index 1b5dfdcb373d..1d8686d51e81 100644
--- a/include/linux/ioport.h
+++ b/include/linux/ioport.h
@@ -272,5 +272,23 @@ struct resource *resource_iter_last(struct resource *current, struct resource *g
 struct resource *resource_iter_prev(struct resource *current, struct resource *gap);
 struct resource *resource_iter_next(struct resource *current, struct resource *gap);
 
+/**
+ * for_each_resource_region - Iterate over child resources and gaps between them
+ * @parent: parent resource
+ * @region: pointer to child resource or gap
+ */
+#define for_each_resource_region(parent, region) \
+	for (struct resource gap, *region = resource_iter_first((parent), &gap); \
+	     region; region = resource_iter_next(region, &gap))
+
+/**
+ * for_each_resource_region_reverse - Reverse iterate over child resources and gaps between them
+ * @parent: parent resource
+ * @region: pointer to child resource or gap
+ */
+#define for_each_resource_region_reverse(parent, region) \
+	for (struct resource gap, *region = resource_iter_last((parent), &gap); \
+	     region; region = resource_iter_prev(region, &gap))
+
 #endif /* __ASSEMBLY__ */
 #endif	/* _LINUX_IOPORT_H */
diff --git a/include/memory.h b/include/memory.h
index dea76dd2b1f7..65614b97ec8b 100644
--- a/include/memory.h
+++ b/include/memory.h
@@ -30,6 +30,12 @@ int barebox_add_memory_bank(const char *name, resource_size_t start,
 	list_for_each_entry(rsv, &(mem)->res->children, sibling) \
 		if (is_reserved_resource(rsv))
 
+#define for_each_memory_bank_region(bank, region) \
+	for_each_resource_region((bank)->res, region)
+
+#define for_each_memory_bank_region_reverse(bank, region) \
+	for_each_resource_region_reverse((bank)->res, region)
+
 struct resource *__request_sdram_region(const char *name,
 					resource_size_t start, resource_size_t size);
 
-- 
2.47.3




More information about the barebox mailing list