1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Regents of the University of California
6 #include <linux/acpi.h>
8 #include <linux/ctype.h>
9 #include <linux/init.h>
10 #include <linux/seq_file.h>
13 #include <asm/cpufeature.h>
15 #include <asm/hwcap.h>
18 #include <asm/pgtable.h>
21 * Returns the hart ID of the given device tree node, or -ENODEV if the node
22 * isn't an enabled and valid RISC-V hart node.
24 int riscv_of_processor_hartid(struct device_node *node, unsigned long *hart)
28 *hart = (unsigned long)of_get_cpu_hwid(node, 0);
30 pr_warn("Found CPU without hart ID\n");
34 cpu = riscv_hartid_to_cpuid(*hart);
38 if (!cpu_possible(cpu))
44 int riscv_early_of_processor_hartid(struct device_node *node, unsigned long *hart)
48 if (!of_device_is_compatible(node, "riscv")) {
49 pr_warn("Found incompatible CPU\n");
53 *hart = (unsigned long)of_get_cpu_hwid(node, 0);
55 pr_warn("Found CPU without hart ID\n");
59 if (!of_device_is_available(node)) {
60 pr_info("CPU with hartid=%lu is not available\n", *hart);
64 if (of_property_read_string(node, "riscv,isa", &isa)) {
65 pr_warn("CPU with hartid=%lu has no \"riscv,isa\" property\n", *hart);
69 if (IS_ENABLED(CONFIG_32BIT) && strncasecmp(isa, "rv32ima", 7))
72 if (IS_ENABLED(CONFIG_64BIT) && strncasecmp(isa, "rv64ima", 7))
79 * Find hart ID of the CPU DT node under which given DT node falls.
81 * To achieve this, we walk up the DT tree until we find an active
82 * RISC-V core (HART) node and extract the cpuid from it.
84 int riscv_of_parent_hartid(struct device_node *node, unsigned long *hartid)
88 for (; node; node = node->parent) {
89 if (of_device_is_compatible(node, "riscv")) {
90 rc = riscv_of_processor_hartid(node, hartid);
99 DEFINE_PER_CPU(struct riscv_cpuinfo, riscv_cpuinfo);
101 unsigned long riscv_cached_mvendorid(unsigned int cpu_id)
103 struct riscv_cpuinfo *ci = per_cpu_ptr(&riscv_cpuinfo, cpu_id);
105 return ci->mvendorid;
107 EXPORT_SYMBOL(riscv_cached_mvendorid);
109 unsigned long riscv_cached_marchid(unsigned int cpu_id)
111 struct riscv_cpuinfo *ci = per_cpu_ptr(&riscv_cpuinfo, cpu_id);
115 EXPORT_SYMBOL(riscv_cached_marchid);
117 unsigned long riscv_cached_mimpid(unsigned int cpu_id)
119 struct riscv_cpuinfo *ci = per_cpu_ptr(&riscv_cpuinfo, cpu_id);
123 EXPORT_SYMBOL(riscv_cached_mimpid);
125 static int riscv_cpuinfo_starting(unsigned int cpu)
127 struct riscv_cpuinfo *ci = this_cpu_ptr(&riscv_cpuinfo);
129 #if IS_ENABLED(CONFIG_RISCV_SBI)
130 ci->mvendorid = sbi_spec_is_0_1() ? 0 : sbi_get_mvendorid();
131 ci->marchid = sbi_spec_is_0_1() ? 0 : sbi_get_marchid();
132 ci->mimpid = sbi_spec_is_0_1() ? 0 : sbi_get_mimpid();
133 #elif IS_ENABLED(CONFIG_RISCV_M_MODE)
134 ci->mvendorid = csr_read(CSR_MVENDORID);
135 ci->marchid = csr_read(CSR_MARCHID);
136 ci->mimpid = csr_read(CSR_MIMPID);
146 static int __init riscv_cpuinfo_init(void)
150 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "riscv/cpuinfo:starting",
151 riscv_cpuinfo_starting, NULL);
153 pr_err("cpuinfo: failed to register hotplug callbacks.\n");
159 arch_initcall(riscv_cpuinfo_init);
161 #ifdef CONFIG_PROC_FS
163 #define __RISCV_ISA_EXT_DATA(UPROP, EXTID) \
166 .isa_ext_id = EXTID, \
170 * The canonical order of ISA extension names in the ISA string is defined in
171 * chapter 27 of the unprivileged specification.
173 * Ordinarily, for in-kernel data structures, this order is unimportant but
174 * isa_ext_arr defines the order of the ISA string in /proc/cpuinfo.
176 * The specification uses vague wording, such as should, when it comes to
177 * ordering, so for our purposes the following rules apply:
179 * 1. All multi-letter extensions must be separated from other extensions by an
182 * 2. Additional standard extensions (starting with 'Z') must be sorted after
183 * single-letter extensions and before any higher-privileged extensions.
185 * 3. The first letter following the 'Z' conventionally indicates the most
186 * closely related alphabetical extension category, IMAFDQLCBKJTPVH.
187 * If multiple 'Z' extensions are named, they must be ordered first by
188 * category, then alphabetically within a category.
190 * 3. Standard supervisor-level extensions (starting with 'S') must be listed
191 * after standard unprivileged extensions. If multiple supervisor-level
192 * extensions are listed, they must be ordered alphabetically.
194 * 4. Standard machine-level extensions (starting with 'Zxm') must be listed
195 * after any lower-privileged, standard extensions. If multiple
196 * machine-level extensions are listed, they must be ordered
199 * 5. Non-standard extensions (starting with 'X') must be listed after all
200 * standard extensions. If multiple non-standard extensions are listed, they
201 * must be ordered alphabetically.
203 * An example string following the order is:
204 * rv64imadc_zifoo_zigoo_zafoo_sbar_scar_zxmbaz_xqux_xrux
206 * New entries to this struct should follow the ordering rules described above.
208 static struct riscv_isa_ext_data isa_ext_arr[] = {
209 __RISCV_ISA_EXT_DATA(zicbom, RISCV_ISA_EXT_ZICBOM),
210 __RISCV_ISA_EXT_DATA(zicboz, RISCV_ISA_EXT_ZICBOZ),
211 __RISCV_ISA_EXT_DATA(zicntr, RISCV_ISA_EXT_ZICNTR),
212 __RISCV_ISA_EXT_DATA(zicsr, RISCV_ISA_EXT_ZICSR),
213 __RISCV_ISA_EXT_DATA(zifencei, RISCV_ISA_EXT_ZIFENCEI),
214 __RISCV_ISA_EXT_DATA(zihintpause, RISCV_ISA_EXT_ZIHINTPAUSE),
215 __RISCV_ISA_EXT_DATA(zihpm, RISCV_ISA_EXT_ZIHPM),
216 __RISCV_ISA_EXT_DATA(zbb, RISCV_ISA_EXT_ZBB),
217 __RISCV_ISA_EXT_DATA(smaia, RISCV_ISA_EXT_SMAIA),
218 __RISCV_ISA_EXT_DATA(ssaia, RISCV_ISA_EXT_SSAIA),
219 __RISCV_ISA_EXT_DATA(sscofpmf, RISCV_ISA_EXT_SSCOFPMF),
220 __RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),
221 __RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL),
222 __RISCV_ISA_EXT_DATA(svnapot, RISCV_ISA_EXT_SVNAPOT),
223 __RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),
224 __RISCV_ISA_EXT_DATA("", RISCV_ISA_EXT_MAX),
227 static void print_isa_ext(struct seq_file *f)
229 struct riscv_isa_ext_data *edata;
232 arr_sz = ARRAY_SIZE(isa_ext_arr) - 1;
234 /* No extension support available */
238 for (i = 0; i <= arr_sz; i++) {
239 edata = &isa_ext_arr[i];
240 if (!__riscv_isa_extension_available(NULL, edata->isa_ext_id))
242 seq_printf(f, "_%s", edata->uprop);
247 * These are the only valid base (single letter) ISA extensions as per the spec.
248 * It also specifies the canonical order in which it appears in the spec.
249 * Some of the extension may just be a place holder for now (B, K, P, J).
250 * This should be updated once corresponding extensions are ratified.
252 static const char base_riscv_exts[13] = "imafdqcbkjpvh";
254 static void print_isa(struct seq_file *f, const char *isa)
258 seq_puts(f, "isa\t\t: ");
259 /* Print the rv[64/32] part */
260 seq_write(f, isa, 4);
261 for (i = 0; i < sizeof(base_riscv_exts); i++) {
262 if (__riscv_isa_extension_available(NULL, base_riscv_exts[i] - 'a'))
263 /* Print only enabled the base ISA extensions */
264 seq_write(f, &base_riscv_exts[i], 1);
270 static void print_mmu(struct seq_file *f)
275 #if defined(CONFIG_32BIT)
276 strncpy(sv_type, "sv32", 5);
277 #elif defined(CONFIG_64BIT)
278 if (pgtable_l5_enabled)
279 strncpy(sv_type, "sv57", 5);
280 else if (pgtable_l4_enabled)
281 strncpy(sv_type, "sv48", 5);
283 strncpy(sv_type, "sv39", 5);
286 strncpy(sv_type, "none", 5);
287 #endif /* CONFIG_MMU */
288 seq_printf(f, "mmu\t\t: %s\n", sv_type);
291 static void *c_start(struct seq_file *m, loff_t *pos)
293 if (*pos == nr_cpu_ids)
296 *pos = cpumask_next(*pos - 1, cpu_online_mask);
297 if ((*pos) < nr_cpu_ids)
298 return (void *)(uintptr_t)(1 + *pos);
302 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
305 return c_start(m, pos);
308 static void c_stop(struct seq_file *m, void *v)
312 static int c_show(struct seq_file *m, void *v)
314 unsigned long cpu_id = (unsigned long)v - 1;
315 struct riscv_cpuinfo *ci = per_cpu_ptr(&riscv_cpuinfo, cpu_id);
316 struct device_node *node;
317 const char *compat, *isa;
319 seq_printf(m, "processor\t: %lu\n", cpu_id);
320 seq_printf(m, "hart\t\t: %lu\n", cpuid_to_hartid_map(cpu_id));
323 node = of_get_cpu_node(cpu_id, NULL);
324 if (!of_property_read_string(node, "riscv,isa", &isa))
328 if (!of_property_read_string(node, "compatible", &compat) &&
329 strcmp(compat, "riscv"))
330 seq_printf(m, "uarch\t\t: %s\n", compat);
334 if (!acpi_get_riscv_isa(NULL, cpu_id, &isa))
340 seq_printf(m, "mvendorid\t: 0x%lx\n", ci->mvendorid);
341 seq_printf(m, "marchid\t\t: 0x%lx\n", ci->marchid);
342 seq_printf(m, "mimpid\t\t: 0x%lx\n", ci->mimpid);
348 const struct seq_operations cpuinfo_op = {
355 #endif /* CONFIG_PROC_FS */