[PATCH v2 6/8] pbl: fdt: add support to parse reserved mem
Rouven Czerwinski
r.czerwinski at pengutronix.de
Tue Aug 3 02:44:16 PDT 2021
This allows the PBL to fill a reserved memory map which a subsequent
commit will use modify the early MMU mapping.
Signed-off-by: Rouven Czerwinski <r.czerwinski at pengutronix.de>
---
include/pbl.h | 25 ++++++++++++
pbl/fdt.c | 103 ++++++++++++++++++++++++++++++++++++++++++--------
2 files changed, 113 insertions(+), 15 deletions(-)
diff --git a/include/pbl.h b/include/pbl.h
index f58daec735..03272879d7 100644
--- a/include/pbl.h
+++ b/include/pbl.h
@@ -42,5 +42,30 @@ struct fdt_device_id {
const void *
fdt_device_get_match_data(const void *fdt, const char *nodepath,
const struct fdt_device_id ids[]);
+struct pbl_reserved_memory {
+ phys_addr_t base;
+ phys_addr_t size;
+ unsigned int flags;
+};
+
+#define MAX_RESERVED_REGIONS 8
+
+#define FDT_RES_MEM_FLAG_NOMAP BIT(0)
+
+void fdt_fill_reserve_mem(const void *fdt);
+
+#ifdef CONFIG_LIBFDT
+const struct pbl_reserved_memory *get_pbl_reserved_memory(void);
+int get_pbl_reserved_memory_num(void);
+#else
+static inline const struct pbl_reserved_memory *get_pbl_reserved_memory(void)
+{
+ return NULL;
+}
+static inline int get_pbl_reserved_memory_num(void)
+{
+ return 0;
+}
+#endif
#endif /* __PBL_H__ */
diff --git a/pbl/fdt.c b/pbl/fdt.c
index 18ddb9f48a..383d0fec74 100644
--- a/pbl/fdt.c
+++ b/pbl/fdt.c
@@ -5,8 +5,8 @@
void fdt_find_mem(const void *fdt, unsigned long *membase, unsigned long *memsize)
{
- const __be32 *nap, *nsp, *reg;
- uint32_t na, ns;
+ const __be32 *reg;
+ int na, ns;
uint64_t memsize64, membase64;
int node, size, i;
@@ -16,26 +16,17 @@ void fdt_find_mem(const void *fdt, unsigned long *membase, unsigned long *memsiz
goto err;
}
- /* Find the #address-cells and #size-cells properties */
- node = fdt_path_offset(fdt, "/");
- if (node < 0) {
- pr_err("Cannot find root node\n");
- goto err;
- }
-
- nap = fdt_getprop(fdt, node, "#address-cells", &size);
- if (!nap || (size != 4)) {
+ na = fdt_address_cells(fdt, 0);
+ if (na < 0) {
pr_err("Cannot find #address-cells property");
goto err;
}
- na = fdt32_to_cpu(*nap);
- nsp = fdt_getprop(fdt, node, "#size-cells", &size);
- if (!nsp || (size != 4)) {
+ ns = fdt_size_cells(fdt, 0);
+ if (ns < 0) {
pr_err("Cannot find #size-cells property");
goto err;
}
- ns = fdt32_to_cpu(*nap);
/* Find the memory range */
node = fdt_node_offset_by_prop_value(fdt, -1, "device_type",
@@ -103,3 +94,85 @@ const void *fdt_device_get_match_data(const void *fdt, const char *nodepath,
return NULL;
}
+
+static struct pbl_reserved_memory reserved_mem[MAX_RESERVED_REGIONS];
+static int reserved_mem_count;
+
+void fdt_fill_reserve_mem(const void *fdt)
+{
+ const __be32 *nap, *nsp, *reg;
+ uint32_t na, ns;
+ int node, size, i, parent;
+ const void *prop;
+ uint64_t memsize64, membase64;
+ bool nomap;
+
+ /* Make sure FDT blob is sane */
+ if (fdt_check_header(fdt) != 0) {
+ pr_err("Invalid device tree blob\n");
+ return;
+ }
+
+ parent = fdt_path_offset(fdt, "/reserved-memory");
+ if (parent < 0) {
+ pr_info("Cannot find reserved-memory node\n");
+ return;
+ }
+
+ nap = fdt_getprop(fdt, parent, "#address-cells", &size);
+ if (!nap || (size != 4)) {
+ pr_err("Cannot find #address-cells property");
+ return;
+ }
+ na = fdt32_to_cpu(*nap);
+
+ nsp = fdt_getprop(fdt, parent, "#size-cells", &size);
+ if (!nsp || (size != 4)) {
+ pr_err("Cannot find #size-cells property");
+ return;
+ }
+ ns = fdt32_to_cpu(*nap);
+
+ fdt_for_each_subnode(node, fdt, parent) {
+ nomap = true;
+ reg = fdt_getprop(fdt, node, "reg", &size);
+ if (size < (na + ns) * sizeof(u32)) {
+ pr_err("cannot get memory range\n");
+ return;
+ }
+
+ membase64 = 0;
+ for (i = 0; i < na; i++)
+ membase64 = (membase64 << 32) | fdt32_to_cpu(*reg++);
+
+ /* get the memsize and truncate it to under 4G on 32 bit machines */
+ memsize64 = 0;
+ for (i = 0; i < ns; i++)
+ memsize64 = (memsize64 << 32) | fdt32_to_cpu(*reg++);
+
+ prop = fdt_getprop(fdt, node, "nomap", NULL);
+ if (!prop)
+ nomap = false;
+
+ reserved_mem[reserved_mem_count].base = membase64;
+ reserved_mem[reserved_mem_count].size = memsize64;
+ if (nomap)
+ reserved_mem[reserved_mem_count].flags |= FDT_RES_MEM_FLAG_NOMAP;
+ reserved_mem_count++;
+ }
+
+ if ((node < 0) && (node != -FDT_ERR_NOTFOUND)) {
+ pr_err("Error while parsing reserved-memory nodes: %d\n", node);
+ return;
+ }
+}
+
+const struct pbl_reserved_memory *get_pbl_reserved_memory(void)
+{
+ return reserved_mem;
+}
+
+int get_pbl_reserved_memory_num(void)
+{
+ return reserved_mem_count;
+}
--
2.32.0
More information about the barebox
mailing list