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>
19 #include <asm/vendor_extensions.h>
21 bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
23 return phys_id == cpuid_to_hartid_map(cpu);
27 * Returns the hart ID of the given device tree node, or -ENODEV if the node
28 * isn't an enabled and valid RISC-V hart node.
30 int riscv_of_processor_hartid(struct device_node *node, unsigned long *hart)
34 *hart = (unsigned long)of_get_cpu_hwid(node, 0);
36 pr_warn("Found CPU without hart ID\n");
40 cpu = riscv_hartid_to_cpuid(*hart);
44 if (!cpu_possible(cpu))
50 int __init riscv_early_of_processor_hartid(struct device_node *node, unsigned long *hart)
54 if (!of_device_is_compatible(node, "riscv")) {
55 pr_warn("Found incompatible CPU\n");
59 *hart = (unsigned long)of_get_cpu_hwid(node, 0);
61 pr_warn("Found CPU without hart ID\n");
65 if (!of_device_is_available(node)) {
66 pr_info("CPU with hartid=%lu is not available\n", *hart);
70 if (of_property_read_string(node, "riscv,isa-base", &isa))
73 if (IS_ENABLED(CONFIG_32BIT) && strncasecmp(isa, "rv32i", 5)) {
74 pr_warn("CPU with hartid=%lu does not support rv32i", *hart);
78 if (IS_ENABLED(CONFIG_64BIT) && strncasecmp(isa, "rv64i", 5)) {
79 pr_warn("CPU with hartid=%lu does not support rv64i", *hart);
83 if (!of_property_present(node, "riscv,isa-extensions"))
86 if (of_property_match_string(node, "riscv,isa-extensions", "i") < 0 ||
87 of_property_match_string(node, "riscv,isa-extensions", "m") < 0 ||
88 of_property_match_string(node, "riscv,isa-extensions", "a") < 0) {
89 pr_warn("CPU with hartid=%lu does not support ima", *hart);
96 if (!riscv_isa_fallback) {
97 pr_warn("CPU with hartid=%lu is invalid: this kernel does not parse \"riscv,isa\"",
102 if (of_property_read_string(node, "riscv,isa", &isa)) {
103 pr_warn("CPU with hartid=%lu has no \"riscv,isa-base\" or \"riscv,isa\" property\n",
108 if (IS_ENABLED(CONFIG_32BIT) && strncasecmp(isa, "rv32ima", 7)) {
109 pr_warn("CPU with hartid=%lu does not support rv32ima", *hart);
113 if (IS_ENABLED(CONFIG_64BIT) && strncasecmp(isa, "rv64ima", 7)) {
114 pr_warn("CPU with hartid=%lu does not support rv64ima", *hart);
122 * Find hart ID of the CPU DT node under which given DT node falls.
124 * To achieve this, we walk up the DT tree until we find an active
125 * RISC-V core (HART) node and extract the cpuid from it.
127 int riscv_of_parent_hartid(struct device_node *node, unsigned long *hartid)
129 for (; node; node = node->parent) {
130 if (of_device_is_compatible(node, "riscv")) {
131 *hartid = (unsigned long)of_get_cpu_hwid(node, 0);
132 if (*hartid == ~0UL) {
133 pr_warn("Found CPU without hart ID\n");
143 unsigned long __init riscv_get_marchid(void)
145 struct riscv_cpuinfo *ci = this_cpu_ptr(&riscv_cpuinfo);
147 #if IS_ENABLED(CONFIG_RISCV_SBI)
148 ci->marchid = sbi_spec_is_0_1() ? 0 : sbi_get_marchid();
149 #elif IS_ENABLED(CONFIG_RISCV_M_MODE)
150 ci->marchid = csr_read(CSR_MARCHID);
157 unsigned long __init riscv_get_mvendorid(void)
159 struct riscv_cpuinfo *ci = this_cpu_ptr(&riscv_cpuinfo);
161 #if IS_ENABLED(CONFIG_RISCV_SBI)
162 ci->mvendorid = sbi_spec_is_0_1() ? 0 : sbi_get_mvendorid();
163 #elif IS_ENABLED(CONFIG_RISCV_M_MODE)
164 ci->mvendorid = csr_read(CSR_MVENDORID);
168 return ci->mvendorid;
171 DEFINE_PER_CPU(struct riscv_cpuinfo, riscv_cpuinfo);
173 unsigned long riscv_cached_mvendorid(unsigned int cpu_id)
175 struct riscv_cpuinfo *ci = per_cpu_ptr(&riscv_cpuinfo, cpu_id);
177 return ci->mvendorid;
179 EXPORT_SYMBOL(riscv_cached_mvendorid);
181 unsigned long riscv_cached_marchid(unsigned int cpu_id)
183 struct riscv_cpuinfo *ci = per_cpu_ptr(&riscv_cpuinfo, cpu_id);
187 EXPORT_SYMBOL(riscv_cached_marchid);
189 unsigned long riscv_cached_mimpid(unsigned int cpu_id)
191 struct riscv_cpuinfo *ci = per_cpu_ptr(&riscv_cpuinfo, cpu_id);
195 EXPORT_SYMBOL(riscv_cached_mimpid);
197 static int riscv_cpuinfo_starting(unsigned int cpu)
199 struct riscv_cpuinfo *ci = this_cpu_ptr(&riscv_cpuinfo);
201 #if IS_ENABLED(CONFIG_RISCV_SBI)
203 ci->mvendorid = sbi_spec_is_0_1() ? 0 : sbi_get_mvendorid();
205 ci->marchid = sbi_spec_is_0_1() ? 0 : sbi_get_marchid();
206 ci->mimpid = sbi_spec_is_0_1() ? 0 : sbi_get_mimpid();
207 #elif IS_ENABLED(CONFIG_RISCV_M_MODE)
209 ci->mvendorid = csr_read(CSR_MVENDORID);
211 ci->marchid = csr_read(CSR_MARCHID);
212 ci->mimpid = csr_read(CSR_MIMPID);
222 static int __init riscv_cpuinfo_init(void)
226 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "riscv/cpuinfo:starting",
227 riscv_cpuinfo_starting, NULL);
229 pr_err("cpuinfo: failed to register hotplug callbacks.\n");
235 arch_initcall(riscv_cpuinfo_init);
237 #ifdef CONFIG_PROC_FS
241 static void print_vendor_isa(struct seq_file *f, int cpu)
243 struct riscv_isavendorinfo *vendor_bitmap;
244 struct riscv_isa_vendor_ext_data_list *ext_list;
245 const struct riscv_isa_ext_data *ext_data;
247 for (int i = 0; i < riscv_isa_vendor_ext_list_size; i++) {
248 ext_list = riscv_isa_vendor_ext_list[i];
249 ext_data = riscv_isa_vendor_ext_list[i]->ext_data;
252 vendor_bitmap = &ext_list->all_harts_isa_bitmap;
254 vendor_bitmap = &ext_list->per_hart_isa_bitmap[cpu];
256 for (int j = 0; j < ext_list->ext_data_count; j++) {
257 if (!__riscv_isa_extension_available(vendor_bitmap->isa, ext_data[j].id))
260 seq_printf(f, "_%s", ext_data[j].name);
265 static void print_isa(struct seq_file *f, const unsigned long *isa_bitmap, int cpu)
268 if (IS_ENABLED(CONFIG_32BIT))
269 seq_write(f, "rv32", 4);
271 seq_write(f, "rv64", 4);
273 for (int i = 0; i < riscv_isa_ext_count; i++) {
274 if (!__riscv_isa_extension_available(isa_bitmap, riscv_isa_ext[i].id))
277 /* Only multi-letter extensions are split by underscores */
278 if (strnlen(riscv_isa_ext[i].name, 2) != 1)
281 seq_printf(f, "%s", riscv_isa_ext[i].name);
284 print_vendor_isa(f, cpu);
289 static void print_mmu(struct seq_file *f)
294 #if defined(CONFIG_32BIT)
296 #elif defined(CONFIG_64BIT)
297 if (pgtable_l5_enabled)
299 else if (pgtable_l4_enabled)
306 #endif /* CONFIG_MMU */
307 seq_printf(f, "mmu\t\t: %s\n", sv_type);
310 static void *c_start(struct seq_file *m, loff_t *pos)
312 if (*pos == nr_cpu_ids)
315 *pos = cpumask_next(*pos - 1, cpu_online_mask);
316 if ((*pos) < nr_cpu_ids)
317 return (void *)(uintptr_t)(1 + *pos);
321 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
324 return c_start(m, pos);
327 static void c_stop(struct seq_file *m, void *v)
331 static int c_show(struct seq_file *m, void *v)
333 unsigned long cpu_id = (unsigned long)v - 1;
334 struct riscv_cpuinfo *ci = per_cpu_ptr(&riscv_cpuinfo, cpu_id);
335 struct device_node *node;
338 seq_printf(m, "processor\t: %lu\n", cpu_id);
339 seq_printf(m, "hart\t\t: %lu\n", cpuid_to_hartid_map(cpu_id));
342 * For historical raisins, the isa: line is limited to the lowest common
343 * denominator of extensions supported across all harts. A true list of
344 * extensions supported on this hart is printed later in the hart isa:
347 seq_puts(m, "isa\t\t: ");
348 print_isa(m, NULL, ALL_CPUS);
352 node = of_get_cpu_node(cpu_id, NULL);
354 if (!of_property_read_string(node, "compatible", &compat) &&
355 strcmp(compat, "riscv"))
356 seq_printf(m, "uarch\t\t: %s\n", compat);
361 seq_printf(m, "mvendorid\t: 0x%lx\n", ci->mvendorid);
362 seq_printf(m, "marchid\t\t: 0x%lx\n", ci->marchid);
363 seq_printf(m, "mimpid\t\t: 0x%lx\n", ci->mimpid);
366 * Print the ISA extensions specific to this hart, which may show
367 * additional extensions not present across all harts.
369 seq_puts(m, "hart isa\t: ");
370 print_isa(m, hart_isa[cpu_id].isa, cpu_id);
376 const struct seq_operations cpuinfo_op = {
383 #endif /* CONFIG_PROC_FS */