From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 1/6] resource: implement resource walker
Date: Thu, 11 Dec 2025 21:50:03 +0100 [thread overview]
Message-ID: <20251211205240.2836186-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20251211205240.2836186-1-a.fatoum@pengutronix.de>
We currently keep track of regions with the SDRAM by requesting a struct
resource from the memory bank. We will make additional use of that to
request memory handed out to EFI applications when barebox is being used
as EFI loader.
For that purpose, we need an easy way to walk the requested memory
region and especially the gaps between them.
Add a new resource_iter API for exactly this purpose.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
common/resource.c | 112 +++++++++++++++++++++++++++++++++++++++++
include/linux/ioport.h | 7 +++
2 files changed, 119 insertions(+)
diff --git a/common/resource.c b/common/resource.c
index 152f5a502a1e..c8623b9c40a6 100644
--- a/common/resource.c
+++ b/common/resource.c
@@ -169,6 +169,118 @@ struct resource *request_ioport_region(const char *name,
return res;
}
+static struct resource *
+resource_iter_gap(struct resource *parent, struct resource *before,
+ struct resource *gap, struct resource *after)
+{
+ *gap = (struct resource) {};
+
+ gap->attrs = parent->attrs;
+ gap->type = parent->type;
+ gap->flags = IORESOURCE_UNSET | parent->flags;
+ gap->parent = parent;
+ INIT_LIST_HEAD(&gap->children);
+
+ if (before) {
+ gap->start = before->end + 1;
+ gap->sibling.prev = &before->sibling;
+ } else {
+ gap->start = parent->start;
+ gap->sibling.prev = &parent->children;
+ }
+
+ if (after) {
+ gap->end = after->start - 1;
+ gap->sibling.next = &after->sibling;
+ } else {
+ gap->end = parent->end;
+ gap->sibling.next = &parent->children;
+ }
+
+ return gap;
+}
+
+struct resource *
+resource_iter_first(struct resource *parent, struct resource *gap)
+{
+ struct resource *child;
+
+ if (!parent || region_is_gap(parent))
+ return NULL;
+
+ child = list_first_entry_or_null(&parent->children,
+ struct resource, sibling);
+ if (!gap || (child && parent->start == child->start))
+ return child;
+
+ return resource_iter_gap(parent, NULL, gap, child);
+}
+
+struct resource *
+resource_iter_last(struct resource *parent, struct resource *gap)
+{
+ struct resource *child;
+
+ if (!parent || region_is_gap(parent))
+ return NULL;
+
+ child = list_last_entry_or_null(&parent->children,
+ struct resource, sibling);
+ if (!gap || (child && parent->end == child->end))
+ return child;
+
+ return resource_iter_gap(parent, child, gap, NULL);
+}
+
+struct resource *
+resource_iter_next(struct resource *current, struct resource *gap)
+{
+ struct resource *parent, *cursor, *next = NULL;
+
+ if (!current || !current->parent)
+ return NULL;
+
+ parent = current->parent;
+ if (current->end == parent->end)
+ return NULL;
+
+ cursor = current;
+ list_for_each_entry_continue(cursor, &parent->children, sibling) {
+ next = cursor;
+ break;
+ }
+
+ if (!gap || (next && resource_adjacent(current, next)))
+ return next;
+
+ return resource_iter_gap(parent, current, gap, next);
+}
+
+struct resource *
+resource_iter_prev(struct resource *current,
+ struct resource *gap)
+{
+ struct resource *parent, *cursor, *prev = NULL;
+
+ if (!current || !current->parent)
+ return NULL;
+
+ parent = current->parent;
+ if (current->start == parent->start)
+ return NULL;
+
+ cursor = current;
+ list_for_each_entry_continue_reverse(cursor, &parent->children, sibling) {
+ prev = cursor;
+ break;
+ }
+
+ if (!gap || (prev && resource_adjacent(prev, current)))
+ return prev;
+
+ return resource_iter_gap(parent, prev, gap, current);
+}
+
struct resource_entry *resource_list_create_entry(struct resource *res,
size_t extra_size)
{
diff --git a/include/linux/ioport.h b/include/linux/ioport.h
index e24984f7f722..1b5dfdcb373d 100644
--- a/include/linux/ioport.h
+++ b/include/linux/ioport.h
@@ -265,5 +265,12 @@ static inline void resource_set_range(struct resource *res,
resource_set_size(res, size);
}
+#define region_is_gap(region) ((region)->flags & IORESOURCE_UNSET)
+
+struct resource *resource_iter_first(struct resource *current, struct resource *gap);
+struct resource *resource_iter_last(struct resource *current, struct resource *gap);
+struct resource *resource_iter_prev(struct resource *current, struct resource *gap);
+struct resource *resource_iter_next(struct resource *current, struct resource *gap);
+
#endif /* __ASSEMBLY__ */
#endif /* _LINUX_IOPORT_H */
--
2.47.3
next prev parent reply other threads:[~2025-12-11 20:53 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-11 20:50 [PATCH 0/6] resource: add support for walking resource gaps Ahmad Fatoum
2025-12-11 20:50 ` Ahmad Fatoum [this message]
2025-12-11 20:50 ` [PATCH 2/6] test: self: implement resource walker selftest Ahmad Fatoum
2025-12-11 20:50 ` [PATCH 3/6] commands: iomem: add support for printing gaps Ahmad Fatoum
2025-12-11 20:50 ` [PATCH 4/6] test: py: add test for valid JSON output from iomem/clk_dump Ahmad Fatoum
2025-12-11 20:50 ` [PATCH 5/6] memory: add helpers for iterating over memory regions Ahmad Fatoum
2025-12-11 20:50 ` [PATCH 6/6] resource: implement release_region_range Ahmad Fatoum
2025-12-15 9:29 ` [PATCH 0/6] resource: add support for walking resource gaps Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251211205240.2836186-2-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox