riscv: Fix syscall wrapper for >word-size arguments
[sfrench/cifs-2.6.git] / arch / riscv / kernel / cpufeature.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copied from arch/arm64/kernel/cpufeature.c
4  *
5  * Copyright (C) 2015 ARM Ltd.
6  * Copyright (C) 2017 SiFive
7  */
8
9 #include <linux/acpi.h>
10 #include <linux/bitmap.h>
11 #include <linux/cpu.h>
12 #include <linux/cpuhotplug.h>
13 #include <linux/ctype.h>
14 #include <linux/log2.h>
15 #include <linux/memory.h>
16 #include <linux/module.h>
17 #include <linux/of.h>
18 #include <asm/acpi.h>
19 #include <asm/alternative.h>
20 #include <asm/cacheflush.h>
21 #include <asm/cpufeature.h>
22 #include <asm/hwcap.h>
23 #include <asm/patch.h>
24 #include <asm/processor.h>
25 #include <asm/vector.h>
26
27 #define NUM_ALPHA_EXTS ('z' - 'a' + 1)
28
29 unsigned long elf_hwcap __read_mostly;
30
31 /* Host ISA bitmap */
32 static DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX) __read_mostly;
33
34 /* Per-cpu ISA extensions. */
35 struct riscv_isainfo hart_isa[NR_CPUS];
36
37 /**
38  * riscv_isa_extension_base() - Get base extension word
39  *
40  * @isa_bitmap: ISA bitmap to use
41  * Return: base extension word as unsigned long value
42  *
43  * NOTE: If isa_bitmap is NULL then Host ISA bitmap will be used.
44  */
45 unsigned long riscv_isa_extension_base(const unsigned long *isa_bitmap)
46 {
47         if (!isa_bitmap)
48                 return riscv_isa[0];
49         return isa_bitmap[0];
50 }
51 EXPORT_SYMBOL_GPL(riscv_isa_extension_base);
52
53 /**
54  * __riscv_isa_extension_available() - Check whether given extension
55  * is available or not
56  *
57  * @isa_bitmap: ISA bitmap to use
58  * @bit: bit position of the desired extension
59  * Return: true or false
60  *
61  * NOTE: If isa_bitmap is NULL then Host ISA bitmap will be used.
62  */
63 bool __riscv_isa_extension_available(const unsigned long *isa_bitmap, unsigned int bit)
64 {
65         const unsigned long *bmap = (isa_bitmap) ? isa_bitmap : riscv_isa;
66
67         if (bit >= RISCV_ISA_EXT_MAX)
68                 return false;
69
70         return test_bit(bit, bmap) ? true : false;
71 }
72 EXPORT_SYMBOL_GPL(__riscv_isa_extension_available);
73
74 static bool riscv_isa_extension_check(int id)
75 {
76         switch (id) {
77         case RISCV_ISA_EXT_ZICBOM:
78                 if (!riscv_cbom_block_size) {
79                         pr_err("Zicbom detected in ISA string, disabling as no cbom-block-size found\n");
80                         return false;
81                 } else if (!is_power_of_2(riscv_cbom_block_size)) {
82                         pr_err("Zicbom disabled as cbom-block-size present, but is not a power-of-2\n");
83                         return false;
84                 }
85                 return true;
86         case RISCV_ISA_EXT_ZICBOZ:
87                 if (!riscv_cboz_block_size) {
88                         pr_err("Zicboz detected in ISA string, disabling as no cboz-block-size found\n");
89                         return false;
90                 } else if (!is_power_of_2(riscv_cboz_block_size)) {
91                         pr_err("Zicboz disabled as cboz-block-size present, but is not a power-of-2\n");
92                         return false;
93                 }
94                 return true;
95         case RISCV_ISA_EXT_INVALID:
96                 return false;
97         }
98
99         return true;
100 }
101
102 #define _RISCV_ISA_EXT_DATA(_name, _id, _subset_exts, _subset_exts_size) {      \
103         .name = #_name,                                                         \
104         .property = #_name,                                                     \
105         .id = _id,                                                              \
106         .subset_ext_ids = _subset_exts,                                         \
107         .subset_ext_size = _subset_exts_size                                    \
108 }
109
110 #define __RISCV_ISA_EXT_DATA(_name, _id) _RISCV_ISA_EXT_DATA(_name, _id, NULL, 0)
111
112 /* Used to declare pure "lasso" extension (Zk for instance) */
113 #define __RISCV_ISA_EXT_BUNDLE(_name, _bundled_exts) \
114         _RISCV_ISA_EXT_DATA(_name, RISCV_ISA_EXT_INVALID, _bundled_exts, ARRAY_SIZE(_bundled_exts))
115
116 /* Used to declare extensions that are a superset of other extensions (Zvbb for instance) */
117 #define __RISCV_ISA_EXT_SUPERSET(_name, _id, _sub_exts) \
118         _RISCV_ISA_EXT_DATA(_name, _id, _sub_exts, ARRAY_SIZE(_sub_exts))
119
120 static const unsigned int riscv_zk_bundled_exts[] = {
121         RISCV_ISA_EXT_ZBKB,
122         RISCV_ISA_EXT_ZBKC,
123         RISCV_ISA_EXT_ZBKX,
124         RISCV_ISA_EXT_ZKND,
125         RISCV_ISA_EXT_ZKNE,
126         RISCV_ISA_EXT_ZKR,
127         RISCV_ISA_EXT_ZKT,
128 };
129
130 static const unsigned int riscv_zkn_bundled_exts[] = {
131         RISCV_ISA_EXT_ZBKB,
132         RISCV_ISA_EXT_ZBKC,
133         RISCV_ISA_EXT_ZBKX,
134         RISCV_ISA_EXT_ZKND,
135         RISCV_ISA_EXT_ZKNE,
136         RISCV_ISA_EXT_ZKNH,
137 };
138
139 static const unsigned int riscv_zks_bundled_exts[] = {
140         RISCV_ISA_EXT_ZBKB,
141         RISCV_ISA_EXT_ZBKC,
142         RISCV_ISA_EXT_ZKSED,
143         RISCV_ISA_EXT_ZKSH
144 };
145
146 #define RISCV_ISA_EXT_ZVKN      \
147         RISCV_ISA_EXT_ZVKNED,   \
148         RISCV_ISA_EXT_ZVKNHB,   \
149         RISCV_ISA_EXT_ZVKB,     \
150         RISCV_ISA_EXT_ZVKT
151
152 static const unsigned int riscv_zvkn_bundled_exts[] = {
153         RISCV_ISA_EXT_ZVKN
154 };
155
156 static const unsigned int riscv_zvknc_bundled_exts[] = {
157         RISCV_ISA_EXT_ZVKN,
158         RISCV_ISA_EXT_ZVBC
159 };
160
161 static const unsigned int riscv_zvkng_bundled_exts[] = {
162         RISCV_ISA_EXT_ZVKN,
163         RISCV_ISA_EXT_ZVKG
164 };
165
166 #define RISCV_ISA_EXT_ZVKS      \
167         RISCV_ISA_EXT_ZVKSED,   \
168         RISCV_ISA_EXT_ZVKSH,    \
169         RISCV_ISA_EXT_ZVKB,     \
170         RISCV_ISA_EXT_ZVKT
171
172 static const unsigned int riscv_zvks_bundled_exts[] = {
173         RISCV_ISA_EXT_ZVKS
174 };
175
176 static const unsigned int riscv_zvksc_bundled_exts[] = {
177         RISCV_ISA_EXT_ZVKS,
178         RISCV_ISA_EXT_ZVBC
179 };
180
181 static const unsigned int riscv_zvksg_bundled_exts[] = {
182         RISCV_ISA_EXT_ZVKS,
183         RISCV_ISA_EXT_ZVKG
184 };
185
186 static const unsigned int riscv_zvbb_exts[] = {
187         RISCV_ISA_EXT_ZVKB
188 };
189
190 /*
191  * The canonical order of ISA extension names in the ISA string is defined in
192  * chapter 27 of the unprivileged specification.
193  *
194  * Ordinarily, for in-kernel data structures, this order is unimportant but
195  * isa_ext_arr defines the order of the ISA string in /proc/cpuinfo.
196  *
197  * The specification uses vague wording, such as should, when it comes to
198  * ordering, so for our purposes the following rules apply:
199  *
200  * 1. All multi-letter extensions must be separated from other extensions by an
201  *    underscore.
202  *
203  * 2. Additional standard extensions (starting with 'Z') must be sorted after
204  *    single-letter extensions and before any higher-privileged extensions.
205  *
206  * 3. The first letter following the 'Z' conventionally indicates the most
207  *    closely related alphabetical extension category, IMAFDQLCBKJTPVH.
208  *    If multiple 'Z' extensions are named, they must be ordered first by
209  *    category, then alphabetically within a category.
210  *
211  * 3. Standard supervisor-level extensions (starting with 'S') must be listed
212  *    after standard unprivileged extensions.  If multiple supervisor-level
213  *    extensions are listed, they must be ordered alphabetically.
214  *
215  * 4. Standard machine-level extensions (starting with 'Zxm') must be listed
216  *    after any lower-privileged, standard extensions.  If multiple
217  *    machine-level extensions are listed, they must be ordered
218  *    alphabetically.
219  *
220  * 5. Non-standard extensions (starting with 'X') must be listed after all
221  *    standard extensions. If multiple non-standard extensions are listed, they
222  *    must be ordered alphabetically.
223  *
224  * An example string following the order is:
225  *    rv64imadc_zifoo_zigoo_zafoo_sbar_scar_zxmbaz_xqux_xrux
226  *
227  * New entries to this struct should follow the ordering rules described above.
228  */
229 const struct riscv_isa_ext_data riscv_isa_ext[] = {
230         __RISCV_ISA_EXT_DATA(i, RISCV_ISA_EXT_i),
231         __RISCV_ISA_EXT_DATA(m, RISCV_ISA_EXT_m),
232         __RISCV_ISA_EXT_DATA(a, RISCV_ISA_EXT_a),
233         __RISCV_ISA_EXT_DATA(f, RISCV_ISA_EXT_f),
234         __RISCV_ISA_EXT_DATA(d, RISCV_ISA_EXT_d),
235         __RISCV_ISA_EXT_DATA(q, RISCV_ISA_EXT_q),
236         __RISCV_ISA_EXT_DATA(c, RISCV_ISA_EXT_c),
237         __RISCV_ISA_EXT_DATA(v, RISCV_ISA_EXT_v),
238         __RISCV_ISA_EXT_DATA(h, RISCV_ISA_EXT_h),
239         __RISCV_ISA_EXT_DATA(zicbom, RISCV_ISA_EXT_ZICBOM),
240         __RISCV_ISA_EXT_DATA(zicboz, RISCV_ISA_EXT_ZICBOZ),
241         __RISCV_ISA_EXT_DATA(zicntr, RISCV_ISA_EXT_ZICNTR),
242         __RISCV_ISA_EXT_DATA(zicond, RISCV_ISA_EXT_ZICOND),
243         __RISCV_ISA_EXT_DATA(zicsr, RISCV_ISA_EXT_ZICSR),
244         __RISCV_ISA_EXT_DATA(zifencei, RISCV_ISA_EXT_ZIFENCEI),
245         __RISCV_ISA_EXT_DATA(zihintntl, RISCV_ISA_EXT_ZIHINTNTL),
246         __RISCV_ISA_EXT_DATA(zihintpause, RISCV_ISA_EXT_ZIHINTPAUSE),
247         __RISCV_ISA_EXT_DATA(zihpm, RISCV_ISA_EXT_ZIHPM),
248         __RISCV_ISA_EXT_DATA(zacas, RISCV_ISA_EXT_ZACAS),
249         __RISCV_ISA_EXT_DATA(zfa, RISCV_ISA_EXT_ZFA),
250         __RISCV_ISA_EXT_DATA(zfh, RISCV_ISA_EXT_ZFH),
251         __RISCV_ISA_EXT_DATA(zfhmin, RISCV_ISA_EXT_ZFHMIN),
252         __RISCV_ISA_EXT_DATA(zba, RISCV_ISA_EXT_ZBA),
253         __RISCV_ISA_EXT_DATA(zbb, RISCV_ISA_EXT_ZBB),
254         __RISCV_ISA_EXT_DATA(zbc, RISCV_ISA_EXT_ZBC),
255         __RISCV_ISA_EXT_DATA(zbkb, RISCV_ISA_EXT_ZBKB),
256         __RISCV_ISA_EXT_DATA(zbkc, RISCV_ISA_EXT_ZBKC),
257         __RISCV_ISA_EXT_DATA(zbkx, RISCV_ISA_EXT_ZBKX),
258         __RISCV_ISA_EXT_DATA(zbs, RISCV_ISA_EXT_ZBS),
259         __RISCV_ISA_EXT_BUNDLE(zk, riscv_zk_bundled_exts),
260         __RISCV_ISA_EXT_BUNDLE(zkn, riscv_zkn_bundled_exts),
261         __RISCV_ISA_EXT_DATA(zknd, RISCV_ISA_EXT_ZKND),
262         __RISCV_ISA_EXT_DATA(zkne, RISCV_ISA_EXT_ZKNE),
263         __RISCV_ISA_EXT_DATA(zknh, RISCV_ISA_EXT_ZKNH),
264         __RISCV_ISA_EXT_DATA(zkr, RISCV_ISA_EXT_ZKR),
265         __RISCV_ISA_EXT_BUNDLE(zks, riscv_zks_bundled_exts),
266         __RISCV_ISA_EXT_DATA(zkt, RISCV_ISA_EXT_ZKT),
267         __RISCV_ISA_EXT_DATA(zksed, RISCV_ISA_EXT_ZKSED),
268         __RISCV_ISA_EXT_DATA(zksh, RISCV_ISA_EXT_ZKSH),
269         __RISCV_ISA_EXT_DATA(ztso, RISCV_ISA_EXT_ZTSO),
270         __RISCV_ISA_EXT_SUPERSET(zvbb, RISCV_ISA_EXT_ZVBB, riscv_zvbb_exts),
271         __RISCV_ISA_EXT_DATA(zvbc, RISCV_ISA_EXT_ZVBC),
272         __RISCV_ISA_EXT_DATA(zvfh, RISCV_ISA_EXT_ZVFH),
273         __RISCV_ISA_EXT_DATA(zvfhmin, RISCV_ISA_EXT_ZVFHMIN),
274         __RISCV_ISA_EXT_DATA(zvkb, RISCV_ISA_EXT_ZVKB),
275         __RISCV_ISA_EXT_DATA(zvkg, RISCV_ISA_EXT_ZVKG),
276         __RISCV_ISA_EXT_BUNDLE(zvkn, riscv_zvkn_bundled_exts),
277         __RISCV_ISA_EXT_BUNDLE(zvknc, riscv_zvknc_bundled_exts),
278         __RISCV_ISA_EXT_DATA(zvkned, RISCV_ISA_EXT_ZVKNED),
279         __RISCV_ISA_EXT_BUNDLE(zvkng, riscv_zvkng_bundled_exts),
280         __RISCV_ISA_EXT_DATA(zvknha, RISCV_ISA_EXT_ZVKNHA),
281         __RISCV_ISA_EXT_DATA(zvknhb, RISCV_ISA_EXT_ZVKNHB),
282         __RISCV_ISA_EXT_BUNDLE(zvks, riscv_zvks_bundled_exts),
283         __RISCV_ISA_EXT_BUNDLE(zvksc, riscv_zvksc_bundled_exts),
284         __RISCV_ISA_EXT_DATA(zvksed, RISCV_ISA_EXT_ZVKSED),
285         __RISCV_ISA_EXT_DATA(zvksh, RISCV_ISA_EXT_ZVKSH),
286         __RISCV_ISA_EXT_BUNDLE(zvksg, riscv_zvksg_bundled_exts),
287         __RISCV_ISA_EXT_DATA(zvkt, RISCV_ISA_EXT_ZVKT),
288         __RISCV_ISA_EXT_DATA(smaia, RISCV_ISA_EXT_SMAIA),
289         __RISCV_ISA_EXT_DATA(smstateen, RISCV_ISA_EXT_SMSTATEEN),
290         __RISCV_ISA_EXT_DATA(ssaia, RISCV_ISA_EXT_SSAIA),
291         __RISCV_ISA_EXT_DATA(sscofpmf, RISCV_ISA_EXT_SSCOFPMF),
292         __RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC),
293         __RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL),
294         __RISCV_ISA_EXT_DATA(svnapot, RISCV_ISA_EXT_SVNAPOT),
295         __RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),
296         __RISCV_ISA_EXT_DATA(xandespmu, RISCV_ISA_EXT_XANDESPMU),
297 };
298
299 const size_t riscv_isa_ext_count = ARRAY_SIZE(riscv_isa_ext);
300
301 static void __init match_isa_ext(const struct riscv_isa_ext_data *ext, const char *name,
302                                  const char *name_end, struct riscv_isainfo *isainfo)
303 {
304         if ((name_end - name == strlen(ext->name)) &&
305              !strncasecmp(name, ext->name, name_end - name)) {
306                 /*
307                  * If this is a bundle, enable all the ISA extensions that
308                  * comprise the bundle.
309                  */
310                 if (ext->subset_ext_size) {
311                         for (int i = 0; i < ext->subset_ext_size; i++) {
312                                 if (riscv_isa_extension_check(ext->subset_ext_ids[i]))
313                                         set_bit(ext->subset_ext_ids[i], isainfo->isa);
314                         }
315                 }
316
317                 /*
318                  * This is valid even for bundle extensions which uses the RISCV_ISA_EXT_INVALID id
319                  * (rejected by riscv_isa_extension_check()).
320                  */
321                 if (riscv_isa_extension_check(ext->id))
322                         set_bit(ext->id, isainfo->isa);
323         }
324 }
325
326 static void __init riscv_parse_isa_string(unsigned long *this_hwcap, struct riscv_isainfo *isainfo,
327                                           unsigned long *isa2hwcap, const char *isa)
328 {
329         /*
330          * For all possible cpus, we have already validated in
331          * the boot process that they at least contain "rv" and
332          * whichever of "32"/"64" this kernel supports, and so this
333          * section can be skipped.
334          */
335         isa += 4;
336
337         while (*isa) {
338                 const char *ext = isa++;
339                 const char *ext_end = isa;
340                 bool ext_long = false, ext_err = false;
341
342                 switch (*ext) {
343                 case 's':
344                         /*
345                          * Workaround for invalid single-letter 's' & 'u' (QEMU).
346                          * No need to set the bit in riscv_isa as 's' & 'u' are
347                          * not valid ISA extensions. It works unless the first
348                          * multi-letter extension in the ISA string begins with
349                          * "Su" and is not prefixed with an underscore.
350                          */
351                         if (ext[-1] != '_' && ext[1] == 'u') {
352                                 ++isa;
353                                 ext_err = true;
354                                 break;
355                         }
356                         fallthrough;
357                 case 'S':
358                 case 'x':
359                 case 'X':
360                 case 'z':
361                 case 'Z':
362                         /*
363                          * Before attempting to parse the extension itself, we find its end.
364                          * As multi-letter extensions must be split from other multi-letter
365                          * extensions with an "_", the end of a multi-letter extension will
366                          * either be the null character or the "_" at the start of the next
367                          * multi-letter extension.
368                          *
369                          * Next, as the extensions version is currently ignored, we
370                          * eliminate that portion. This is done by parsing backwards from
371                          * the end of the extension, removing any numbers. This may be a
372                          * major or minor number however, so the process is repeated if a
373                          * minor number was found.
374                          *
375                          * ext_end is intended to represent the first character *after* the
376                          * name portion of an extension, but will be decremented to the last
377                          * character itself while eliminating the extensions version number.
378                          * A simple re-increment solves this problem.
379                          */
380                         ext_long = true;
381                         for (; *isa && *isa != '_'; ++isa)
382                                 if (unlikely(!isalnum(*isa)))
383                                         ext_err = true;
384
385                         ext_end = isa;
386                         if (unlikely(ext_err))
387                                 break;
388
389                         if (!isdigit(ext_end[-1]))
390                                 break;
391
392                         while (isdigit(*--ext_end))
393                                 ;
394
395                         if (tolower(ext_end[0]) != 'p' || !isdigit(ext_end[-1])) {
396                                 ++ext_end;
397                                 break;
398                         }
399
400                         while (isdigit(*--ext_end))
401                                 ;
402
403                         ++ext_end;
404                         break;
405                 default:
406                         /*
407                          * Things are a little easier for single-letter extensions, as they
408                          * are parsed forwards.
409                          *
410                          * After checking that our starting position is valid, we need to
411                          * ensure that, when isa was incremented at the start of the loop,
412                          * that it arrived at the start of the next extension.
413                          *
414                          * If we are already on a non-digit, there is nothing to do. Either
415                          * we have a multi-letter extension's _, or the start of an
416                          * extension.
417                          *
418                          * Otherwise we have found the current extension's major version
419                          * number. Parse past it, and a subsequent p/minor version number
420                          * if present. The `p` extension must not appear immediately after
421                          * a number, so there is no fear of missing it.
422                          *
423                          */
424                         if (unlikely(!isalpha(*ext))) {
425                                 ext_err = true;
426                                 break;
427                         }
428
429                         if (!isdigit(*isa))
430                                 break;
431
432                         while (isdigit(*++isa))
433                                 ;
434
435                         if (tolower(*isa) != 'p')
436                                 break;
437
438                         if (!isdigit(*++isa)) {
439                                 --isa;
440                                 break;
441                         }
442
443                         while (isdigit(*++isa))
444                                 ;
445
446                         break;
447                 }
448
449                 /*
450                  * The parser expects that at the start of an iteration isa points to the
451                  * first character of the next extension. As we stop parsing an extension
452                  * on meeting a non-alphanumeric character, an extra increment is needed
453                  * where the succeeding extension is a multi-letter prefixed with an "_".
454                  */
455                 if (*isa == '_')
456                         ++isa;
457
458                 if (unlikely(ext_err))
459                         continue;
460                 if (!ext_long) {
461                         int nr = tolower(*ext) - 'a';
462
463                         if (riscv_isa_extension_check(nr)) {
464                                 *this_hwcap |= isa2hwcap[nr];
465                                 set_bit(nr, isainfo->isa);
466                         }
467                 } else {
468                         for (int i = 0; i < riscv_isa_ext_count; i++)
469                                 match_isa_ext(&riscv_isa_ext[i], ext, ext_end, isainfo);
470                 }
471         }
472 }
473
474 static void __init riscv_fill_hwcap_from_isa_string(unsigned long *isa2hwcap)
475 {
476         struct device_node *node;
477         const char *isa;
478         int rc;
479         struct acpi_table_header *rhct;
480         acpi_status status;
481         unsigned int cpu;
482
483         if (!acpi_disabled) {
484                 status = acpi_get_table(ACPI_SIG_RHCT, 0, &rhct);
485                 if (ACPI_FAILURE(status))
486                         return;
487         }
488
489         for_each_possible_cpu(cpu) {
490                 struct riscv_isainfo *isainfo = &hart_isa[cpu];
491                 unsigned long this_hwcap = 0;
492
493                 if (acpi_disabled) {
494                         node = of_cpu_device_node_get(cpu);
495                         if (!node) {
496                                 pr_warn("Unable to find cpu node\n");
497                                 continue;
498                         }
499
500                         rc = of_property_read_string(node, "riscv,isa", &isa);
501                         of_node_put(node);
502                         if (rc) {
503                                 pr_warn("Unable to find \"riscv,isa\" devicetree entry\n");
504                                 continue;
505                         }
506                 } else {
507                         rc = acpi_get_riscv_isa(rhct, cpu, &isa);
508                         if (rc < 0) {
509                                 pr_warn("Unable to get ISA for the hart - %d\n", cpu);
510                                 continue;
511                         }
512                 }
513
514                 riscv_parse_isa_string(&this_hwcap, isainfo, isa2hwcap, isa);
515
516                 /*
517                  * These ones were as they were part of the base ISA when the
518                  * port & dt-bindings were upstreamed, and so can be set
519                  * unconditionally where `i` is in riscv,isa on DT systems.
520                  */
521                 if (acpi_disabled) {
522                         set_bit(RISCV_ISA_EXT_ZICSR, isainfo->isa);
523                         set_bit(RISCV_ISA_EXT_ZIFENCEI, isainfo->isa);
524                         set_bit(RISCV_ISA_EXT_ZICNTR, isainfo->isa);
525                         set_bit(RISCV_ISA_EXT_ZIHPM, isainfo->isa);
526                 }
527
528                 /*
529                  * All "okay" hart should have same isa. Set HWCAP based on
530                  * common capabilities of every "okay" hart, in case they don't
531                  * have.
532                  */
533                 if (elf_hwcap)
534                         elf_hwcap &= this_hwcap;
535                 else
536                         elf_hwcap = this_hwcap;
537
538                 if (bitmap_empty(riscv_isa, RISCV_ISA_EXT_MAX))
539                         bitmap_copy(riscv_isa, isainfo->isa, RISCV_ISA_EXT_MAX);
540                 else
541                         bitmap_and(riscv_isa, riscv_isa, isainfo->isa, RISCV_ISA_EXT_MAX);
542         }
543
544         if (!acpi_disabled && rhct)
545                 acpi_put_table((struct acpi_table_header *)rhct);
546 }
547
548 static int __init riscv_fill_hwcap_from_ext_list(unsigned long *isa2hwcap)
549 {
550         unsigned int cpu;
551
552         for_each_possible_cpu(cpu) {
553                 unsigned long this_hwcap = 0;
554                 struct device_node *cpu_node;
555                 struct riscv_isainfo *isainfo = &hart_isa[cpu];
556
557                 cpu_node = of_cpu_device_node_get(cpu);
558                 if (!cpu_node) {
559                         pr_warn("Unable to find cpu node\n");
560                         continue;
561                 }
562
563                 if (!of_property_present(cpu_node, "riscv,isa-extensions")) {
564                         of_node_put(cpu_node);
565                         continue;
566                 }
567
568                 for (int i = 0; i < riscv_isa_ext_count; i++) {
569                         const struct riscv_isa_ext_data *ext = &riscv_isa_ext[i];
570
571                         if (of_property_match_string(cpu_node, "riscv,isa-extensions",
572                                                      ext->property) < 0)
573                                 continue;
574
575                         if (ext->subset_ext_size) {
576                                 for (int j = 0; j < ext->subset_ext_size; j++) {
577                                         if (riscv_isa_extension_check(ext->subset_ext_ids[i]))
578                                                 set_bit(ext->subset_ext_ids[j], isainfo->isa);
579                                 }
580                         }
581
582                         if (riscv_isa_extension_check(ext->id)) {
583                                 set_bit(ext->id, isainfo->isa);
584
585                                 /* Only single letter extensions get set in hwcap */
586                                 if (strnlen(riscv_isa_ext[i].name, 2) == 1)
587                                         this_hwcap |= isa2hwcap[riscv_isa_ext[i].id];
588                         }
589                 }
590
591                 of_node_put(cpu_node);
592
593                 /*
594                  * All "okay" harts should have same isa. Set HWCAP based on
595                  * common capabilities of every "okay" hart, in case they don't.
596                  */
597                 if (elf_hwcap)
598                         elf_hwcap &= this_hwcap;
599                 else
600                         elf_hwcap = this_hwcap;
601
602                 if (bitmap_empty(riscv_isa, RISCV_ISA_EXT_MAX))
603                         bitmap_copy(riscv_isa, isainfo->isa, RISCV_ISA_EXT_MAX);
604                 else
605                         bitmap_and(riscv_isa, riscv_isa, isainfo->isa, RISCV_ISA_EXT_MAX);
606         }
607
608         if (bitmap_empty(riscv_isa, RISCV_ISA_EXT_MAX))
609                 return -ENOENT;
610
611         return 0;
612 }
613
614 #ifdef CONFIG_RISCV_ISA_FALLBACK
615 bool __initdata riscv_isa_fallback = true;
616 #else
617 bool __initdata riscv_isa_fallback;
618 static int __init riscv_isa_fallback_setup(char *__unused)
619 {
620         riscv_isa_fallback = true;
621         return 1;
622 }
623 early_param("riscv_isa_fallback", riscv_isa_fallback_setup);
624 #endif
625
626 void __init riscv_fill_hwcap(void)
627 {
628         char print_str[NUM_ALPHA_EXTS + 1];
629         unsigned long isa2hwcap[26] = {0};
630         int i, j;
631
632         isa2hwcap['i' - 'a'] = COMPAT_HWCAP_ISA_I;
633         isa2hwcap['m' - 'a'] = COMPAT_HWCAP_ISA_M;
634         isa2hwcap['a' - 'a'] = COMPAT_HWCAP_ISA_A;
635         isa2hwcap['f' - 'a'] = COMPAT_HWCAP_ISA_F;
636         isa2hwcap['d' - 'a'] = COMPAT_HWCAP_ISA_D;
637         isa2hwcap['c' - 'a'] = COMPAT_HWCAP_ISA_C;
638         isa2hwcap['v' - 'a'] = COMPAT_HWCAP_ISA_V;
639
640         if (!acpi_disabled) {
641                 riscv_fill_hwcap_from_isa_string(isa2hwcap);
642         } else {
643                 int ret = riscv_fill_hwcap_from_ext_list(isa2hwcap);
644
645                 if (ret && riscv_isa_fallback) {
646                         pr_info("Falling back to deprecated \"riscv,isa\"\n");
647                         riscv_fill_hwcap_from_isa_string(isa2hwcap);
648                 }
649         }
650
651         /*
652          * We don't support systems with F but without D, so mask those out
653          * here.
654          */
655         if ((elf_hwcap & COMPAT_HWCAP_ISA_F) && !(elf_hwcap & COMPAT_HWCAP_ISA_D)) {
656                 pr_info("This kernel does not support systems with F but not D\n");
657                 elf_hwcap &= ~COMPAT_HWCAP_ISA_F;
658         }
659
660         if (elf_hwcap & COMPAT_HWCAP_ISA_V) {
661                 riscv_v_setup_vsize();
662                 /*
663                  * ISA string in device tree might have 'v' flag, but
664                  * CONFIG_RISCV_ISA_V is disabled in kernel.
665                  * Clear V flag in elf_hwcap if CONFIG_RISCV_ISA_V is disabled.
666                  */
667                 if (!IS_ENABLED(CONFIG_RISCV_ISA_V))
668                         elf_hwcap &= ~COMPAT_HWCAP_ISA_V;
669         }
670
671         memset(print_str, 0, sizeof(print_str));
672         for (i = 0, j = 0; i < NUM_ALPHA_EXTS; i++)
673                 if (riscv_isa[0] & BIT_MASK(i))
674                         print_str[j++] = (char)('a' + i);
675         pr_info("riscv: base ISA extensions %s\n", print_str);
676
677         memset(print_str, 0, sizeof(print_str));
678         for (i = 0, j = 0; i < NUM_ALPHA_EXTS; i++)
679                 if (elf_hwcap & BIT_MASK(i))
680                         print_str[j++] = (char)('a' + i);
681         pr_info("riscv: ELF capabilities %s\n", print_str);
682 }
683
684 unsigned long riscv_get_elf_hwcap(void)
685 {
686         unsigned long hwcap;
687
688         hwcap = (elf_hwcap & ((1UL << RISCV_ISA_EXT_BASE) - 1));
689
690         if (!riscv_v_vstate_ctrl_user_allowed())
691                 hwcap &= ~COMPAT_HWCAP_ISA_V;
692
693         return hwcap;
694 }
695
696 void riscv_user_isa_enable(void)
697 {
698         if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_ZICBOZ))
699                 csr_set(CSR_SENVCFG, ENVCFG_CBZE);
700 }
701
702 #ifdef CONFIG_RISCV_ALTERNATIVE
703 /*
704  * Alternative patch sites consider 48 bits when determining when to patch
705  * the old instruction sequence with the new. These bits are broken into a
706  * 16-bit vendor ID and a 32-bit patch ID. A non-zero vendor ID means the
707  * patch site is for an erratum, identified by the 32-bit patch ID. When
708  * the vendor ID is zero, the patch site is for a cpufeature. cpufeatures
709  * further break down patch ID into two 16-bit numbers. The lower 16 bits
710  * are the cpufeature ID and the upper 16 bits are used for a value specific
711  * to the cpufeature and patch site. If the upper 16 bits are zero, then it
712  * implies no specific value is specified. cpufeatures that want to control
713  * patching on a per-site basis will provide non-zero values and implement
714  * checks here. The checks return true when patching should be done, and
715  * false otherwise.
716  */
717 static bool riscv_cpufeature_patch_check(u16 id, u16 value)
718 {
719         if (!value)
720                 return true;
721
722         switch (id) {
723         case RISCV_ISA_EXT_ZICBOZ:
724                 /*
725                  * Zicboz alternative applications provide the maximum
726                  * supported block size order, or zero when it doesn't
727                  * matter. If the current block size exceeds the maximum,
728                  * then the alternative cannot be applied.
729                  */
730                 return riscv_cboz_block_size <= (1U << value);
731         }
732
733         return false;
734 }
735
736 void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,
737                                                   struct alt_entry *end,
738                                                   unsigned int stage)
739 {
740         struct alt_entry *alt;
741         void *oldptr, *altptr;
742         u16 id, value;
743
744         if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
745                 return;
746
747         for (alt = begin; alt < end; alt++) {
748                 if (alt->vendor_id != 0)
749                         continue;
750
751                 id = PATCH_ID_CPUFEATURE_ID(alt->patch_id);
752
753                 if (id >= RISCV_ISA_EXT_MAX) {
754                         WARN(1, "This extension id:%d is not in ISA extension list", id);
755                         continue;
756                 }
757
758                 if (!__riscv_isa_extension_available(NULL, id))
759                         continue;
760
761                 value = PATCH_ID_CPUFEATURE_VALUE(alt->patch_id);
762                 if (!riscv_cpufeature_patch_check(id, value))
763                         continue;
764
765                 oldptr = ALT_OLD_PTR(alt);
766                 altptr = ALT_ALT_PTR(alt);
767
768                 mutex_lock(&text_mutex);
769                 patch_text_nosync(oldptr, altptr, alt->alt_len);
770                 riscv_alternative_fix_offsets(oldptr, alt->alt_len, oldptr - altptr);
771                 mutex_unlock(&text_mutex);
772         }
773 }
774 #endif