More K210 Support
Subsystems (2)
Palmer Dabbelt
maintainer* Builds a table of device trees, keyed by mvendorid/marchid/mimpid. This allows multiple device trees to be built into the kernel. I don't really like maintaining the two lists (one in C and one in assembly) or having that function in the table, but it's the best I could come up with.
* "handles" PMP traps by just skipping the PMP setup phase.
I don't actually have a K210 so I can't test any of this. I also couldn't find the K210 identifiers listed anywhere online, so someone will have to dig them out of the board.
I also didn't spend any time thinking through how we free these device trees, but given that .dtb.init.rodata already exists as a section I'm just guessing that's were we're supposed to put them.
Palmer Dabbelt
maintainerPATCH 1/3 RISC-V: Allow device trees to be built into the kernel
From: Palmer Dabbelt <[email protected]>
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.
--- 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
arch/riscv/Kconfig +5 -0
@@ -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"
arch/riscv/include/asm/soc.h +27 -0
@@ -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
arch/riscv/kernel/Makefile +3 -0
@@ -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:
arch/riscv/kernel/builtin-dtb-table.c new file +6 -0
@@ -0,0 +1,6 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Table of built-in device trees +*/ + +#include <asm/soc.h>
arch/riscv/kernel/builtin-dtb.S new file +3 -0
@@ -0,0 +1,3 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +.section .dtb.init.rodata,"a"
arch/riscv/kernel/soc.c +25 -0
@@ -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 ; +}
arch/riscv/kernel/vmlinux.lds.S +5 -0
@@ -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 : {
arch/riscv/mm/init.c +14 -0
@@ -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 )
Signature · 1 line
2.26.0.292.g33ef6b2f38-goog
Palmer Dabbelt
maintainerPATCH 2/3 RISC-V: K210: Add a built-in device tree
In reply to Palmer DabbeltFrom: Palmer Dabbelt <[email protected]>
The K210's bootloader doesn't provide a device tree, so we must provide our own.
FIXME: I don't actually know the unique IDs on the K210.
--- arch/riscv/Kconfig.socs | 13 ++++++++++++- arch/riscv/boot/dts/kendryte/Makefile | 2 +- arch/riscv/kernel/builtin-dtb-table.c | 5 +++++ arch/riscv/kernel/builtin-dtb.S | 6 ++++++ 4 files changed, 24 insertions(+), 2 deletions(-)
arch/riscv/Kconfig.socs +12 -1
@@ -37,11 +37,22 @@ config SOC_VIRT config SOC_KENDRYTE bool "Kendryte K210 SoC" depends on !MMU - select BUILTIN_DTB select SERIAL_SIFIVE if TTY select SERIAL_SIFIVE_CONSOLE if TTY select SIFIVE_PLIC + select SOC_KENDRYTE_K210_DTB_BUILTIN help This enables support for Kendryte K210 SoC platform hardware. +config SOC_KENDRYTE_K210_DTB + def_bool y + depends on OF + +config SOC_KENDRYTE_K210_DTB_BUILTIN + bool "Builtin device tree for the Kendryte K210" + depends on BUILTIN_DTB + select SOC_KENDRYTE_K210_DTB + help + Builds a device tree for the Kendryte K210 into the Linux image. + endmenu
arch/riscv/boot/dts/kendryte/Makefile +1 -1
@@ -1,2 +1,2 @@ # SPDX-License-Identifier: GPL-2.0 -dtb-$(CONFIG_SOC_KENDRYTE) += k210.dtb +dtb-$(CONFIG_SOC_KENDRYTE_K210_DTB) += k210.dtb
arch/riscv/kernel/builtin-dtb-table.c +5 -0
@@ -4,3 +4,8 @@*/ #include <asm/soc.h> + +#ifdef CONFIG_SOC_KENDRYTE_K210_DTB_BUILTIN +extern void *kendryte_k210_dtb; +SOC_BUILTIN_DTB_DECLARE (kendryte_k210,0x0 ,0x0 ,0x0 , kendryte_k210_dtb); +#endif
arch/riscv/kernel/builtin-dtb.S +6 -0
@@ -1,3 +1,9 @@ /* SPDX-License-Identifier: GPL-2.0 */ .section .dtb.init.rodata,"a" + +#ifdef CONFIG_SOC_KENDRYTE_K210_DTB_BUILTIN +.global kendryte_k210_dtb +kendryte_k210_dtb: + .incbin "arch/riscv/boot/dts/kendryte/k210.dtb" +#endif
Signature · 1 line
2.26.0.292.g33ef6b2f38-goog
Re: [PATCH 2/3] RISC-V: K210: Add a built-in device tree
Quoted 6 lines · Palmer Dabbelt
From: Palmer Dabbelt <[email protected]>
The K210's bootloader doesn't provide a device tree, so we must provide our own.
FIXME: I don't actually know the unique IDs on the K210.
OK. I will test.
Quoted 14 lines
Signed-off-by Palmer Dabbelt <[email protected]>--- arch/riscv/Kconfig.socs | 13 ++++++++++++- arch/riscv/boot/dts/kendryte/Makefile | 2 +- arch/riscv/kernel/builtin-dtb-table.c | 5 +++++ arch/riscv/kernel/builtin-dtb.S | 6 ++++++ 4 files changed, 24 insertions(+), 2 deletions(-)arch/riscv/Kconfig.socs +0
-0
arch/riscv/Kconfig.socs config SOC_VIRT · line 40
Quoted 4 lines
arch/riscv/Kconfig.socs +0
-1@@ -37,11 +37,22 @@ config SOC_VIRT config SOC_KENDRYTE bool "Kendryte K210 SoC" depends on !MMU - select BUILTIN_DTB
Since SOC_KENDRYTE_K210_DTB_BUILTIN depends on BUILTIN_DTB, you need to keep this select, no ?
arch/riscv/Kconfig.socs config SOC_VIRT · line 49
Quoted 10 lines
arch/riscv/Kconfig.socs +4
-0select SERIAL_SIFIVE if TTY select SERIAL_SIFIVE_CONSOLE if TTY select SIFIVE_PLIC + select SOC_KENDRYTE_K210_DTB_BUILTIN help This enables support for Kendryte K210 SoC platform hardware. +config SOC_KENDRYTE_K210_DTB + def_bool y + depends on OF
This one is used only in the makefile. Why not use SOC_KENDRYTE_K210_DTB_BUILTIN directly and drop this SOC_KENDRYTE_K210_DTB ?
Quoted 45 lines
arch/riscv/Kconfig.socs +8
-0+ +config SOC_KENDRYTE_K210_DTB_BUILTIN + bool "Builtin device tree for the Kendryte K210" + depends on BUILTIN_DTB + select SOC_KENDRYTE_K210_DTB + help + Builds a device tree for the Kendryte K210 into the Linux image. + endmenuarch/riscv/boot/dts/kendryte/Makefile +1
-1@@ -1,2 +1,2 @@ # SPDX-License-Identifier: GPL-2.0 -dtb-$(CONFIG_SOC_KENDRYTE) += k210.dtb +dtb-$(CONFIG_SOC_KENDRYTE_K210_DTB) += k210.dtbarch/riscv/kernel/builtin-dtb-table.c +5
-0@@ -4,3 +4,8 @@*/ #include <asm/soc.h> + +#ifdef CONFIG_SOC_KENDRYTE_K210_DTB_BUILTIN +extern void *kendryte_k210_dtb; +SOC_BUILTIN_DTB_DECLARE (kendryte_k210,0x0 ,0x0 ,0x0 , kendryte_k210_dtb); +#endif arch/riscv/kernel/builtin-dtb.S +6
-0@@ -1,3 +1,9 @@ /* SPDX-License-Identifier: GPL-2.0 */ .section .dtb.init.rodata,"a" + +#ifdef CONFIG_SOC_KENDRYTE_K210_DTB_BUILTIN +.global kendryte_k210_dtb +kendryte_k210_dtb: + .incbin "arch/riscv/boot/dts/kendryte/k210.dtb" +#endif
Signature · 2 lines
Damien Le Moal Western Digital Research
Palmer Dabbelt
maintainerPATCH 3/3 RISC-V: Skip setting up PMPs on traps
In reply to Palmer DabbeltFrom: Palmer Dabbelt <[email protected]>
The RISC-V ISA manual says that PMPs are WARL, but it appears the K210 doesn't implement them and instead traps on the unsupported accesses. This patch handles those traps by just skipping the PMP initialization entirely, under the theory that machines that trap on PMP accesses must allow memory accesses as otherwise they're pretty useless.
--- arch/riscv/kernel/head.S | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
arch/riscv/kernel/head.S +10 -1
@@ -161,11 +161,20 @@ ENTRY(_start_kernel) /* Reset all registers except ra, a0, a1 */ call reset_regs - /* Setup a PMP to permit access to all of memory. */ + /* + * Setup a PMP to permit access to all of memory. Some machines may + * not implement PMPs, so we set up a quick trap handler to just skip + * touching the PMPs on any trap. + */ + la a0, pmp_done + csrw CSR_TVEC, a0 + li a0, -1 csrw CSR_PMPADDR0, a0 li a0, (PMP_A_NAPOT | PMP_R | PMP_W | PMP_X) csrw CSR_PMPCFG0, a0 +.align 2 +pmp_done: /* * The hartid in a0 is expected later on, and we have no firmware
Signature · 1 line
2.26.0.292.g33ef6b2f38-goog
Re: [PATCH 3/3] RISC-V: Skip setting up PMPs on traps
Quoted 9 lines · Palmer Dabbelt
From: Palmer Dabbelt <[email protected]>
The RISC-V ISA manual says that PMPs are WARL, but it appears the K210 doesn't implement them and instead traps on the unsupported accesses. This patch handles those traps by just skipping the PMP initialization entirely, under the theory that machines that trap on PMP accesses must allow memory accesses as otherwise they're pretty useless.
Signed-off-by Palmer Dabbelt <[email protected]>
This one works:
Testing/hacking the other 2 patches of the series now.
Quoted 31 lines
--- arch/riscv/kernel/head.S | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)arch/riscv/kernel/head.S +10
-1@@ -161,11 +161,20 @@ ENTRY(_start_kernel) /* Reset all registers except ra, a0, a1 */ call reset_regs - /* Setup a PMP to permit access to all of memory. */ + /* + * Setup a PMP to permit access to all of memory. Some machines may + * not implement PMPs, so we set up a quick trap handler to just skip + * touching the PMPs on any trap. + */ + la a0, pmp_done + csrw CSR_TVEC, a0 + li a0, -1 csrw CSR_PMPADDR0, a0 li a0, (PMP_A_NAPOT | PMP_R | PMP_W | PMP_X) csrw CSR_PMPCFG0, a0 +.align 2 +pmp_done: /* * The hartid in a0 is expected later on, and we have no firmware
Signature · 2 lines
Damien Le Moal Western Digital Research
Quoted 6 lines · Palmer Dabbelt
* Builds a table of device trees, keyed by mvendorid/marchid/mimpid. This allows multiple device trees to be built into the kernel. I don't really like maintaining the two lists (one in C and one in assembly) or having that function in the table, but it's the best I could come up with.
* "handles" PMP traps by just skipping the PMP setup phase.
Quoted 7 lines
I don't actually have a K210 so I can't test any of this. I also couldn't find the K210 identifiers listed anywhere online, so someone will have to dig them out of the board.
I also didn't spend any time thinking through how we free these device trees, but given that .dtb.init.rodata already exists as a section I'm just guessing that's were we're supposed to put them.
I cannot test anything yet. Something funky is going on with boot objcopy/Image
size: I end up with an Image file (and loader/loader.bin) that are 8.5MB instead of the 1.4MB I had before pulling in Linus tree. rc1 as of this morning still shows the same problem. vmlinux is 1.8MB vs 1.9 MB before.
To check if my toolchain is broken, I recompiled the 5.6 tree I used for developing the series and I end up with a loader.bin file of 1.4 MB. All looking good. But there are changes to the vmlinux elf section headers which likely cause the huge final size I am seeing. Will try to dig into this.
Signature · 2 lines
Damien Le Moal Western Digital Research
Palmer Dabbelt
maintainerQuoted 20 lines · Damien Le Moal
Quoted 7 lines · Palmer Dabbelt
* Builds a table of device trees, keyed by mvendorid/marchid/mimpid. This allows multiple device trees to be built into the kernel. I don't really like maintaining the two lists (one in C and one in assembly) or having that function in the table, but it's the best I could come up with.
* "handles" PMP traps by just skipping the PMP setup phase.Quoted 7 lines
I don't actually have a K210 so I can't test any of this. I also couldn't find the K210 identifiers listed anywhere online, so someone will have to dig them out of the board.
I also didn't spend any time thinking through how we free these device trees, but given that .dtb.init.rodata already exists as a section I'm just guessing that's were we're supposed to put them.
I cannot test anything yet. Something funky is going on with boot objcopy/Image
size: I end up with an Image file (and loader/loader.bin) that are 8.5MB instead of the 1.4MB I had before pulling in Linus tree. rc1 as of this morning still shows the same problem. vmlinux is 1.8MB vs 1.9 MB before.
Quoted 4 lines
To check if my toolchain is broken, I recompiled the 5.6 tree I used for developing the series and I end up with a loader.bin file of 1.4 MB. All looking good. But there are changes to the vmlinux elf section headers which likely cause the huge final size I am seeing. Will try to dig into this.
Thanks!
Quoted 21 lines · Palmer Dabbelt
Quoted 20 lines · Damien Le Moal
Quoted 7 lines · Palmer Dabbelt
* Builds a table of device trees, keyed by mvendorid/marchid/mimpid. This allows multiple device trees to be built into the kernel. I don't really like maintaining the two lists (one in C and one in assembly) or having that function in the table, but it's the best I could come up with.
* "handles" PMP traps by just skipping the PMP setup phase.Quoted 7 lines
I don't actually have a K210 so I can't test any of this. I also couldn't find the K210 identifiers listed anywhere online, so someone will have to dig them out of the board.
I also didn't spend any time thinking through how we free these device trees, but given that .dtb.init.rodata already exists as a section I'm just guessing that's were we're supposed to put them.
I cannot test anything yet. Something funky is going on with boot objcopy/Image
size: I end up with an Image file (and loader/loader.bin) that are 8.5MB instead of the 1.4MB I had before pulling in Linus tree. rc1 as of this morning still shows the same problem. vmlinux is 1.8MB vs 1.9 MB before.
Quoted 6 lines
Quoted 4 lines
To check if my toolchain is broken, I recompiled the 5.6 tree I used for developing the series and I end up with a loader.bin file of 1.4 MB. All looking good. But there are changes to the vmlinux elf section headers which likely cause the huge final size I am seeing. Will try to dig into this.
Thanks!
I sent you fixed patches already. Please review them.
The large image size was due to the strict RWX alignment of elf sections, which can be disabled for no mmu case since there is no RW protection anyway.
Signature · 2 lines
Damien Le Moal Western Digital Research
Palmer Dabbelt
maintainerQuoted 27 lines · Damien Le Moal
Quoted 26 lines · Palmer Dabbelt
Quoted 24 lines · Damien Le Moal
Quoted 7 lines · Palmer Dabbelt
* Builds a table of device trees, keyed by mvendorid/marchid/mimpid. This allows multiple device trees to be built into the kernel. I don't really like maintaining the two lists (one in C and one in assembly) or having that function in the table, but it's the best I could come up with.
* "handles" PMP traps by just skipping the PMP setup phase.Quoted 7 lines
I don't actually have a K210 so I can't test any of this. I also couldn't find the K210 identifiers listed anywhere online, so someone will have to dig them out of the board.
I also didn't spend any time thinking through how we free these device trees, but given that .dtb.init.rodata already exists as a section I'm just guessing that's were we're supposed to put them.
I cannot test anything yet. Something funky is going on with boot objcopy/Image
size: I end up with an Image file (and loader/loader.bin) that are 8.5MB instead of the 1.4MB I had before pulling in Linus tree. rc1 as of this morning still shows the same problem. vmlinux is 1.8MB vs 1.9 MB before.To check if my toolchain is broken, I recompiled the 5.6 tree I used for developing the series and I end up with a loader.bin file of 1.4 MB. All looking good. But there are changes to the vmlinux elf section headers which likely cause the huge final size I am seeing. Will try to dig into this.
Quoted 5 lines
Quoted 1 line
Thanks!
I sent you fixed patches already. Please review them.
The large image size was due to the strict RWX alignment of elf sections, which can be disabled for no mmu case since there is no RW protection anyway.
That RWX thing didn't make it to my inbox for some reason, but Atish pointed it out in another thread. It's on for-next now. Thanks!
Quoted 6 lines · Palmer Dabbelt
* Builds a table of device trees, keyed by mvendorid/marchid/mimpid. This allows multiple device trees to be built into the kernel. I don't really like maintaining the two lists (one in C and one in assembly) or having that function in the table, but it's the best I could come up with.
* "handles" PMP traps by just skipping the PMP setup phase.
Quoted 10 lines
I don't actually have a K210 so I can't test any of this. I also couldn't find the K210 identifiers listed anywhere online, so someone will have to dig them out of the board.
I also didn't spend any time thinking through how we free these device trees, but given that .dtb.init.rodata already exists as a section I'm just guessing that's were we're supposed to put them.
(replying to your original post instead of my update since infradead does not send me back my posts).
Palmer, have you had a chance to review the updated patches I sent for the builtin dtb ?
Signature · 2 lines
Damien Le Moal Western Digital Research