xref: /linux/arch/riscv/kernel/pi/cmdline_early.c (revision 84b9b44b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/types.h>
3 #include <linux/init.h>
4 #include <linux/libfdt.h>
5 #include <linux/string.h>
6 #include <asm/pgtable.h>
7 #include <asm/setup.h>
8 
9 static char early_cmdline[COMMAND_LINE_SIZE];
10 
11 /*
12  * Declare the functions that are exported (but prefixed) here so that LLVM
13  * does not complain it lacks the 'static' keyword (which, if added, makes
14  * LLVM complain because the function is actually unused in this file).
15  */
16 u64 set_satp_mode_from_cmdline(uintptr_t dtb_pa);
17 
18 static char *get_early_cmdline(uintptr_t dtb_pa)
19 {
20 	const char *fdt_cmdline = NULL;
21 	unsigned int fdt_cmdline_size = 0;
22 	int chosen_node;
23 
24 	if (!IS_ENABLED(CONFIG_CMDLINE_FORCE)) {
25 		chosen_node = fdt_path_offset((void *)dtb_pa, "/chosen");
26 		if (chosen_node >= 0) {
27 			fdt_cmdline = fdt_getprop((void *)dtb_pa, chosen_node,
28 						  "bootargs", NULL);
29 			if (fdt_cmdline) {
30 				fdt_cmdline_size = strlen(fdt_cmdline);
31 				strscpy(early_cmdline, fdt_cmdline,
32 					COMMAND_LINE_SIZE);
33 			}
34 		}
35 	}
36 
37 	if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) ||
38 	    IS_ENABLED(CONFIG_CMDLINE_FORCE) ||
39 	    fdt_cmdline_size == 0 /* CONFIG_CMDLINE_FALLBACK */) {
40 		strncat(early_cmdline, CONFIG_CMDLINE,
41 			COMMAND_LINE_SIZE - fdt_cmdline_size);
42 	}
43 
44 	return early_cmdline;
45 }
46 
47 static u64 match_noXlvl(char *cmdline)
48 {
49 	if (strstr(cmdline, "no4lvl"))
50 		return SATP_MODE_48;
51 	else if (strstr(cmdline, "no5lvl"))
52 		return SATP_MODE_57;
53 
54 	return 0;
55 }
56 
57 u64 set_satp_mode_from_cmdline(uintptr_t dtb_pa)
58 {
59 	char *cmdline = get_early_cmdline(dtb_pa);
60 
61 	return match_noXlvl(cmdline);
62 }
63