Mention branches and keyring.
[releases.git] / riscv / kernel / cpu.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012 Regents of the University of California
4  */
5
6 #include <linux/cpu.h>
7 #include <linux/init.h>
8 #include <linux/seq_file.h>
9 #include <linux/of.h>
10 #include <asm/csr.h>
11 #include <asm/hwcap.h>
12 #include <asm/sbi.h>
13 #include <asm/smp.h>
14 #include <asm/pgtable.h>
15
16 /*
17  * Returns the hart ID of the given device tree node, or -ENODEV if the node
18  * isn't an enabled and valid RISC-V hart node.
19  */
20 int riscv_of_processor_hartid(struct device_node *node, unsigned long *hart)
21 {
22         const char *isa;
23
24         if (!of_device_is_compatible(node, "riscv")) {
25                 pr_warn("Found incompatible CPU\n");
26                 return -ENODEV;
27         }
28
29         *hart = (unsigned long) of_get_cpu_hwid(node, 0);
30         if (*hart == ~0UL) {
31                 pr_warn("Found CPU without hart ID\n");
32                 return -ENODEV;
33         }
34
35         if (!of_device_is_available(node)) {
36                 pr_info("CPU with hartid=%lu is not available\n", *hart);
37                 return -ENODEV;
38         }
39
40         if (of_property_read_string(node, "riscv,isa", &isa)) {
41                 pr_warn("CPU with hartid=%lu has no \"riscv,isa\" property\n", *hart);
42                 return -ENODEV;
43         }
44         if (isa[0] != 'r' || isa[1] != 'v') {
45                 pr_warn("CPU with hartid=%lu has an invalid ISA of \"%s\"\n", *hart, isa);
46                 return -ENODEV;
47         }
48
49         return 0;
50 }
51
52 /*
53  * Find hart ID of the CPU DT node under which given DT node falls.
54  *
55  * To achieve this, we walk up the DT tree until we find an active
56  * RISC-V core (HART) node and extract the cpuid from it.
57  */
58 int riscv_of_parent_hartid(struct device_node *node, unsigned long *hartid)
59 {
60         for (; node; node = node->parent) {
61                 if (of_device_is_compatible(node, "riscv")) {
62                         *hartid = (unsigned long)of_get_cpu_hwid(node, 0);
63                         if (*hartid == ~0UL) {
64                                 pr_warn("Found CPU without hart ID\n");
65                                 return -ENODEV;
66                         }
67                         return 0;
68                 }
69         }
70
71         return -1;
72 }
73
74 #ifdef CONFIG_PROC_FS
75
76 struct riscv_cpuinfo {
77         unsigned long mvendorid;
78         unsigned long marchid;
79         unsigned long mimpid;
80 };
81 static DEFINE_PER_CPU(struct riscv_cpuinfo, riscv_cpuinfo);
82
83 static int riscv_cpuinfo_starting(unsigned int cpu)
84 {
85         struct riscv_cpuinfo *ci = this_cpu_ptr(&riscv_cpuinfo);
86
87 #if IS_ENABLED(CONFIG_RISCV_SBI)
88         ci->mvendorid = sbi_spec_is_0_1() ? 0 : sbi_get_mvendorid();
89         ci->marchid = sbi_spec_is_0_1() ? 0 : sbi_get_marchid();
90         ci->mimpid = sbi_spec_is_0_1() ? 0 : sbi_get_mimpid();
91 #elif IS_ENABLED(CONFIG_RISCV_M_MODE)
92         ci->mvendorid = csr_read(CSR_MVENDORID);
93         ci->marchid = csr_read(CSR_MARCHID);
94         ci->mimpid = csr_read(CSR_MIMPID);
95 #else
96         ci->mvendorid = 0;
97         ci->marchid = 0;
98         ci->mimpid = 0;
99 #endif
100
101         return 0;
102 }
103
104 static int __init riscv_cpuinfo_init(void)
105 {
106         int ret;
107
108         ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "riscv/cpuinfo:starting",
109                                 riscv_cpuinfo_starting, NULL);
110         if (ret < 0) {
111                 pr_err("cpuinfo: failed to register hotplug callbacks.\n");
112                 return ret;
113         }
114
115         return 0;
116 }
117 device_initcall(riscv_cpuinfo_init);
118
119 #define __RISCV_ISA_EXT_DATA(UPROP, EXTID) \
120         {                                                       \
121                 .uprop = #UPROP,                                \
122                 .isa_ext_id = EXTID,                            \
123         }
124 /*
125  * Here are the ordering rules of extension naming defined by RISC-V
126  * specification :
127  * 1. All extensions should be separated from other multi-letter extensions
128  *    by an underscore.
129  * 2. The first letter following the 'Z' conventionally indicates the most
130  *    closely related alphabetical extension category, IMAFDQLCBKJTPVH.
131  *    If multiple 'Z' extensions are named, they should be ordered first
132  *    by category, then alphabetically within a category.
133  * 3. Standard supervisor-level extensions (starts with 'S') should be
134  *    listed after standard unprivileged extensions.  If multiple
135  *    supervisor-level extensions are listed, they should be ordered
136  *    alphabetically.
137  * 4. Non-standard extensions (starts with 'X') must be listed after all
138  *    standard extensions. They must be separated from other multi-letter
139  *    extensions by an underscore.
140  */
141 static struct riscv_isa_ext_data isa_ext_arr[] = {
142         __RISCV_ISA_EXT_DATA(sscofpmf, RISCV_ISA_EXT_SSCOFPMF),
143         __RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),
144         __RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL),
145         __RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),
146         __RISCV_ISA_EXT_DATA(zicbom, RISCV_ISA_EXT_ZICBOM),
147         __RISCV_ISA_EXT_DATA(zihintpause, RISCV_ISA_EXT_ZIHINTPAUSE),
148         __RISCV_ISA_EXT_DATA("", RISCV_ISA_EXT_MAX),
149 };
150
151 static void print_isa_ext(struct seq_file *f)
152 {
153         struct riscv_isa_ext_data *edata;
154         int i = 0, arr_sz;
155
156         arr_sz = ARRAY_SIZE(isa_ext_arr) - 1;
157
158         /* No extension support available */
159         if (arr_sz <= 0)
160                 return;
161
162         for (i = 0; i <= arr_sz; i++) {
163                 edata = &isa_ext_arr[i];
164                 if (!__riscv_isa_extension_available(NULL, edata->isa_ext_id))
165                         continue;
166                 seq_printf(f, "_%s", edata->uprop);
167         }
168 }
169
170 /*
171  * These are the only valid base (single letter) ISA extensions as per the spec.
172  * It also specifies the canonical order in which it appears in the spec.
173  * Some of the extension may just be a place holder for now (B, K, P, J).
174  * This should be updated once corresponding extensions are ratified.
175  */
176 static const char base_riscv_exts[13] = "imafdqcbkjpvh";
177
178 static void print_isa(struct seq_file *f, const char *isa)
179 {
180         int i;
181
182         seq_puts(f, "isa\t\t: ");
183         /* Print the rv[64/32] part */
184         seq_write(f, isa, 4);
185         for (i = 0; i < sizeof(base_riscv_exts); i++) {
186                 if (__riscv_isa_extension_available(NULL, base_riscv_exts[i] - 'a'))
187                         /* Print only enabled the base ISA extensions */
188                         seq_write(f, &base_riscv_exts[i], 1);
189         }
190         print_isa_ext(f);
191         seq_puts(f, "\n");
192 }
193
194 static void print_mmu(struct seq_file *f)
195 {
196         char sv_type[16];
197
198 #ifdef CONFIG_MMU
199 #if defined(CONFIG_32BIT)
200         strncpy(sv_type, "sv32", 5);
201 #elif defined(CONFIG_64BIT)
202         if (pgtable_l5_enabled)
203                 strncpy(sv_type, "sv57", 5);
204         else if (pgtable_l4_enabled)
205                 strncpy(sv_type, "sv48", 5);
206         else
207                 strncpy(sv_type, "sv39", 5);
208 #endif
209 #else
210         strncpy(sv_type, "none", 5);
211 #endif /* CONFIG_MMU */
212         seq_printf(f, "mmu\t\t: %s\n", sv_type);
213 }
214
215 static void *c_start(struct seq_file *m, loff_t *pos)
216 {
217         if (*pos == nr_cpu_ids)
218                 return NULL;
219
220         *pos = cpumask_next(*pos - 1, cpu_online_mask);
221         if ((*pos) < nr_cpu_ids)
222                 return (void *)(uintptr_t)(1 + *pos);
223         return NULL;
224 }
225
226 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
227 {
228         (*pos)++;
229         return c_start(m, pos);
230 }
231
232 static void c_stop(struct seq_file *m, void *v)
233 {
234 }
235
236 static int c_show(struct seq_file *m, void *v)
237 {
238         unsigned long cpu_id = (unsigned long)v - 1;
239         struct device_node *node = of_get_cpu_node(cpu_id, NULL);
240         struct riscv_cpuinfo *ci = per_cpu_ptr(&riscv_cpuinfo, cpu_id);
241         const char *compat, *isa;
242
243         seq_printf(m, "processor\t: %lu\n", cpu_id);
244         seq_printf(m, "hart\t\t: %lu\n", cpuid_to_hartid_map(cpu_id));
245         if (!of_property_read_string(node, "riscv,isa", &isa))
246                 print_isa(m, isa);
247         print_mmu(m);
248         if (!of_property_read_string(node, "compatible", &compat)
249             && strcmp(compat, "riscv"))
250                 seq_printf(m, "uarch\t\t: %s\n", compat);
251         seq_printf(m, "mvendorid\t: 0x%lx\n", ci->mvendorid);
252         seq_printf(m, "marchid\t\t: 0x%lx\n", ci->marchid);
253         seq_printf(m, "mimpid\t\t: 0x%lx\n", ci->mimpid);
254         seq_puts(m, "\n");
255         of_node_put(node);
256
257         return 0;
258 }
259
260 const struct seq_operations cpuinfo_op = {
261         .start  = c_start,
262         .next   = c_next,
263         .stop   = c_stop,
264         .show   = c_show
265 };
266
267 #endif /* CONFIG_PROC_FS */