From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH] memory: remap immediately in reserve_sdram_region()
Date: Mon, 23 Oct 2023 10:44:26 +0200 [thread overview]
Message-ID: <20231023084426.17581-1-s.hauer@pengutronix.de> (raw)
reserve_sdram_region() has the purpose of reserving SDRAM regions from
being accessed by the CPU. Right now this remaps the reserved region
during MMU setup. Instead of doing this, remap the region immediately.
The MMU may be enabled already by early code. This means that when
reserve_sdram_region() is called with MMU enabled, we can't rely on
the region being mapped non-executable right after the call, but only
when __mmu_init() is executed. This patch relaxes this constraint.
Also, reserve_sdram_region() may now be called after __mmu_init() is
executed.
So far we silently aligned the remapped region to page boundaries, but
really calling reserve_sdram_region() with non page aligned boundaries
has undesired effects on the regions between the reserved region and
the page boundaries. Stay with this behaviour, but warn the user when
the to be reserved region is not page aligned as this really shouldn't
happen.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
arch/arm/cpu/mmu_32.c | 2 +-
arch/arm/cpu/mmu_64.c | 3 +--
common/memory.c | 26 ++++++++++++++++++++++++++
include/memory.h | 9 ++-------
4 files changed, 30 insertions(+), 10 deletions(-)
diff --git a/arch/arm/cpu/mmu_32.c b/arch/arm/cpu/mmu_32.c
index 07b2250677..d0ada5866f 100644
--- a/arch/arm/cpu/mmu_32.c
+++ b/arch/arm/cpu/mmu_32.c
@@ -558,8 +558,8 @@ void __mmu_init(bool mmu_on)
pos = bank->start;
+ /* Skip reserved regions */
for_each_reserved_region(bank, rsv) {
- remap_range((void *)rsv->start, resource_size(rsv), MAP_UNCACHED);
remap_range((void *)pos, rsv->start - pos, MAP_CACHED);
pos = rsv->end + 1;
}
diff --git a/arch/arm/cpu/mmu_64.c b/arch/arm/cpu/mmu_64.c
index fb57260c90..b718cb1efa 100644
--- a/arch/arm/cpu/mmu_64.c
+++ b/arch/arm/cpu/mmu_64.c
@@ -243,9 +243,8 @@ void __mmu_init(bool mmu_on)
pos = bank->start;
+ /* Skip reserved regions */
for_each_reserved_region(bank, rsv) {
- remap_range((void *)resource_first_page(rsv),
- resource_count_pages(rsv), MAP_UNCACHED);
remap_range((void *)pos, rsv->start - pos, MAP_CACHED);
pos = rsv->end + 1;
}
diff --git a/common/memory.c b/common/memory.c
index 0ae9e7383c..d560d444b0 100644
--- a/common/memory.c
+++ b/common/memory.c
@@ -15,6 +15,7 @@
#include <asm/sections.h>
#include <malloc.h>
#include <of.h>
+#include <mmu.h>
/*
* Begin and End of memory area for malloc(), and current "brk"
@@ -211,6 +212,31 @@ struct resource *__request_sdram_region(const char *name, unsigned flags,
return NULL;
}
+/* use for secure firmware to inhibit speculation */
+struct resource *reserve_sdram_region(const char *name, resource_size_t start,
+ resource_size_t size)
+{
+ struct resource *res;
+
+ res = __request_sdram_region(name, IORESOURCE_BUSY, start, size);
+ if (IS_ERR(res))
+ return ERR_CAST(res);
+
+ if (!IS_ALIGNED(start, PAGE_SIZE)) {
+ pr_err("%s: %s start is not page aligned\n", __func__, name);
+ start = ALIGN_DOWN(start, PAGE_SIZE);
+ }
+
+ if (!IS_ALIGNED(size, PAGE_SIZE)) {
+ pr_err("%s: %s size is not page aligned\n", __func__, name);
+ size = ALIGN(size, PAGE_SIZE);
+ }
+
+ remap_range((void *)start, size, MAP_UNCACHED);
+
+ return res;
+}
+
int release_sdram_region(struct resource *res)
{
return release_region(res);
diff --git a/include/memory.h b/include/memory.h
index 9c2a037610..d8691972ec 100644
--- a/include/memory.h
+++ b/include/memory.h
@@ -43,13 +43,8 @@ static inline struct resource *request_sdram_region(const char *name,
return __request_sdram_region(name, 0, start, size);
}
-/* use for secure firmware to inhibit speculation */
-static inline struct resource *reserve_sdram_region(const char *name,
- resource_size_t start,
- resource_size_t size)
-{
- return __request_sdram_region(name, IORESOURCE_BUSY, start, size);
-}
+struct resource *reserve_sdram_region(const char *name, resource_size_t start,
+ resource_size_t size);
int release_sdram_region(struct resource *res);
--
2.39.2
reply other threads:[~2023-10-23 8:45 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20231023084426.17581-1-s.hauer@pengutronix.de \
--to=s.hauer@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