@@ -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"
@@ -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
@@ -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:
new file mode 100644
@@ -0,0 +1,6 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Table of built-in device trees
+ */
+
+#include <asm/soc.h>
new file mode 100644
@@ -0,0 +1,3 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+.section .dtb.init.rodata,"a"
@@ -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;
+}
@@ -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 :
{
@@ -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)