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>
20 bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
22 return phys_id == cpuid_to_hartid_map(cpu);
26 * Returns the hart ID of the given device tree node, or -ENODEV if the node
27 * isn't an enabled and valid RISC-V hart node.
29 int riscv_of_processor_hartid(struct device_node *node, unsigned long *hart)
33 *hart = (unsigned long)of_get_cpu_hwid(node, 0);
35 pr_warn("Found CPU without hart ID\n");
39 cpu = riscv_hartid_to_cpuid(*hart);
43 if (!cpu_possible(cpu))
49 int riscv_early_of_processor_hartid(struct device_node *node, unsigned long *hart)
53 if (!of_device_is_compatible(node, "riscv")) {
54 pr_warn("Found incompatible CPU\n");
58 *hart = (unsigned long)of_get_cpu_hwid(node, 0);
60 pr_warn("Found CPU without hart ID\n");
64 if (!of_device_is_available(node)) {
65 pr_info("CPU with hartid=%lu is not available\n", *hart);
69 if (of_property_read_string(node, "riscv,isa", &isa)) {
70 pr_warn("CPU with hartid=%lu has no \"riscv,isa\" property\n", *hart);
74 if (IS_ENABLED(CONFIG_32BIT) && strncasecmp(isa, "rv32ima", 7))
77 if (IS_ENABLED(CONFIG_64BIT) && strncasecmp(isa, "rv64ima", 7))
84 * Find hart ID of the CPU DT node under which given DT node falls.
86 * To achieve this, we walk up the DT tree until we find an active
87 * RISC-V core (HART) node and extract the cpuid from it.
89 int riscv_of_parent_hartid(struct device_node *node, unsigned long *hartid)
93 for (; node; node = node->parent) {
94 if (of_device_is_compatible(node, "riscv")) {
95 rc = riscv_of_processor_hartid(node, hartid);
104 DEFINE_PER_CPU(struct riscv_cpuinfo, riscv_cpuinfo);
106 unsigned long riscv_cached_mvendorid(unsigned int cpu_id)
108 struct riscv_cpuinfo *ci = per_cpu_ptr(&riscv_cpuinfo, cpu_id);
110 return ci->mvendorid;
112 EXPORT_SYMBOL(riscv_cached_mvendorid);
114 unsigned long riscv_cached_marchid(unsigned int cpu_id)
116 struct riscv_cpuinfo *ci = per_cpu_ptr(&riscv_cpuinfo, cpu_id);
120 EXPORT_SYMBOL(riscv_cached_marchid);
122 unsigned long riscv_cached_mimpid(unsigned int cpu_id)
124 struct riscv_cpuinfo *ci = per_cpu_ptr(&riscv_cpuinfo, cpu_id);
128 EXPORT_SYMBOL(riscv_cached_mimpid);
130 static int riscv_cpuinfo_starting(unsigned int cpu)
132 struct riscv_cpuinfo *ci = this_cpu_ptr(&riscv_cpuinfo);
134 #if IS_ENABLED(CONFIG_RISCV_SBI)
135 ci->mvendorid = sbi_spec_is_0_1() ? 0 : sbi_get_mvendorid();
136 ci->marchid = sbi_spec_is_0_1() ? 0 : sbi_get_marchid();
137 ci->mimpid = sbi_spec_is_0_1() ? 0 : sbi_get_mimpid();
138 #elif IS_ENABLED(CONFIG_RISCV_M_MODE)
139 ci->mvendorid = csr_read(CSR_MVENDORID);
140 ci->marchid = csr_read(CSR_MARCHID);
141 ci->mimpid = csr_read(CSR_MIMPID);
151 static int __init riscv_cpuinfo_init(void)
155 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "riscv/cpuinfo:starting",
156 riscv_cpuinfo_starting, NULL);
158 pr_err("cpuinfo: failed to register hotplug callbacks.\n");
164 arch_initcall(riscv_cpuinfo_init);
166 #ifdef CONFIG_PROC_FS
168 #define __RISCV_ISA_EXT_DATA(UPROP, EXTID) \
171 .isa_ext_id = EXTID, \
175 * The canonical order of ISA extension names in the ISA string is defined in
176 * chapter 27 of the unprivileged specification.
178 * Ordinarily, for in-kernel data structures, this order is unimportant but
179 * isa_ext_arr defines the order of the ISA string in /proc/cpuinfo.
181 * The specification uses vague wording, such as should, when it comes to
182 * ordering, so for our purposes the following rules apply:
184 * 1. All multi-letter extensions must be separated from other extensions by an
187 * 2. Additional standard extensions (starting with 'Z') must be sorted after
188 * single-letter extensions and before any higher-privileged extensions.
190 * 3. The first letter following the 'Z' conventionally indicates the most
191 * closely related alphabetical extension category, IMAFDQLCBKJTPVH.
192 * If multiple 'Z' extensions are named, they must be ordered first by
193 * category, then alphabetically within a category.
195 * 3. Standard supervisor-level extensions (starting with 'S') must be listed
196 * after standard unprivileged extensions. If multiple supervisor-level
197 * extensions are listed, they must be ordered alphabetically.
199 * 4. Standard machine-level extensions (starting with 'Zxm') must be listed
200 * after any lower-privileged, standard extensions. If multiple
201 * machine-level extensions are listed, they must be ordered
204 * 5. Non-standard extensions (starting with 'X') must be listed after all
205 * standard extensions. If multiple non-standard extensions are listed, they
206 * must be ordered alphabetically.
208 * An example string following the order is:
209 * rv64imadc_zifoo_zigoo_zafoo_sbar_scar_zxmbaz_xqux_xrux
211 * New entries to this struct should follow the ordering rules described above.
213 static struct riscv_isa_ext_data isa_ext_arr[] = {
214 __RISCV_ISA_EXT_DATA(zicbom, RISCV_ISA_EXT_ZICBOM),
215 __RISCV_ISA_EXT_DATA(zicboz, RISCV_ISA_EXT_ZICBOZ),
216 __RISCV_ISA_EXT_DATA(zicntr, RISCV_ISA_EXT_ZICNTR),
217 __RISCV_ISA_EXT_DATA(zicsr, RISCV_ISA_EXT_ZICSR),
218 __RISCV_ISA_EXT_DATA(zifencei, RISCV_ISA_EXT_ZIFENCEI),
219 __RISCV_ISA_EXT_DATA(zihintpause, RISCV_ISA_EXT_ZIHINTPAUSE),
220 __RISCV_ISA_EXT_DATA(zihpm, RISCV_ISA_EXT_ZIHPM),
221 __RISCV_ISA_EXT_DATA(zba, RISCV_ISA_EXT_ZBA),
222 __RISCV_ISA_EXT_DATA(zbb, RISCV_ISA_EXT_ZBB),
223 __RISCV_ISA_EXT_DATA(zbs, RISCV_ISA_EXT_ZBS),
224 __RISCV_ISA_EXT_DATA(smaia, RISCV_ISA_EXT_SMAIA),
225 __RISCV_ISA_EXT_DATA(ssaia, RISCV_ISA_EXT_SSAIA),
226 __RISCV_ISA_EXT_DATA(sscofpmf, RISCV_ISA_EXT_SSCOFPMF),
227 __RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),
228 __RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL),
229 __RISCV_ISA_EXT_DATA(svnapot, RISCV_ISA_EXT_SVNAPOT),
230 __RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),
231 __RISCV_ISA_EXT_DATA("", RISCV_ISA_EXT_MAX),
234 static void print_isa_ext(struct seq_file *f)
236 struct riscv_isa_ext_data *edata;
239 arr_sz = ARRAY_SIZE(isa_ext_arr) - 1;
241 /* No extension support available */
245 for (i = 0; i <= arr_sz; i++) {
246 edata = &isa_ext_arr[i];
247 if (!__riscv_isa_extension_available(NULL, edata->isa_ext_id))
249 seq_printf(f, "_%s", edata->uprop);
254 * These are the only valid base (single letter) ISA extensions as per the spec.
255 * It also specifies the canonical order in which it appears in the spec.
256 * Some of the extension may just be a place holder for now (B, K, P, J).
257 * This should be updated once corresponding extensions are ratified.
259 static const char base_riscv_exts[13] = "imafdqcbkjpvh";
261 static void print_isa(struct seq_file *f, const char *isa)
265 seq_puts(f, "isa\t\t: ");
266 /* Print the rv[64/32] part */
267 seq_write(f, isa, 4);
268 for (i = 0; i < sizeof(base_riscv_exts); i++) {
269 if (__riscv_isa_extension_available(NULL, base_riscv_exts[i] - 'a'))
270 /* Print only enabled the base ISA extensions */
271 seq_write(f, &base_riscv_exts[i], 1);
277 static void print_mmu(struct seq_file *f)
282 #if defined(CONFIG_32BIT)
283 strncpy(sv_type, "sv32", 5);
284 #elif defined(CONFIG_64BIT)
285 if (pgtable_l5_enabled)
286 strncpy(sv_type, "sv57", 5);
287 else if (pgtable_l4_enabled)
288 strncpy(sv_type, "sv48", 5);
290 strncpy(sv_type, "sv39", 5);
293 strncpy(sv_type, "none", 5);
294 #endif /* CONFIG_MMU */
295 seq_printf(f, "mmu\t\t: %s\n", sv_type);
298 static void *c_start(struct seq_file *m, loff_t *pos)
300 if (*pos == nr_cpu_ids)
303 *pos = cpumask_next(*pos - 1, cpu_online_mask);
304 if ((*pos) < nr_cpu_ids)
305 return (void *)(uintptr_t)(1 + *pos);
309 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
312 return c_start(m, pos);
315 static void c_stop(struct seq_file *m, void *v)
319 static int c_show(struct seq_file *m, void *v)
321 unsigned long cpu_id = (unsigned long)v - 1;
322 struct riscv_cpuinfo *ci = per_cpu_ptr(&riscv_cpuinfo, cpu_id);
323 struct device_node *node;
324 const char *compat, *isa;
326 seq_printf(m, "processor\t: %lu\n", cpu_id);
327 seq_printf(m, "hart\t\t: %lu\n", cpuid_to_hartid_map(cpu_id));
330 node = of_get_cpu_node(cpu_id, NULL);
331 if (!of_property_read_string(node, "riscv,isa", &isa))
335 if (!of_property_read_string(node, "compatible", &compat) &&
336 strcmp(compat, "riscv"))
337 seq_printf(m, "uarch\t\t: %s\n", compat);
341 if (!acpi_get_riscv_isa(NULL, cpu_id, &isa))
347 seq_printf(m, "mvendorid\t: 0x%lx\n", ci->mvendorid);
348 seq_printf(m, "marchid\t\t: 0x%lx\n", ci->marchid);
349 seq_printf(m, "mimpid\t\t: 0x%lx\n", ci->mimpid);
355 const struct seq_operations cpuinfo_op = {
362 #endif /* CONFIG_PROC_FS */