1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * The 'sbi' command displays information about the SBI implementation.
4  *
5  * Copyright (c) 2020, Heinrich Schuchardt <xypron.glpk@gmx.de>
6  */
7 
8 #include <command.h>
9 #include <asm/sbi.h>
10 
11 struct sbi_imp {
12 	const long id;
13 	const char *name;
14 };
15 
16 struct sbi_ext {
17 	const u32 id;
18 	const char *name;
19 };
20 
21 static struct sbi_imp implementations[] = {
22 	{ 0, "Berkeley Boot Loader (BBL)" },
23 	{ 1, "OpenSBI" },
24 	{ 2, "Xvisor" },
25 	{ 3, "KVM" },
26 	{ 4, "RustSBI" },
27 	{ 5, "Diosix" },
28 	{ 6, "Coffer" },
29 	{ 7, "Xen Project" },
30 	{ 8, "PolarFire Hart Software Services" },
31 	{ 9, "coreboot" },
32 	{ 10, "oreboot" },
33 	{ 11, "bhyve" },
34 };
35 
36 static struct sbi_ext extensions[] = {
37 	{ SBI_EXT_0_1_SET_TIMER,	      "Set Timer" },
38 	{ SBI_EXT_0_1_CONSOLE_PUTCHAR,	      "Console Putchar" },
39 	{ SBI_EXT_0_1_CONSOLE_GETCHAR,	      "Console Getchar" },
40 	{ SBI_EXT_0_1_CLEAR_IPI,	      "Clear IPI" },
41 	{ SBI_EXT_0_1_SEND_IPI,		      "Send IPI" },
42 	{ SBI_EXT_0_1_REMOTE_FENCE_I,	      "Remote FENCE.I" },
43 	{ SBI_EXT_0_1_REMOTE_SFENCE_VMA,      "Remote SFENCE.VMA" },
44 	{ SBI_EXT_0_1_REMOTE_SFENCE_VMA_ASID, "Remote SFENCE.VMA with ASID" },
45 	{ SBI_EXT_0_1_SHUTDOWN,		      "System Shutdown" },
46 	{ SBI_EXT_BASE,			      "SBI Base Functionality" },
47 	{ SBI_EXT_TIME,			      "Timer Extension" },
48 	{ SBI_EXT_IPI,			      "IPI Extension" },
49 	{ SBI_EXT_RFENCE,		      "RFENCE Extension" },
50 	{ SBI_EXT_HSM,			      "Hart State Management Extension" },
51 	{ SBI_EXT_SRST,			      "System Reset Extension" },
52 	{ SBI_EXT_PMU,			      "Performance Monitoring Unit Extension" },
53 	{ SBI_EXT_DBCN,			      "Debug Console Extension" },
54 	{ SBI_EXT_SUSP,			      "System Suspend Extension" },
55 	{ SBI_EXT_CPPC,			      "Collaborative Processor Performance Control Extension" },
56 	{ SBI_EXT_NACL,			      "Nested Acceleration Extension" },
57 	{ SBI_EXT_STA,			      "Steal-time Accounting Extension" },
58 	{ SBI_EXT_SSE,			      "Supervisor Software Events" },
59 	{ SBI_EXT_FWFT,			      "Firmware Features Extension" },
60 	{ SBI_EXT_DBTR,			      "Debug Triggers Extension" },
61 	{ SBI_EXT_MPXY,			      "Message Proxy Extension" },
62 };
63 
do_sbi(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])64 static int do_sbi(struct cmd_tbl *cmdtp, int flag, int argc,
65 		  char *const argv[])
66 {
67 	int i, impl_id;
68 	long ret;
69 	long mvendorid, marchid, mimpid;
70 
71 	ret = sbi_get_spec_version();
72 	if (ret < 0) {
73 		printf("No SBI 0.2+\n");
74 		return CMD_RET_FAILURE;
75 	}
76 	printf("SBI %ld.%ld", ret >> 24, ret & 0xffffff);
77 	impl_id = sbi_get_impl_id();
78 	if (impl_id >= 0) {
79 		for (i = 0; i < ARRAY_SIZE(implementations); ++i) {
80 			if (impl_id == implementations[i].id) {
81 				long vers;
82 
83 				printf("\n%s ", implementations[i].name);
84 				ret = sbi_get_impl_version(&vers);
85 				if (ret < 0)
86 					break;
87 				switch (impl_id) {
88 				case 1: /* OpenSBI */
89 				case 8: /* PolarFire Hart Software Services */
90 					printf("%ld.%ld",
91 					       vers >> 16, vers & 0xffff);
92 					break;
93 				case 3: /* KVM */
94 				case 4: /* RustSBI */
95 					printf("%ld.%ld.%ld",
96 					       vers >> 16,
97 					       (vers >> 8) & 0xff,
98 					       vers & 0xff);
99 					break;
100 				default:
101 					printf("0x%lx", vers);
102 					break;
103 				}
104 				break;
105 			}
106 		}
107 		if (i == ARRAY_SIZE(implementations))
108 			printf("\nUnknown implementation ID 0x%x", impl_id);
109 	}
110 	printf("\nMachine:\n");
111 	ret = sbi_get_mvendorid(&mvendorid);
112 	if (!ret)
113 		printf("  Vendor ID %lx\n", mvendorid);
114 	ret = sbi_get_marchid(&marchid);
115 	if (!ret)
116 		printf("  Architecture ID %lx\n", marchid);
117 	ret = sbi_get_mimpid(&mimpid);
118 	if (!ret)
119 		printf("  Implementation ID %lx\n", mimpid);
120 	printf("Extensions:\n");
121 	for (i = 0; i < ARRAY_SIZE(extensions); ++i) {
122 		ret = sbi_probe_extension(extensions[i].id);
123 		if (ret > 0)
124 			printf("  %s\n", extensions[i].name);
125 	}
126 	return 0;
127 }
128 
129 U_BOOT_LONGHELP(sbi,
130 	"- display SBI spec version, implementation, and available extensions");
131 
132 U_BOOT_CMD_COMPLETE(
133 	sbi, 1, 0, do_sbi,
134 	"display SBI information",
135 	sbi_help_text, NULL
136 );
137