xref: /openbmc/linux/arch/riscv/kernel/cpu.c (revision e802ca75)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012 Regents of the University of California
4  */
5 
6 #include <linux/init.h>
7 #include <linux/seq_file.h>
8 #include <linux/of.h>
9 #include <asm/hwcap.h>
10 #include <asm/smp.h>
11 #include <asm/pgtable.h>
12 
13 /*
14  * Returns the hart ID of the given device tree node, or -ENODEV if the node
15  * isn't an enabled and valid RISC-V hart node.
16  */
17 int riscv_of_processor_hartid(struct device_node *node)
18 {
19 	const char *isa;
20 	u32 hart;
21 
22 	if (!of_device_is_compatible(node, "riscv")) {
23 		pr_warn("Found incompatible CPU\n");
24 		return -ENODEV;
25 	}
26 
27 	hart = of_get_cpu_hwid(node, 0);
28 	if (hart == ~0U) {
29 		pr_warn("Found CPU without hart ID\n");
30 		return -ENODEV;
31 	}
32 
33 	if (!of_device_is_available(node)) {
34 		pr_info("CPU with hartid=%d is not available\n", hart);
35 		return -ENODEV;
36 	}
37 
38 	if (of_property_read_string(node, "riscv,isa", &isa)) {
39 		pr_warn("CPU with hartid=%d has no \"riscv,isa\" property\n", hart);
40 		return -ENODEV;
41 	}
42 	if (isa[0] != 'r' || isa[1] != 'v') {
43 		pr_warn("CPU with hartid=%d has an invalid ISA of \"%s\"\n", hart, isa);
44 		return -ENODEV;
45 	}
46 
47 	return hart;
48 }
49 
50 /*
51  * Find hart ID of the CPU DT node under which given DT node falls.
52  *
53  * To achieve this, we walk up the DT tree until we find an active
54  * RISC-V core (HART) node and extract the cpuid from it.
55  */
56 int riscv_of_parent_hartid(struct device_node *node)
57 {
58 	for (; node; node = node->parent) {
59 		if (of_device_is_compatible(node, "riscv"))
60 			return riscv_of_processor_hartid(node);
61 	}
62 
63 	return -1;
64 }
65 
66 #ifdef CONFIG_PROC_FS
67 #define __RISCV_ISA_EXT_DATA(UPROP, EXTID) \
68 	{							\
69 		.uprop = #UPROP,				\
70 		.isa_ext_id = EXTID,				\
71 	}
72 /*
73  * Here are the ordering rules of extension naming defined by RISC-V
74  * specification :
75  * 1. All extensions should be separated from other multi-letter extensions
76  *    by an underscore.
77  * 2. The first letter following the 'Z' conventionally indicates the most
78  *    closely related alphabetical extension category, IMAFDQLCBKJTPVH.
79  *    If multiple 'Z' extensions are named, they should be ordered first
80  *    by category, then alphabetically within a category.
81  * 3. Standard supervisor-level extensions (starts with 'S') should be
82  *    listed after standard unprivileged extensions.  If multiple
83  *    supervisor-level extensions are listed, they should be ordered
84  *    alphabetically.
85  * 4. Non-standard extensions (starts with 'X') must be listed after all
86  *    standard extensions. They must be separated from other multi-letter
87  *    extensions by an underscore.
88  */
89 static struct riscv_isa_ext_data isa_ext_arr[] = {
90 	__RISCV_ISA_EXT_DATA(sscofpmf, RISCV_ISA_EXT_SSCOFPMF),
91 	__RISCV_ISA_EXT_DATA("", RISCV_ISA_EXT_MAX),
92 };
93 
94 static void print_isa_ext(struct seq_file *f)
95 {
96 	struct riscv_isa_ext_data *edata;
97 	int i = 0, arr_sz;
98 
99 	arr_sz = ARRAY_SIZE(isa_ext_arr) - 1;
100 
101 	/* No extension support available */
102 	if (arr_sz <= 0)
103 		return;
104 
105 	for (i = 0; i <= arr_sz; i++) {
106 		edata = &isa_ext_arr[i];
107 		if (!__riscv_isa_extension_available(NULL, edata->isa_ext_id))
108 			continue;
109 		seq_printf(f, "_%s", edata->uprop);
110 	}
111 }
112 
113 /*
114  * These are the only valid base (single letter) ISA extensions as per the spec.
115  * It also specifies the canonical order in which it appears in the spec.
116  * Some of the extension may just be a place holder for now (B, K, P, J).
117  * This should be updated once corresponding extensions are ratified.
118  */
119 static const char base_riscv_exts[13] = "imafdqcbkjpvh";
120 
121 static void print_isa(struct seq_file *f, const char *isa)
122 {
123 	int i;
124 
125 	seq_puts(f, "isa\t\t: ");
126 	/* Print the rv[64/32] part */
127 	seq_write(f, isa, 4);
128 	for (i = 0; i < sizeof(base_riscv_exts); i++) {
129 		if (__riscv_isa_extension_available(NULL, base_riscv_exts[i] - 'a'))
130 			/* Print only enabled the base ISA extensions */
131 			seq_write(f, &base_riscv_exts[i], 1);
132 	}
133 	print_isa_ext(f);
134 	seq_puts(f, "\n");
135 }
136 
137 static void print_mmu(struct seq_file *f)
138 {
139 	char sv_type[16];
140 
141 #if defined(CONFIG_32BIT)
142 	strncpy(sv_type, "sv32", 5);
143 #elif defined(CONFIG_64BIT)
144 	if (pgtable_l5_enabled)
145 		strncpy(sv_type, "sv57", 5);
146 	else if (pgtable_l4_enabled)
147 		strncpy(sv_type, "sv48", 5);
148 	else
149 		strncpy(sv_type, "sv39", 5);
150 #endif
151 	seq_printf(f, "mmu\t\t: %s\n", sv_type);
152 }
153 
154 static void *c_start(struct seq_file *m, loff_t *pos)
155 {
156 	*pos = cpumask_next(*pos - 1, cpu_online_mask);
157 	if ((*pos) < nr_cpu_ids)
158 		return (void *)(uintptr_t)(1 + *pos);
159 	return NULL;
160 }
161 
162 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
163 {
164 	(*pos)++;
165 	return c_start(m, pos);
166 }
167 
168 static void c_stop(struct seq_file *m, void *v)
169 {
170 }
171 
172 static int c_show(struct seq_file *m, void *v)
173 {
174 	unsigned long cpu_id = (unsigned long)v - 1;
175 	struct device_node *node = of_get_cpu_node(cpu_id, NULL);
176 	const char *compat, *isa;
177 
178 	seq_printf(m, "processor\t: %lu\n", cpu_id);
179 	seq_printf(m, "hart\t\t: %lu\n", cpuid_to_hartid_map(cpu_id));
180 	if (!of_property_read_string(node, "riscv,isa", &isa))
181 		print_isa(m, isa);
182 	print_mmu(m);
183 	if (!of_property_read_string(node, "compatible", &compat)
184 	    && strcmp(compat, "riscv"))
185 		seq_printf(m, "uarch\t\t: %s\n", compat);
186 	seq_puts(m, "\n");
187 	of_node_put(node);
188 
189 	return 0;
190 }
191 
192 const struct seq_operations cpuinfo_op = {
193 	.start	= c_start,
194 	.next	= c_next,
195 	.stop	= c_stop,
196 	.show	= c_show
197 };
198 
199 #endif /* CONFIG_PROC_FS */
200