Skip to content

Commit 335b139

Browse files
damien-lemoalpalmer-dabbelt
authored andcommitted
riscv: Add SOC early init support
Add a mechanism for early SoC initialization for platforms that need additional hardware initialization not possible through the regular device tree and drivers mechanism. With this, a SoC specific initialization function can be called very early, before DTB parsing is done by parse_dtb() in Linux RISC-V kernel setup code. This can be very useful for early hardware initialization for No-MMU kernels booted directly in M-mode because it is quite likely that no other booting stage exist prior to the No-MMU kernel. Example use of a SoC early initialization is as follows: static void vendor_abc_early_init(const void *fdt) { /* * some early init code here that can use simple matches * against the flat device tree file. */ } SOC_EARLY_INIT_DECLARE("vendor,abc", abc_early_init); This early initialization function is executed only if the flat device tree for the board has a 'compatible = "vendor,abc"' entry; Signed-off-by: Damien Le Moal <[email protected]> Signed-off-by: Anup Patel <[email protected]> Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 956d705 commit 335b139

File tree

5 files changed

+59
-0
lines changed

5 files changed

+59
-0
lines changed

arch/riscv/include/asm/soc.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later */
2+
/*
3+
* Copyright (C) 2020 Western Digital Corporation or its affiliates.
4+
*/
5+
6+
#ifndef _ASM_RISCV_SOC_H
7+
#define _ASM_RISCV_SOC_H
8+
9+
#include <linux/of.h>
10+
#include <linux/linkage.h>
11+
#include <linux/types.h>
12+
13+
#define SOC_EARLY_INIT_DECLARE(name, compat, fn) \
14+
static const struct of_device_id __soc_early_init__##name \
15+
__used __section(__soc_early_init_table) \
16+
= { .compatible = compat, .data = fn }
17+
18+
void soc_early_init(void);
19+
20+
extern unsigned long __soc_early_init_table_start;
21+
extern unsigned long __soc_early_init_table_end;
22+
23+
#endif

arch/riscv/kernel/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ endif
1111
extra-y += head.o
1212
extra-y += vmlinux.lds
1313

14+
obj-y += soc.o
1415
obj-y += cpu.o
1516
obj-y += cpufeature.o
1617
obj-y += entry.o

arch/riscv/kernel/head.S

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ clear_bss_done:
234234
call kasan_early_init
235235
#endif
236236
/* Start the kernel */
237+
call soc_early_init
237238
call parse_dtb
238239
tail start_kernel
239240

arch/riscv/kernel/soc.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Copyright (C) 2020 Western Digital Corporation or its affiliates.
4+
*/
5+
#include <linux/init.h>
6+
#include <linux/libfdt.h>
7+
#include <asm/pgtable.h>
8+
#include <asm/soc.h>
9+
10+
/*
11+
* This is called extremly early, before parse_dtb(), to allow initializing
12+
* SoC hardware before memory or any device driver initialization.
13+
*/
14+
void __init soc_early_init(void)
15+
{
16+
void (*early_fn)(const void *fdt);
17+
const struct of_device_id *s;
18+
const void *fdt = dtb_early_va;
19+
20+
for (s = (void *)&__soc_early_init_table_start;
21+
(void *)s < (void *)&__soc_early_init_table_end; s++) {
22+
if (!fdt_node_check_compatible(fdt, 0, s->compatible)) {
23+
early_fn = s->data;
24+
early_fn(fdt);
25+
return;
26+
}
27+
}
28+
}

arch/riscv/kernel/vmlinux.lds.S

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ SECTIONS
2828
__init_begin = .;
2929
INIT_TEXT_SECTION(PAGE_SIZE)
3030
INIT_DATA_SECTION(16)
31+
. = ALIGN(8);
32+
__soc_early_init_table : {
33+
__soc_early_init_table_start = .;
34+
KEEP(*(__soc_early_init_table))
35+
__soc_early_init_table_end = .;
36+
}
3137
/* we have to discard exit text and such at runtime, not link time */
3238
.exit.text :
3339
{

0 commit comments

Comments
 (0)