diff mbox series

[1/3] RISC-V: Allow device trees to be built into the kernel

Message ID 20200408165802.167546-2-palmer@dabbelt.com (mailing list archive)
State New, archived
Headers show
Series [1/3] RISC-V: Allow device trees to be built into the kernel | expand

Commit Message

Palmer Dabbelt April 8, 2020, 4:57 p.m. UTC
From: Palmer Dabbelt <palmerdabbelt@google.com>

Some systems don't provide a useful device tree to the kernel on boot.
Chasing around bootloaders for these systems is a headache, so instead
le't's just keep device tree table in the kernel, keyed by the SOC's
unique identifier, that contains the relevant DTB.

This is only implemented for M mode right now.  While we could implement
this via the SBI calls that allow access to these identifiers, we don't
have any systems that need this right now.

Signed-off-by: Palmer Dabbelt <palmerdabbelt@google.com>
---
 arch/riscv/Kconfig                    |  5 +++++
 arch/riscv/include/asm/soc.h          | 27 +++++++++++++++++++++++++++
 arch/riscv/kernel/Makefile            |  3 +++
 arch/riscv/kernel/builtin-dtb-table.c |  6 ++++++
 arch/riscv/kernel/builtin-dtb.S       |  3 +++
 arch/riscv/kernel/soc.c               | 25 +++++++++++++++++++++++++
 arch/riscv/kernel/vmlinux.lds.S       |  5 +++++
 arch/riscv/mm/init.c                  | 14 ++++++++++++++
 8 files changed, 88 insertions(+)
 create mode 100644 arch/riscv/kernel/builtin-dtb-table.c
 create mode 100644 arch/riscv/kernel/builtin-dtb.S
diff mbox series

Patch

diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index bc713666f00a..7572afc43bd8 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -380,6 +380,11 @@  endchoice
 
 endmenu
 
+config BUILTIN_DTB
+	def_bool y
+	depends on RISCV_M_MODE
+	depends on OF
+
 menu "Power management options"
 
 source "kernel/power/Kconfig"
diff --git a/arch/riscv/include/asm/soc.h b/arch/riscv/include/asm/soc.h
index 18bd1253ea18..c232e57bb356 100644
--- a/arch/riscv/include/asm/soc.h
+++ b/arch/riscv/include/asm/soc.h
@@ -1,6 +1,7 @@ 
 /* SPDX-License-Identifier: GPL-2.0-or-later */
 /*
  * Copyright (C) 2020 Western Digital Corporation or its affiliates.
+ * Copyright (C) 2020 Google, Inc
  */
 
 #ifndef _ASM_RISCV_SOC_H
@@ -22,5 +23,31 @@ 
 
 void soc_early_init(void);
 
+/*
+ * Allows Linux to provide a device tree, which is necessary for SOCs that
+ * don't provide a useful one on their own.
+ */
+struct soc_builtin_dtb_table_entry {
+	long vendor_id;
+	long arch_id;
+	long imp_id;
+	void *(*dtb_func)(void);
+};
+
+#define SOC_BUILTIN_DTB_DECLARE(name, vendor, impl, arch, dtb)		\
+	static __init __used						\
+	void *__soc_builtin_dtb_f__##name(void) { return dtb; }		\
+									\
+	static const struct soc_builtin_dtb_table_entry			\
+	__soc_builtin_dtb__##name __used				\
+	__section(__soc_builtin_dtb_table)				\
+	= {								\
+		.vendor_id = vendor,					\
+		.arch_id   = arch,					\
+		.imp_id    = impl,					\
+		.dtb_func  = __soc_builtin_dtb_f__##name,		\
+	}
+
+void *soc_lookup_builtin_dtb(long vendor_id, long arch_id, long imp_id);
 
 #endif
diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile
index 86c83081044f..f40516b6e6c1 100644
--- a/arch/riscv/kernel/Makefile
+++ b/arch/riscv/kernel/Makefile
@@ -52,4 +52,7 @@  obj-$(CONFIG_SMP) += cpu_ops_sbi.o
 endif
 obj-$(CONFIG_HOTPLUG_CPU)	+= cpu-hotplug.o
 
+obj-$(CONFIG_BUILTIN_DTB) += builtin-dtb.o
+obj-$(CONFIG_BUILTIN_DTB) += builtin-dtb-table.o
+
 clean:
diff --git a/arch/riscv/kernel/builtin-dtb-table.c b/arch/riscv/kernel/builtin-dtb-table.c
new file mode 100644
index 000000000000..7ad6fe93b8a6
--- /dev/null
+++ b/arch/riscv/kernel/builtin-dtb-table.c
@@ -0,0 +1,6 @@ 
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Table of built-in device trees
+ */
+
+#include <asm/soc.h>
diff --git a/arch/riscv/kernel/builtin-dtb.S b/arch/riscv/kernel/builtin-dtb.S
new file mode 100644
index 000000000000..3d459ad86948
--- /dev/null
+++ b/arch/riscv/kernel/builtin-dtb.S
@@ -0,0 +1,3 @@ 
+/* SPDX-License-Identifier: GPL-2.0 */
+
+.section .dtb.init.rodata,"a"
diff --git a/arch/riscv/kernel/soc.c b/arch/riscv/kernel/soc.c
index c32e7c8e7870..3a05110a2447 100644
--- a/arch/riscv/kernel/soc.c
+++ b/arch/riscv/kernel/soc.c
@@ -32,3 +32,28 @@  void __init soc_early_init(void)
 		}
 	}
 }
+
+static int builtin_dtb_match(long vendor_id, long arch_id, long imp_id,
+			     const struct soc_builtin_dtb_table_entry *entry)
+{
+	return (entry->vendor_id == vendor_id)
+	       && (entry->arch_id == arch_id)
+	       && (entry->imp_id == imp_id);
+}
+
+extern unsigned long __soc_builtin_dtb_table_start;
+extern unsigned long __soc_builtin_dtb_table_end;
+
+void * __init soc_lookup_builtin_dtb(long vendor_id, long arch_id, long imp_id)
+{
+	const struct soc_builtin_dtb_table_entry *s;
+
+	for (s = (void *)&__soc_early_init_table_start;
+	     (void *)s < (void *)&__soc_early_init_table_end; s++) {
+		if (builtin_dtb_match(vendor_id, arch_id, imp_id, s)) {
+			return s->dtb_func();
+		}
+	}
+
+	return NULL;
+}
diff --git a/arch/riscv/kernel/vmlinux.lds.S b/arch/riscv/kernel/vmlinux.lds.S
index 0339b6bbe11a..e6f8016b366a 100644
--- a/arch/riscv/kernel/vmlinux.lds.S
+++ b/arch/riscv/kernel/vmlinux.lds.S
@@ -34,6 +34,11 @@  SECTIONS
 		KEEP(*(__soc_early_init_table))
 		__soc_early_init_table_end = .;
 	}
+	__soc_builtin_dtb_table : {
+		__soc_builtin_dtb_table_start = .;
+		KEEP(*(__soc_builtin_dtb_table))
+		__soc_builtin_dtb_table_end = .;
+	}
 	/* we have to discard exit text and such at runtime, not link time */
 	.exit.text :
 	{
diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
index 0c625a5e98db..ad07d931bca8 100644
--- a/arch/riscv/mm/init.c
+++ b/arch/riscv/mm/init.c
@@ -17,6 +17,7 @@ 
 #include <asm/fixmap.h>
 #include <asm/tlbflush.h>
 #include <asm/sections.h>
+#include <asm/soc.h>
 #include <asm/pgtable.h>
 #include <asm/io.h>
 
@@ -492,7 +493,20 @@  void free_initmem(void)
 #else
 asmlinkage void __init setup_vm(uintptr_t dtb_pa)
 {
+#ifdef CONFIG_RISCV_M_MODE
+	void *builtin_dtb;
+	long mvendorid, marchid, mimpid;
+
+	__asm__ ("csrr %0, mvendorid" : "=r"(mvendorid));
+	__asm__ ("csrr %0, marchid" : "=r"(marchid));
+	__asm__ ("csrr %0, mimpid" : "=r"(mimpid));
+
+	builtin_dtb = soc_lookup_builtin_dtb(mvendorid, marchid, mimpid);
+	if (builtin_dtb != NULL)
+		dtb_early_va = builtin_dtb;
+#else
 	dtb_early_va = (void *)dtb_pa;
+#endif
 }
 
 static inline void setup_vm_final(void)