summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAtish Patra <atish.patra@wdc.com>2020-03-23 22:48:58 +0300
committerAnup Patel <anup@brainfault.org>2020-03-24 11:00:32 +0300
commit040e4e2ec220403fb2508aa0809052924ef63a0b (patch)
treeb2a05b09962fc06fca987035b0b0cd8b20bc449e
parentd1d6560a878200679e6e12e77032d06add8847a5 (diff)
downloadopensbi-040e4e2ec220403fb2508aa0809052924ef63a0b.tar.xz
lib: utils: Move fdt fixup helper routines to a different file
FDT helper file contain both fdt fixup and parsing functions. Split the fixup related functions to a separate file for a better code organization. Signed-off-by: Atish Patra <atish.patra@wdc.com> Reviewed-by: Anup Patel <anup.patel@wdc.com>
-rw-r--r--include/sbi_utils/fdt/fdt_fixup.h64
-rw-r--r--include/sbi_utils/fdt/fdt_helper.h56
-rw-r--r--lib/utils/fdt/fdt_fixup.c203
-rw-r--r--lib/utils/fdt/fdt_helper.c187
-rw-r--r--lib/utils/fdt/objects.mk1
-rw-r--r--platform/andes/ae350/platform.c2
-rw-r--r--platform/fpga/ariane/platform.c2
-rw-r--r--platform/fpga/openpiton/platform.c1
-rw-r--r--platform/qemu/virt/platform.c2
-rw-r--r--platform/sifive/fu540/platform.c2
10 files changed, 276 insertions, 244 deletions
diff --git a/include/sbi_utils/fdt/fdt_fixup.h b/include/sbi_utils/fdt/fdt_fixup.h
new file mode 100644
index 0000000..54568c2
--- /dev/null
+++ b/include/sbi_utils/fdt/fdt_fixup.h
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * fdt_fixup.h - Flat Device Tree manipulation helper routines
+ * Implement platform specific DT fixups on top of libfdt.
+ *
+ * Copyright (C) 2020 Bin Meng <bmeng.cn@gmail.com>
+ */
+
+#ifndef __FDT_FIXUP_H__
+#define __FDT_FIXUP_H__
+
+/**
+ * Fix up the CPU node in the device tree
+ *
+ * This routine updates the "status" property of a CPU node in the device tree
+ * to "disabled" if that hart is in disabled state in OpenSBI.
+ *
+ * It is recommended that platform codes call this helper in their final_init()
+ *
+ * @param fdt: device tree blob
+ */
+void fdt_cpu_fixup(void *fdt);
+
+/**
+ * Fix up the PLIC node in the device tree
+ *
+ * This routine updates the "interrupt-extended" property of the PLIC node in
+ * the device tree to hide the M-mode external interrupt from CPUs.
+ *
+ * It is recommended that platform codes call this helper in their final_init()
+ *
+ * @param fdt: device tree blob
+ * @param compat: PLIC node compatible string
+ */
+void fdt_plic_fixup(void *fdt, const char *compat);
+
+/**
+ * Fix up the reserved memory node in the device tree
+ *
+ * This routine inserts a child node of the reserved memory node in the device
+ * tree that describes the protected memory region done by OpenSBI via PMP.
+ *
+ * It is recommended that platform codes call this helper in their final_init()
+ *
+ * @param fdt: device tree blob
+ * @return zero on success and -ve on failure
+ */
+int fdt_reserved_memory_fixup(void *fdt);
+
+/**
+ * General device tree fix-up
+ *
+ * This routine do all required device tree fix-ups for a typical platform.
+ * It fixes up the PLIC node and the reserved memory node in the device tree
+ * by calling the corresponding helper routines to accomplish the task.
+ *
+ * It is recommended that platform codes call this helper in their final_init()
+ *
+ * @param fdt: device tree blob
+ */
+void fdt_fixups(void *fdt);
+
+#endif /* __FDT_FIXUP_H__ */
+
diff --git a/include/sbi_utils/fdt/fdt_helper.h b/include/sbi_utils/fdt/fdt_helper.h
index 42baeab..06b8a62 100644
--- a/include/sbi_utils/fdt/fdt_helper.h
+++ b/include/sbi_utils/fdt/fdt_helper.h
@@ -1,7 +1,8 @@
// SPDX-License-Identifier: BSD-2-Clause
/*
- * fdt_helper.h - Flat Device Tree manipulation helper routines
- * Implement helper routines on top of libfdt for OpenSBI usage
+ * fdt_helper.h - Flat Device Tree parsing helper routines
+ * Implement helper routines to parse FDT nodes on top of
+ * libfdt for OpenSBI usage
*
* Copyright (C) 2020 Bin Meng <bmeng.cn@gmail.com>
*/
@@ -29,55 +30,4 @@ int fdt_parse_plic(void *fdt, struct platform_plic_data *plic,
int fdt_parse_clint(void *fdt, unsigned long *clint_addr,
const char *compatible);
-/**
- * Fix up the CPU node in the device tree
- *
- * This routine updates the "status" property of a CPU node in the device tree
- * to "disabled" if that hart is in disabled state in OpenSBI.
- *
- * It is recommended that platform codes call this helper in their final_init()
- *
- * @param fdt: device tree blob
- */
-void fdt_cpu_fixup(void *fdt);
-
-/**
- * Fix up the PLIC node in the device tree
- *
- * This routine updates the "interrupt-extended" property of the PLIC node in
- * the device tree to hide the M-mode external interrupt from CPUs.
- *
- * It is recommended that platform codes call this helper in their final_init()
- *
- * @param fdt: device tree blob
- * @param compat: PLIC node compatible string
- */
-void fdt_plic_fixup(void *fdt, const char *compat);
-
-/**
- * Fix up the reserved memory node in the device tree
- *
- * This routine inserts a child node of the reserved memory node in the device
- * tree that describes the protected memory region done by OpenSBI via PMP.
- *
- * It is recommended that platform codes call this helper in their final_init()
- *
- * @param fdt: device tree blob
- * @return zero on success and -ve on failure
- */
-int fdt_reserved_memory_fixup(void *fdt);
-
-/**
- * General device tree fix-up
- *
- * This routine do all required device tree fix-ups for a typical platform.
- * It fixes up the PLIC node and the reserved memory node in the device tree
- * by calling the corresponding helper routines to accomplish the task.
- *
- * It is recommended that platform codes call this helper in their final_init()
- *
- * @param fdt: device tree blob
- */
-void fdt_fixups(void *fdt);
-
#endif /* __FDT_HELPER_H__ */
diff --git a/lib/utils/fdt/fdt_fixup.c b/lib/utils/fdt/fdt_fixup.c
new file mode 100644
index 0000000..b91e95f
--- /dev/null
+++ b/lib/utils/fdt/fdt_fixup.c
@@ -0,0 +1,203 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * fdt_fixup.c - Flat Device Tree parsing helper routines
+ * Implement helper routines to parse FDT nodes on top of
+ * libfdt for OpenSBI usage
+ *
+ * Copyright (C) 2020 Bin Meng <bmeng.cn@gmail.com>
+ */
+
+#include <libfdt.h>
+#include <sbi/riscv_asm.h>
+#include <sbi/sbi_console.h>
+#include <sbi/sbi_platform.h>
+#include <sbi/sbi_scratch.h>
+
+void fdt_cpu_fixup(void *fdt)
+{
+ struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+ const struct sbi_platform *plat = sbi_platform_ptr(scratch);
+ char cpu_node[32] = "";
+ int cpu_offset;
+ int err;
+ u32 i;
+
+ err = fdt_open_into(fdt, fdt, fdt_totalsize(fdt) + 32);
+ if (err < 0)
+ return;
+
+ /* assume hart ids are continuous */
+ for (i = 0; i < sbi_platform_hart_count(plat); i++) {
+ sbi_sprintf(cpu_node, "/cpus/cpu@%d", i);
+ cpu_offset = fdt_path_offset(fdt, cpu_node);
+
+ if (sbi_platform_hart_invalid(plat, i))
+ fdt_setprop_string(fdt, cpu_offset, "status",
+ "disabled");
+
+ memset(cpu_node, 0, sizeof(cpu_node));
+ }
+}
+
+void fdt_plic_fixup(void *fdt, const char *compat)
+{
+ u32 *cells;
+ int i, cells_count;
+ int plic_off;
+
+ plic_off = fdt_node_offset_by_compatible(fdt, 0, compat);
+ if (plic_off < 0)
+ return;
+
+ cells = (u32 *)fdt_getprop(fdt, plic_off,
+ "interrupts-extended", &cells_count);
+ if (!cells)
+ return;
+
+ cells_count = cells_count / sizeof(u32);
+ if (!cells_count)
+ return;
+
+ for (i = 0; i < (cells_count / 2); i++) {
+ if (fdt32_to_cpu(cells[2 * i + 1]) == IRQ_M_EXT)
+ cells[2 * i + 1] = cpu_to_fdt32(0xffffffff);
+ }
+}
+
+/**
+ * We use PMP to protect OpenSBI firmware to safe-guard it from buggy S-mode
+ * software, see pmp_init() in lib/sbi/sbi_hart.c. The protected memory region
+ * information needs to be conveyed to S-mode software (e.g.: operating system)
+ * via some well-known method.
+ *
+ * With device tree, this can be done by inserting a child node of the reserved
+ * memory node which is used to specify one or more regions of reserved memory.
+ *
+ * For the reserved memory node bindings, see Linux kernel documentation at
+ * Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt
+ *
+ * Some additional memory spaces may be protected by platform codes via PMP as
+ * well, and corresponding child nodes will be inserted.
+ */
+int fdt_reserved_memory_fixup(void *fdt)
+{
+ struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+ const struct sbi_platform *plat = sbi_platform_ptr(scratch);
+ unsigned long prot, addr, size;
+ int na = fdt_address_cells(fdt, 0);
+ int ns = fdt_size_cells(fdt, 0);
+ fdt32_t addr_high, addr_low;
+ fdt32_t size_high, size_low;
+ fdt32_t reg[4];
+ fdt32_t *val;
+ char name[32];
+ int parent, subnode;
+ int i, j;
+ int err;
+
+ if (!sbi_platform_has_pmp(plat))
+ return 0;
+
+ /* expand the device tree to accommodate new node */
+ err = fdt_open_into(fdt, fdt, fdt_totalsize(fdt) + 256);
+ if (err < 0)
+ return err;
+
+ /* try to locate the reserved memory node */
+ parent = fdt_path_offset(fdt, "/reserved-memory");
+ if (parent < 0) {
+ /* if such node does not exist, create one */
+ parent = fdt_add_subnode(fdt, 0, "reserved-memory");
+ if (parent < 0)
+ return parent;
+
+ /*
+ * reserved-memory node has 3 required properties:
+ * - #address-cells: the same value as the root node
+ * - #size-cells: the same value as the root node
+ * - ranges: should be empty
+ */
+
+ err = fdt_setprop_empty(fdt, parent, "ranges");
+ if (err < 0)
+ return err;
+
+ err = fdt_setprop_u32(fdt, parent, "#size-cells", ns);
+ if (err < 0)
+ return err;
+
+ err = fdt_setprop_u32(fdt, parent, "#address-cells", na);
+ if (err < 0)
+ return err;
+ }
+
+ /*
+ * We assume the given device tree does not contain any memory region
+ * child node protected by PMP. Normally PMP programming happens at
+ * M-mode firmware. The memory space used by OpenSBI is protected.
+ * Some additional memory spaces may be protected by platform codes.
+ *
+ * With above assumption, we create child nodes directly.
+ */
+
+ for (i = 0, j = 0; i < PMP_COUNT; i++) {
+ pmp_get(i, &prot, &addr, &size);
+ if (!(prot & PMP_A))
+ continue;
+ if (!(prot & (PMP_R | PMP_W | PMP_X))) {
+ addr_high = (u64)addr >> 32;
+ addr_low = addr;
+ size_high = (u64)size >> 32;
+ size_low = size;
+
+ if (na > 1 && addr_high)
+ sbi_snprintf(name, sizeof(name),
+ "mmode_pmp%d@%x,%x", j,
+ addr_high, addr_low);
+ else
+ sbi_snprintf(name, sizeof(name),
+ "mmode_pmp%d@%x", j,
+ addr_low);
+
+ subnode = fdt_add_subnode(fdt, parent, name);
+ if (subnode < 0)
+ return subnode;
+
+ /*
+ * Tell operating system not to create a virtual
+ * mapping of the region as part of its standard
+ * mapping of system memory.
+ */
+ err = fdt_setprop_empty(fdt, subnode, "no-map");
+ if (err < 0)
+ return err;
+
+ /* encode the <reg> property value */
+ val = reg;
+ if (na > 1)
+ *val++ = cpu_to_fdt32(addr_high);
+ *val++ = cpu_to_fdt32(addr_low);
+ if (ns > 1)
+ *val++ = cpu_to_fdt32(size_high);
+ *val++ = cpu_to_fdt32(size_low);
+
+ err = fdt_setprop(fdt, subnode, "reg", reg,
+ (na + ns) * sizeof(fdt32_t));
+ if (err < 0)
+ return err;
+
+ j++;
+ }
+ }
+
+ return 0;
+}
+
+void fdt_fixups(void *fdt)
+{
+ fdt_plic_fixup(fdt, "riscv,plic0");
+
+ fdt_reserved_memory_fixup(fdt);
+}
+
+
diff --git a/lib/utils/fdt/fdt_helper.c b/lib/utils/fdt/fdt_helper.c
index 81617c9..07f7221 100644
--- a/lib/utils/fdt/fdt_helper.c
+++ b/lib/utils/fdt/fdt_helper.c
@@ -13,193 +13,6 @@
#include <sbi/sbi_scratch.h>
#include <sbi_utils/fdt/fdt_helper.h>
-void fdt_cpu_fixup(void *fdt)
-{
- struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
- const struct sbi_platform *plat = sbi_platform_ptr(scratch);
- char cpu_node[32] = "";
- int cpu_offset;
- int err;
- u32 i;
-
- err = fdt_open_into(fdt, fdt, fdt_totalsize(fdt) + 32);
- if (err < 0)
- return;
-
- /* assume hart ids are continuous */
- for (i = 0; i < sbi_platform_hart_count(plat); i++) {
- sbi_sprintf(cpu_node, "/cpus/cpu@%d", i);
- cpu_offset = fdt_path_offset(fdt, cpu_node);
-
- if (sbi_platform_hart_invalid(plat, i))
- fdt_setprop_string(fdt, cpu_offset, "status",
- "disabled");
-
- memset(cpu_node, 0, sizeof(cpu_node));
- }
-}
-
-void fdt_plic_fixup(void *fdt, const char *compat)
-{
- u32 *cells;
- int i, cells_count;
- int plic_off;
-
- plic_off = fdt_node_offset_by_compatible(fdt, 0, compat);
- if (plic_off < 0)
- return;
-
- cells = (u32 *)fdt_getprop(fdt, plic_off,
- "interrupts-extended", &cells_count);
- if (!cells)
- return;
-
- cells_count = cells_count / sizeof(u32);
- if (!cells_count)
- return;
-
- for (i = 0; i < (cells_count / 2); i++) {
- if (fdt32_to_cpu(cells[2 * i + 1]) == IRQ_M_EXT)
- cells[2 * i + 1] = cpu_to_fdt32(0xffffffff);
- }
-}
-
-/**
- * We use PMP to protect OpenSBI firmware to safe-guard it from buggy S-mode
- * software, see pmp_init() in lib/sbi/sbi_hart.c. The protected memory region
- * information needs to be conveyed to S-mode software (e.g.: operating system)
- * via some well-known method.
- *
- * With device tree, this can be done by inserting a child node of the reserved
- * memory node which is used to specify one or more regions of reserved memory.
- *
- * For the reserved memory node bindings, see Linux kernel documentation at
- * Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt
- *
- * Some additional memory spaces may be protected by platform codes via PMP as
- * well, and corresponding child nodes will be inserted.
- */
-int fdt_reserved_memory_fixup(void *fdt)
-{
- struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
- const struct sbi_platform *plat = sbi_platform_ptr(scratch);
- unsigned long prot, addr, size;
- int na = fdt_address_cells(fdt, 0);
- int ns = fdt_size_cells(fdt, 0);
- fdt32_t addr_high, addr_low;
- fdt32_t size_high, size_low;
- fdt32_t reg[4];
- fdt32_t *val;
- char name[32];
- int parent, subnode;
- int i, j;
- int err;
-
- if (!sbi_platform_has_pmp(plat))
- return 0;
-
- /* expand the device tree to accommodate new node */
- err = fdt_open_into(fdt, fdt, fdt_totalsize(fdt) + 256);
- if (err < 0)
- return err;
-
- /* try to locate the reserved memory node */
- parent = fdt_path_offset(fdt, "/reserved-memory");
- if (parent < 0) {
- /* if such node does not exist, create one */
- parent = fdt_add_subnode(fdt, 0, "reserved-memory");
- if (parent < 0)
- return parent;
-
- /*
- * reserved-memory node has 3 required properties:
- * - #address-cells: the same value as the root node
- * - #size-cells: the same value as the root node
- * - ranges: should be empty
- */
-
- err = fdt_setprop_empty(fdt, parent, "ranges");
- if (err < 0)
- return err;
-
- err = fdt_setprop_u32(fdt, parent, "#size-cells", ns);
- if (err < 0)
- return err;
-
- err = fdt_setprop_u32(fdt, parent, "#address-cells", na);
- if (err < 0)
- return err;
- }
-
- /*
- * We assume the given device tree does not contain any memory region
- * child node protected by PMP. Normally PMP programming happens at
- * M-mode firmware. The memory space used by OpenSBI is protected.
- * Some additional memory spaces may be protected by platform codes.
- *
- * With above assumption, we create child nodes directly.
- */
-
- for (i = 0, j = 0; i < PMP_COUNT; i++) {
- pmp_get(i, &prot, &addr, &size);
- if (!(prot & PMP_A))
- continue;
- if (!(prot & (PMP_R | PMP_W | PMP_X))) {
- addr_high = (u64)addr >> 32;
- addr_low = addr;
- size_high = (u64)size >> 32;
- size_low = size;
-
- if (na > 1 && addr_high)
- sbi_snprintf(name, sizeof(name),
- "mmode_pmp%d@%x,%x", j,
- addr_high, addr_low);
- else
- sbi_snprintf(name, sizeof(name),
- "mmode_pmp%d@%x", j,
- addr_low);
-
- subnode = fdt_add_subnode(fdt, parent, name);
- if (subnode < 0)
- return subnode;
-
- /*
- * Tell operating system not to create a virtual
- * mapping of the region as part of its standard
- * mapping of system memory.
- */
- err = fdt_setprop_empty(fdt, subnode, "no-map");
- if (err < 0)
- return err;
-
- /* encode the <reg> property value */
- val = reg;
- if (na > 1)
- *val++ = cpu_to_fdt32(addr_high);
- *val++ = cpu_to_fdt32(addr_low);
- if (ns > 1)
- *val++ = cpu_to_fdt32(size_high);
- *val++ = cpu_to_fdt32(size_low);
-
- err = fdt_setprop(fdt, subnode, "reg", reg,
- (na + ns) * sizeof(fdt32_t));
- if (err < 0)
- return err;
-
- j++;
- }
- }
-
- return 0;
-}
-
-void fdt_fixups(void *fdt)
-{
- fdt_plic_fixup(fdt, "riscv,plic0");
-
- fdt_reserved_memory_fixup(fdt);
-}
-
static int fdt_get_node_addr_size(void *fdt, int node, unsigned long *addr,
unsigned long *size)
{
diff --git a/lib/utils/fdt/objects.mk b/lib/utils/fdt/objects.mk
index 062e6bd..b860a83 100644
--- a/lib/utils/fdt/objects.mk
+++ b/lib/utils/fdt/objects.mk
@@ -5,3 +5,4 @@
#
libsbiutils-objs-y += fdt/fdt_helper.o
+libsbiutils-objs-y += fdt/fdt_fixup.o
diff --git a/platform/andes/ae350/platform.c b/platform/andes/ae350/platform.c
index 5f1ab6c..c1311d4 100644
--- a/platform/andes/ae350/platform.c
+++ b/platform/andes/ae350/platform.c
@@ -13,7 +13,7 @@
#include <sbi/sbi_console.h>
#include <sbi/sbi_const.h>
#include <sbi/sbi_platform.h>
-#include <sbi_utils/fdt/fdt_helper.h>
+#include <sbi_utils/fdt/fdt_fixup.h>
#include <sbi_utils/irqchip/plic.h>
#include <sbi_utils/serial/uart8250.h>
#include "platform.h"
diff --git a/platform/fpga/ariane/platform.c b/platform/fpga/ariane/platform.c
index 9c13529..9537b64 100644
--- a/platform/fpga/ariane/platform.c
+++ b/platform/fpga/ariane/platform.c
@@ -11,7 +11,7 @@
#include <sbi/sbi_const.h>
#include <sbi/sbi_hart.h>
#include <sbi/sbi_platform.h>
-#include <sbi_utils/fdt/fdt_helper.h>
+#include <sbi_utils/fdt/fdt_fixup.h>
#include <sbi_utils/irqchip/plic.h>
#include <sbi_utils/serial/uart8250.h>
#include <sbi_utils/sys/clint.h>
diff --git a/platform/fpga/openpiton/platform.c b/platform/fpga/openpiton/platform.c
index e2ee671..96c2d57 100644
--- a/platform/fpga/openpiton/platform.c
+++ b/platform/fpga/openpiton/platform.c
@@ -11,6 +11,7 @@
#include <sbi/sbi_hart.h>
#include <sbi/sbi_platform.h>
#include <sbi_utils/fdt/fdt_helper.h>
+#include <sbi_utils/fdt/fdt_fixup.h>
#include <sbi_utils/irqchip/plic.h>
#include <sbi_utils/serial/uart8250.h>
#include <sbi_utils/sys/clint.h>
diff --git a/platform/qemu/virt/platform.c b/platform/qemu/virt/platform.c
index 539006e..86d1bcc 100644
--- a/platform/qemu/virt/platform.c
+++ b/platform/qemu/virt/platform.c
@@ -14,7 +14,7 @@
#include <sbi/sbi_const.h>
#include <sbi/sbi_hart.h>
#include <sbi/sbi_platform.h>
-#include <sbi_utils/fdt/fdt_helper.h>
+#include <sbi_utils/fdt/fdt_fixup.h>
#include <sbi_utils/irqchip/plic.h>
#include <sbi_utils/serial/uart8250.h>
#include <sbi_utils/sys/clint.h>
diff --git a/platform/sifive/fu540/platform.c b/platform/sifive/fu540/platform.c
index 6d43e92..baa0d13 100644
--- a/platform/sifive/fu540/platform.c
+++ b/platform/sifive/fu540/platform.c
@@ -14,7 +14,7 @@
#include <sbi/sbi_console.h>
#include <sbi/sbi_const.h>
#include <sbi/sbi_platform.h>
-#include <sbi_utils/fdt/fdt_helper.h>
+#include <sbi_utils/fdt/fdt_fixup.h>
#include <sbi_utils/irqchip/plic.h>
#include <sbi_utils/serial/sifive-uart.h>
#include <sbi_utils/sys/clint.h>