---
v7:
* v6 was misrebased, and dt_read_number()'s refactor was lost. v7 ensures
it's preserved in the code motion
---
xen/arch/riscv/cpufeature.c | 1 +
xen/arch/riscv/smpboot.c | 1 +
xen/common/device-tree/bootfdt.c | 12 ---
xen/common/device-tree/device-tree.c | 1 +
xen/common/device-tree/static-evtchn.c | 1 +
xen/common/sched/boot-cpupool.c | 1 +
xen/include/xen/bootfdt.h | 103 +++++++++++++++++++++++++
xen/include/xen/device_tree.h | 78 -------------------
8 files changed, 108 insertions(+), 90 deletions(-)
diff --git a/xen/arch/riscv/cpufeature.c b/xen/arch/riscv/cpufeature.c
index b7d5ec6580..b846a106a3 100644
--- a/xen/arch/riscv/cpufeature.c
+++ b/xen/arch/riscv/cpufeature.c
@@ -8,6 +8,7 @@
*/
#include <xen/bitmap.h>
+#include <xen/bootfdt.h>
#include <xen/ctype.h>
#include <xen/device_tree.h>
#include <xen/errno.h>
diff --git a/xen/arch/riscv/smpboot.c b/xen/arch/riscv/smpboot.c
index 470f6d1311..3b8bf98e20 100644
--- a/xen/arch/riscv/smpboot.c
+++ b/xen/arch/riscv/smpboot.c
@@ -1,3 +1,4 @@
+#include <xen/bootfdt.h>
#include <xen/cpumask.h>
#include <xen/device_tree.h>
#include <xen/errno.h>
diff --git a/xen/common/device-tree/bootfdt.c b/xen/common/device-tree/bootfdt.c
index 08d919aba6..67fe5c3cc3 100644
--- a/xen/common/device-tree/bootfdt.c
+++ b/xen/common/device-tree/bootfdt.c
@@ -215,18 +215,6 @@ u32 __init device_tree_get_u32(const void *fdt, int node,
return fdt32_to_cpu(get_unaligned_t(uint32_t, prop->data));
}
-/**
- * device_tree_for_each_node - iterate over all device tree sub-nodes
- * @fdt: flat device tree.
- * @node: parent node to start the search from
- * @func: function to call for each sub-node.
- * @data: data to pass to @func.
- *
- * Any nodes nested at DEVICE_TREE_MAX_DEPTH or deeper are ignored.
- *
- * Returns 0 if all nodes were iterated over successfully. If @func
- * returns a value different from 0, that value is returned immediately.
- */
int __init device_tree_for_each_node(const void *fdt, int node,
device_tree_node_func func,
void *data)
diff --git a/xen/common/device-tree/device-tree.c
b/xen/common/device-tree/device-tree.c
index 7bede20fa6..4ebdb2e52e 100644
--- a/xen/common/device-tree/device-tree.c
+++ b/xen/common/device-tree/device-tree.c
@@ -8,6 +8,7 @@
*/
#include <xen/bitops.h>
+#include <xen/bootfdt.h>
#include <xen/types.h>
#include <xen/init.h>
#include <xen/guest_access.h>
diff --git a/xen/common/device-tree/static-evtchn.c
b/xen/common/device-tree/static-evtchn.c
index 8b82e6b3d8..88342b44a1 100644
--- a/xen/common/device-tree/static-evtchn.c
+++ b/xen/common/device-tree/static-evtchn.c
@@ -1,5 +1,6 @@
/* SPDX-License-Identifier: GPL-2.0-only */
+#include <xen/bootfdt.h>
#include <xen/event.h>
#include <xen/static-evtchn.h>
diff --git a/xen/common/sched/boot-cpupool.c b/xen/common/sched/boot-cpupool.c
index 641f3495cb..03be73efdd 100644
--- a/xen/common/sched/boot-cpupool.c
+++ b/xen/common/sched/boot-cpupool.c
@@ -7,6 +7,7 @@
* Copyright (C) 2022 Arm Ltd.
*/
+#include <xen/bootfdt.h>
#include <xen/acpi.h>
#include <xen/sched.h>
diff --git a/xen/include/xen/bootfdt.h b/xen/include/xen/bootfdt.h
index ac2a79b59b..a5dfaa5c1d 100644
--- a/xen/include/xen/bootfdt.h
+++ b/xen/include/xen/bootfdt.h
@@ -2,8 +2,11 @@
#ifndef XEN_BOOTFDT_H
#define XEN_BOOTFDT_H
+#include <xen/byteorder.h>
+#include <xen/bug.h>
#include <xen/types.h>
#include <xen/kernel.h>
+#include <xen/lib.h>
#include <xen/macros.h>
#include <xen/xmalloc.h>
@@ -16,8 +19,92 @@
#define NR_MEM_BANKS 256
#define NR_SHMEM_BANKS 32
+/* Default #address and #size cells */
+#define DT_ROOT_NODE_ADDR_CELLS_DEFAULT 2
+#define DT_ROOT_NODE_SIZE_CELLS_DEFAULT 1
+
#define MAX_MODULES 32 /* Current maximum useful modules */
+#define DEVICE_TREE_MAX_DEPTH 16
+
+/* Helper to read a big number; size is in cells (not bytes) */
+static inline u64 dt_read_number(const __be32 *cell, int size)
+{
+ u64 r = be32_to_cpu(*cell);
+
+ switch ( size )
+ {
+ case 1:
+ break;
+ case 2:
+ r = (r << 32) | be32_to_cpu(cell[1]);
+ break;
+ default:
+ /* Nonsensical size. default to 1 */
+ printk(XENLOG_ERR "dt_read_number(,%d) bad size\n", size);
+ ASSERT_UNREACHABLE();
+ break;
+ };
+
+ return r;
+}
+
+/* Wrapper for dt_read_number() to return paddr_t (instead of uint64_t) */
+static inline paddr_t dt_read_paddr(const __be32 *cell, int size)
+{
+ uint64_t dt_r;
+ paddr_t r;
+
+ /*
+ * dt_read_number will return uint64_t whereas paddr_t may not be 64-bit.
+ * Thus, there is an implicit cast from uint64_t to paddr_t.
+ */
+ dt_r = dt_read_number(cell, size);
+
+ if ( dt_r != (paddr_t)dt_r )
+ {
+ printk("Physical address greater than max width supported\n");
+ WARN();
+ }
+
+ /*
+ * Xen will truncate the address/size if it is greater than the maximum
+ * supported width and it will give an appropriate warning.
+ */
+ r = dt_r;
+
+ return r;
+}
+
+static inline u64 dt_next_cell(int s, const __be32 **cellp)
+{
+ const __be32 *p = *cellp;
+
+ *cellp = p + s;
+ return dt_read_number(p, s);
+}
+
+typedef int (*device_tree_node_func)(const void *fdt,
+ int node, const char *name, int depth,
+ u32 address_cells, u32 size_cells,
+ void *data);
+
+/**
+ * device_tree_for_each_node - iterate over all device tree sub-nodes
+ * @fdt: flat device tree.
+ * @node: parent node to start the search from
+ * @func: function to call for each sub-node.
+ * @data: data to pass to @func.
+ *
+ * Any nodes nested at DEVICE_TREE_MAX_DEPTH or deeper are ignored.
+ *
+ * Returns 0 if all nodes were iterated over successfully. If @func
+ * returns a value different from 0, that value is returned immediately.
+ */
+int device_tree_for_each_node(const void *fdt, int node,
+ device_tree_node_func func,
+ void *data);
+
typedef enum {
BOOTMOD_XEN,
BOOTMOD_FDT,
@@ -261,4 +348,20 @@ static inline struct membanks *membanks_xzalloc(unsigned
int nr,
return banks;
}
+/*
+ * Interpret the property `prop_name` of `node` as a u32.
+ *
+ * Returns the property value on success; otherwise returns `dflt`.
+ */
+u32 device_tree_get_u32(const void *fdt, int node,
+ const char *prop_name, u32 dflt);
+
+/*
+ * Interpret the property `prop_name` of `node` as a "reg".
+ *
+ * Returns outputs in `start` and `size`.
+ */
+void device_tree_get_reg(const __be32 **cell, uint32_t address_cells,
+ uint32_t size_cells, paddr_t *start, paddr_t *size);
+
#endif /* XEN_BOOTFDT_H */
diff --git a/xen/include/xen/device_tree.h b/xen/include/xen/device_tree.h
index a7cc092d05..8a39a60c54 100644
--- a/xen/include/xen/device_tree.h
+++ b/xen/include/xen/device_tree.h
@@ -22,8 +22,6 @@
#include <xen/list.h>
#include <xen/rwlock.h>
-#define DEVICE_TREE_MAX_DEPTH 16
-
/*
* Struct used for matching a device
*/
@@ -164,17 +162,8 @@ struct dt_raw_irq {
u32 specifier[DT_MAX_IRQ_SPEC];
};
-typedef int (*device_tree_node_func)(const void *fdt,
- int node, const char *name, int depth,
- u32 address_cells, u32 size_cells,
- void *data);
-
extern const void *device_tree_flattened;
-int device_tree_for_each_node(const void *fdt, int node,
- device_tree_node_func func,
- void *data);
-
/**
* dt_unflatten_host_device_tree - Unflatten the host device tree
*
@@ -245,10 +234,6 @@ void intc_dt_preinit(void);
#define dt_node_cmp(s1, s2) strcasecmp((s1), (s2))
#define dt_compat_cmp(s1, s2) strcasecmp((s1), (s2))
-/* Default #address and #size cells */
-#define DT_ROOT_NODE_ADDR_CELLS_DEFAULT 2
-#define DT_ROOT_NODE_SIZE_CELLS_DEFAULT 1
-
#define dt_for_each_property_node(dn, pp) \
for ( pp = (dn)->properties; (pp) != NULL; pp = (pp)->next )
@@ -258,55 +243,6 @@ void intc_dt_preinit(void);
#define dt_for_each_child_node(dt, dn) \
for ( dn = (dt)->child; (dn) != NULL; dn = (dn)->sibling )
-/* Helper to read a big number; size is in cells (not bytes) */
-static inline u64 dt_read_number(const __be32 *cell, int size)
-{
- u64 r = be32_to_cpu(*cell);
-
- switch ( size )
- {
- case 1:
- break;
- case 2:
- r = (r << 32) | be32_to_cpu(cell[1]);
- break;
- default:
- /* Nonsensical size. default to 1 */
- printk(XENLOG_ERR "dt_read_number(,%d) bad size\n", size);
- ASSERT_UNREACHABLE();
- break;
- };
-
- return r;
-}
-
-/* Wrapper for dt_read_number() to return paddr_t (instead of uint64_t) */
-static inline paddr_t dt_read_paddr(const __be32 *cell, int size)
-{
- uint64_t dt_r;
- paddr_t r;
-
- /*
- * dt_read_number will return uint64_t whereas paddr_t may not be 64-bit.
- * Thus, there is an implicit cast from uint64_t to paddr_t.
- */
- dt_r = dt_read_number(cell, size);
-
- if ( dt_r != (paddr_t)dt_r )
- {
- printk("Physical address greater than max width supported\n");
- WARN();
- }
-
- /*
- * Xen will truncate the address/size if it is greater than the maximum
- * supported width and it will give an appropriate warning.
- */
- r = dt_r;
-
- return r;
-}
-
/* Helper to convert a number of cells to bytes */
static inline int dt_cells_to_size(int size)
{
@@ -319,14 +255,6 @@ static inline int dt_size_to_cells(int bytes)
return (bytes / sizeof(u32));
}
-static inline u64 dt_next_cell(int s, const __be32 **cellp)
-{
- const __be32 *p = *cellp;
-
- *cellp = p + s;
- return dt_read_number(p, s);
-}
-
static inline const char *dt_node_full_name(const struct dt_device_node *np)
{
return (np && np->full_name) ? np->full_name : "<no-node>";
@@ -984,12 +912,6 @@ int dt_map_id(const struct dt_device_node *np, uint32_t id,
struct dt_device_node *dt_find_node_by_phandle(dt_phandle handle);
-void device_tree_get_reg(const __be32 **cell, uint32_t address_cells,
- uint32_t size_cells, paddr_t *start, paddr_t *size);
-
-u32 device_tree_get_u32(const void *fdt, int node,
- const char *prop_name, u32 dflt);
-
#ifdef CONFIG_DEVICE_TREE_DEBUG
#define dt_dprintk(fmt, args...) \
printk(XENLOG_DEBUG fmt, ## args)