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/sbi.h> 26 #include <asm/vector.h> 27 #include <asm/vendor_extensions.h> 28 #include <asm/vendor_extensions/thead.h> 29 30 #define NUM_ALPHA_EXTS ('z' - 'a' + 1) 31 32 static bool any_cpu_has_zicboz; 33 34 unsigned long elf_hwcap __read_mostly; 35 36 /* Host ISA bitmap */ 37 static DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX) __read_mostly; 38 39 /* Per-cpu ISA extensions. */ 40 struct riscv_isainfo hart_isa[NR_CPUS]; 41 42 u32 thead_vlenb_of; 43 44 /** 45 * riscv_isa_extension_base() - Get base extension word 46 * 47 * @isa_bitmap: ISA bitmap to use 48 * Return: base extension word as unsigned long value 49 * 50 * NOTE: If isa_bitmap is NULL then Host ISA bitmap will be used. 51 */ 52 unsigned long riscv_isa_extension_base(const unsigned long *isa_bitmap) 53 { 54 if (!isa_bitmap) 55 return riscv_isa[0]; 56 return isa_bitmap[0]; 57 } 58 EXPORT_SYMBOL_GPL(riscv_isa_extension_base); 59 60 /** 61 * __riscv_isa_extension_available() - Check whether given extension 62 * is available or not 63 * 64 * @isa_bitmap: ISA bitmap to use 65 * @bit: bit position of the desired extension 66 * Return: true or false 67 * 68 * NOTE: If isa_bitmap is NULL then Host ISA bitmap will be used. 69 */ 70 bool __riscv_isa_extension_available(const unsigned long *isa_bitmap, unsigned int bit) 71 { 72 const unsigned long *bmap = (isa_bitmap) ? isa_bitmap : riscv_isa; 73 74 if (bit >= RISCV_ISA_EXT_MAX) 75 return false; 76 77 return test_bit(bit, bmap) ? true : false; 78 } 79 EXPORT_SYMBOL_GPL(__riscv_isa_extension_available); 80 81 static int riscv_ext_zicbom_validate(const struct riscv_isa_ext_data *data, 82 const unsigned long *isa_bitmap) 83 { 84 if (!riscv_cbom_block_size) { 85 pr_err("Zicbom detected in ISA string, disabling as no cbom-block-size found\n"); 86 return -EINVAL; 87 } 88 if (!is_power_of_2(riscv_cbom_block_size)) { 89 pr_err("Zicbom disabled as cbom-block-size present, but is not a power-of-2\n"); 90 return -EINVAL; 91 } 92 return 0; 93 } 94 95 static int riscv_ext_zicboz_validate(const struct riscv_isa_ext_data *data, 96 const unsigned long *isa_bitmap) 97 { 98 if (!riscv_cboz_block_size) { 99 pr_err("Zicboz detected in ISA string, disabling as no cboz-block-size found\n"); 100 return -EINVAL; 101 } 102 if (!is_power_of_2(riscv_cboz_block_size)) { 103 pr_err("Zicboz disabled as cboz-block-size present, but is not a power-of-2\n"); 104 return -EINVAL; 105 } 106 any_cpu_has_zicboz = true; 107 return 0; 108 } 109 110 static int riscv_ext_zca_depends(const struct riscv_isa_ext_data *data, 111 const unsigned long *isa_bitmap) 112 { 113 if (__riscv_isa_extension_available(isa_bitmap, RISCV_ISA_EXT_ZCA)) 114 return 0; 115 116 return -EPROBE_DEFER; 117 } 118 static int riscv_ext_zcd_validate(const struct riscv_isa_ext_data *data, 119 const unsigned long *isa_bitmap) 120 { 121 if (__riscv_isa_extension_available(isa_bitmap, RISCV_ISA_EXT_ZCA) && 122 __riscv_isa_extension_available(isa_bitmap, RISCV_ISA_EXT_d)) 123 return 0; 124 125 return -EPROBE_DEFER; 126 } 127 128 static int riscv_ext_zcf_validate(const struct riscv_isa_ext_data *data, 129 const unsigned long *isa_bitmap) 130 { 131 if (IS_ENABLED(CONFIG_64BIT)) 132 return -EINVAL; 133 134 if (__riscv_isa_extension_available(isa_bitmap, RISCV_ISA_EXT_ZCA) && 135 __riscv_isa_extension_available(isa_bitmap, RISCV_ISA_EXT_f)) 136 return 0; 137 138 return -EPROBE_DEFER; 139 } 140 141 static const unsigned int riscv_zk_bundled_exts[] = { 142 RISCV_ISA_EXT_ZBKB, 143 RISCV_ISA_EXT_ZBKC, 144 RISCV_ISA_EXT_ZBKX, 145 RISCV_ISA_EXT_ZKND, 146 RISCV_ISA_EXT_ZKNE, 147 RISCV_ISA_EXT_ZKR, 148 RISCV_ISA_EXT_ZKT, 149 }; 150 151 static const unsigned int riscv_zkn_bundled_exts[] = { 152 RISCV_ISA_EXT_ZBKB, 153 RISCV_ISA_EXT_ZBKC, 154 RISCV_ISA_EXT_ZBKX, 155 RISCV_ISA_EXT_ZKND, 156 RISCV_ISA_EXT_ZKNE, 157 RISCV_ISA_EXT_ZKNH, 158 }; 159 160 static const unsigned int riscv_zks_bundled_exts[] = { 161 RISCV_ISA_EXT_ZBKB, 162 RISCV_ISA_EXT_ZBKC, 163 RISCV_ISA_EXT_ZKSED, 164 RISCV_ISA_EXT_ZKSH 165 }; 166 167 #define RISCV_ISA_EXT_ZVKN \ 168 RISCV_ISA_EXT_ZVKNED, \ 169 RISCV_ISA_EXT_ZVKNHB, \ 170 RISCV_ISA_EXT_ZVKB, \ 171 RISCV_ISA_EXT_ZVKT 172 173 static const unsigned int riscv_zvkn_bundled_exts[] = { 174 RISCV_ISA_EXT_ZVKN 175 }; 176 177 static const unsigned int riscv_zvknc_bundled_exts[] = { 178 RISCV_ISA_EXT_ZVKN, 179 RISCV_ISA_EXT_ZVBC 180 }; 181 182 static const unsigned int riscv_zvkng_bundled_exts[] = { 183 RISCV_ISA_EXT_ZVKN, 184 RISCV_ISA_EXT_ZVKG 185 }; 186 187 #define RISCV_ISA_EXT_ZVKS \ 188 RISCV_ISA_EXT_ZVKSED, \ 189 RISCV_ISA_EXT_ZVKSH, \ 190 RISCV_ISA_EXT_ZVKB, \ 191 RISCV_ISA_EXT_ZVKT 192 193 static const unsigned int riscv_zvks_bundled_exts[] = { 194 RISCV_ISA_EXT_ZVKS 195 }; 196 197 static const unsigned int riscv_zvksc_bundled_exts[] = { 198 RISCV_ISA_EXT_ZVKS, 199 RISCV_ISA_EXT_ZVBC 200 }; 201 202 static const unsigned int riscv_zvksg_bundled_exts[] = { 203 RISCV_ISA_EXT_ZVKS, 204 RISCV_ISA_EXT_ZVKG 205 }; 206 207 static const unsigned int riscv_zvbb_exts[] = { 208 RISCV_ISA_EXT_ZVKB 209 }; 210 211 #define RISCV_ISA_EXT_ZVE64F_IMPLY_LIST \ 212 RISCV_ISA_EXT_ZVE64X, \ 213 RISCV_ISA_EXT_ZVE32F, \ 214 RISCV_ISA_EXT_ZVE32X 215 216 #define RISCV_ISA_EXT_ZVE64D_IMPLY_LIST \ 217 RISCV_ISA_EXT_ZVE64F, \ 218 RISCV_ISA_EXT_ZVE64F_IMPLY_LIST 219 220 #define RISCV_ISA_EXT_V_IMPLY_LIST \ 221 RISCV_ISA_EXT_ZVE64D, \ 222 RISCV_ISA_EXT_ZVE64D_IMPLY_LIST 223 224 static const unsigned int riscv_zve32f_exts[] = { 225 RISCV_ISA_EXT_ZVE32X 226 }; 227 228 static const unsigned int riscv_zve64f_exts[] = { 229 RISCV_ISA_EXT_ZVE64F_IMPLY_LIST 230 }; 231 232 static const unsigned int riscv_zve64d_exts[] = { 233 RISCV_ISA_EXT_ZVE64D_IMPLY_LIST 234 }; 235 236 static const unsigned int riscv_v_exts[] = { 237 RISCV_ISA_EXT_V_IMPLY_LIST 238 }; 239 240 static const unsigned int riscv_zve64x_exts[] = { 241 RISCV_ISA_EXT_ZVE32X, 242 RISCV_ISA_EXT_ZVE64X 243 }; 244 245 /* 246 * While the [ms]envcfg CSRs were not defined until version 1.12 of the RISC-V 247 * privileged ISA, the existence of the CSRs is implied by any extension which 248 * specifies [ms]envcfg bit(s). Hence, we define a custom ISA extension for the 249 * existence of the CSR, and treat it as a subset of those other extensions. 250 */ 251 static const unsigned int riscv_xlinuxenvcfg_exts[] = { 252 RISCV_ISA_EXT_XLINUXENVCFG 253 }; 254 255 /* 256 * Zc* spec states that: 257 * - C always implies Zca 258 * - C+F implies Zcf (RV32 only) 259 * - C+D implies Zcd 260 * 261 * These extensions will be enabled and then validated depending on the 262 * availability of F/D RV32. 263 */ 264 static const unsigned int riscv_c_exts[] = { 265 RISCV_ISA_EXT_ZCA, 266 RISCV_ISA_EXT_ZCF, 267 RISCV_ISA_EXT_ZCD, 268 }; 269 270 /* 271 * The canonical order of ISA extension names in the ISA string is defined in 272 * chapter 27 of the unprivileged specification. 273 * 274 * Ordinarily, for in-kernel data structures, this order is unimportant but 275 * isa_ext_arr defines the order of the ISA string in /proc/cpuinfo. 276 * 277 * The specification uses vague wording, such as should, when it comes to 278 * ordering, so for our purposes the following rules apply: 279 * 280 * 1. All multi-letter extensions must be separated from other extensions by an 281 * underscore. 282 * 283 * 2. Additional standard extensions (starting with 'Z') must be sorted after 284 * single-letter extensions and before any higher-privileged extensions. 285 * 286 * 3. The first letter following the 'Z' conventionally indicates the most 287 * closely related alphabetical extension category, IMAFDQLCBKJTPVH. 288 * If multiple 'Z' extensions are named, they must be ordered first by 289 * category, then alphabetically within a category. 290 * 291 * 3. Standard supervisor-level extensions (starting with 'S') must be listed 292 * after standard unprivileged extensions. If multiple supervisor-level 293 * extensions are listed, they must be ordered alphabetically. 294 * 295 * 4. Standard machine-level extensions (starting with 'Zxm') must be listed 296 * after any lower-privileged, standard extensions. If multiple 297 * machine-level extensions are listed, they must be ordered 298 * alphabetically. 299 * 300 * 5. Non-standard extensions (starting with 'X') must be listed after all 301 * standard extensions. If multiple non-standard extensions are listed, they 302 * must be ordered alphabetically. 303 * 304 * An example string following the order is: 305 * rv64imadc_zifoo_zigoo_zafoo_sbar_scar_zxmbaz_xqux_xrux 306 * 307 * New entries to this struct should follow the ordering rules described above. 308 */ 309 const struct riscv_isa_ext_data riscv_isa_ext[] = { 310 __RISCV_ISA_EXT_DATA(i, RISCV_ISA_EXT_i), 311 __RISCV_ISA_EXT_DATA(m, RISCV_ISA_EXT_m), 312 __RISCV_ISA_EXT_DATA(a, RISCV_ISA_EXT_a), 313 __RISCV_ISA_EXT_DATA(f, RISCV_ISA_EXT_f), 314 __RISCV_ISA_EXT_DATA(d, RISCV_ISA_EXT_d), 315 __RISCV_ISA_EXT_DATA(q, RISCV_ISA_EXT_q), 316 __RISCV_ISA_EXT_SUPERSET(c, RISCV_ISA_EXT_c, riscv_c_exts), 317 __RISCV_ISA_EXT_SUPERSET(v, RISCV_ISA_EXT_v, riscv_v_exts), 318 __RISCV_ISA_EXT_DATA(h, RISCV_ISA_EXT_h), 319 __RISCV_ISA_EXT_SUPERSET_VALIDATE(zicbom, RISCV_ISA_EXT_ZICBOM, riscv_xlinuxenvcfg_exts, 320 riscv_ext_zicbom_validate), 321 __RISCV_ISA_EXT_SUPERSET_VALIDATE(zicboz, RISCV_ISA_EXT_ZICBOZ, riscv_xlinuxenvcfg_exts, 322 riscv_ext_zicboz_validate), 323 __RISCV_ISA_EXT_DATA(ziccrse, RISCV_ISA_EXT_ZICCRSE), 324 __RISCV_ISA_EXT_DATA(zicntr, RISCV_ISA_EXT_ZICNTR), 325 __RISCV_ISA_EXT_DATA(zicond, RISCV_ISA_EXT_ZICOND), 326 __RISCV_ISA_EXT_DATA(zicsr, RISCV_ISA_EXT_ZICSR), 327 __RISCV_ISA_EXT_DATA(zifencei, RISCV_ISA_EXT_ZIFENCEI), 328 __RISCV_ISA_EXT_DATA(zihintntl, RISCV_ISA_EXT_ZIHINTNTL), 329 __RISCV_ISA_EXT_DATA(zihintpause, RISCV_ISA_EXT_ZIHINTPAUSE), 330 __RISCV_ISA_EXT_DATA(zihpm, RISCV_ISA_EXT_ZIHPM), 331 __RISCV_ISA_EXT_DATA(zimop, RISCV_ISA_EXT_ZIMOP), 332 __RISCV_ISA_EXT_DATA(zabha, RISCV_ISA_EXT_ZABHA), 333 __RISCV_ISA_EXT_DATA(zacas, RISCV_ISA_EXT_ZACAS), 334 __RISCV_ISA_EXT_DATA(zawrs, RISCV_ISA_EXT_ZAWRS), 335 __RISCV_ISA_EXT_DATA(zfa, RISCV_ISA_EXT_ZFA), 336 __RISCV_ISA_EXT_DATA(zfh, RISCV_ISA_EXT_ZFH), 337 __RISCV_ISA_EXT_DATA(zfhmin, RISCV_ISA_EXT_ZFHMIN), 338 __RISCV_ISA_EXT_DATA(zca, RISCV_ISA_EXT_ZCA), 339 __RISCV_ISA_EXT_DATA_VALIDATE(zcb, RISCV_ISA_EXT_ZCB, riscv_ext_zca_depends), 340 __RISCV_ISA_EXT_DATA_VALIDATE(zcd, RISCV_ISA_EXT_ZCD, riscv_ext_zcd_validate), 341 __RISCV_ISA_EXT_DATA_VALIDATE(zcf, RISCV_ISA_EXT_ZCF, riscv_ext_zcf_validate), 342 __RISCV_ISA_EXT_DATA_VALIDATE(zcmop, RISCV_ISA_EXT_ZCMOP, riscv_ext_zca_depends), 343 __RISCV_ISA_EXT_DATA(zba, RISCV_ISA_EXT_ZBA), 344 __RISCV_ISA_EXT_DATA(zbb, RISCV_ISA_EXT_ZBB), 345 __RISCV_ISA_EXT_DATA(zbc, RISCV_ISA_EXT_ZBC), 346 __RISCV_ISA_EXT_DATA(zbkb, RISCV_ISA_EXT_ZBKB), 347 __RISCV_ISA_EXT_DATA(zbkc, RISCV_ISA_EXT_ZBKC), 348 __RISCV_ISA_EXT_DATA(zbkx, RISCV_ISA_EXT_ZBKX), 349 __RISCV_ISA_EXT_DATA(zbs, RISCV_ISA_EXT_ZBS), 350 __RISCV_ISA_EXT_BUNDLE(zk, riscv_zk_bundled_exts), 351 __RISCV_ISA_EXT_BUNDLE(zkn, riscv_zkn_bundled_exts), 352 __RISCV_ISA_EXT_DATA(zknd, RISCV_ISA_EXT_ZKND), 353 __RISCV_ISA_EXT_DATA(zkne, RISCV_ISA_EXT_ZKNE), 354 __RISCV_ISA_EXT_DATA(zknh, RISCV_ISA_EXT_ZKNH), 355 __RISCV_ISA_EXT_DATA(zkr, RISCV_ISA_EXT_ZKR), 356 __RISCV_ISA_EXT_BUNDLE(zks, riscv_zks_bundled_exts), 357 __RISCV_ISA_EXT_DATA(zkt, RISCV_ISA_EXT_ZKT), 358 __RISCV_ISA_EXT_DATA(zksed, RISCV_ISA_EXT_ZKSED), 359 __RISCV_ISA_EXT_DATA(zksh, RISCV_ISA_EXT_ZKSH), 360 __RISCV_ISA_EXT_DATA(ztso, RISCV_ISA_EXT_ZTSO), 361 __RISCV_ISA_EXT_SUPERSET(zvbb, RISCV_ISA_EXT_ZVBB, riscv_zvbb_exts), 362 __RISCV_ISA_EXT_DATA(zvbc, RISCV_ISA_EXT_ZVBC), 363 __RISCV_ISA_EXT_SUPERSET(zve32f, RISCV_ISA_EXT_ZVE32F, riscv_zve32f_exts), 364 __RISCV_ISA_EXT_DATA(zve32x, RISCV_ISA_EXT_ZVE32X), 365 __RISCV_ISA_EXT_SUPERSET(zve64d, RISCV_ISA_EXT_ZVE64D, riscv_zve64d_exts), 366 __RISCV_ISA_EXT_SUPERSET(zve64f, RISCV_ISA_EXT_ZVE64F, riscv_zve64f_exts), 367 __RISCV_ISA_EXT_SUPERSET(zve64x, RISCV_ISA_EXT_ZVE64X, riscv_zve64x_exts), 368 __RISCV_ISA_EXT_DATA(zvfh, RISCV_ISA_EXT_ZVFH), 369 __RISCV_ISA_EXT_DATA(zvfhmin, RISCV_ISA_EXT_ZVFHMIN), 370 __RISCV_ISA_EXT_DATA(zvkb, RISCV_ISA_EXT_ZVKB), 371 __RISCV_ISA_EXT_DATA(zvkg, RISCV_ISA_EXT_ZVKG), 372 __RISCV_ISA_EXT_BUNDLE(zvkn, riscv_zvkn_bundled_exts), 373 __RISCV_ISA_EXT_BUNDLE(zvknc, riscv_zvknc_bundled_exts), 374 __RISCV_ISA_EXT_DATA(zvkned, RISCV_ISA_EXT_ZVKNED), 375 __RISCV_ISA_EXT_BUNDLE(zvkng, riscv_zvkng_bundled_exts), 376 __RISCV_ISA_EXT_DATA(zvknha, RISCV_ISA_EXT_ZVKNHA), 377 __RISCV_ISA_EXT_DATA(zvknhb, RISCV_ISA_EXT_ZVKNHB), 378 __RISCV_ISA_EXT_BUNDLE(zvks, riscv_zvks_bundled_exts), 379 __RISCV_ISA_EXT_BUNDLE(zvksc, riscv_zvksc_bundled_exts), 380 __RISCV_ISA_EXT_DATA(zvksed, RISCV_ISA_EXT_ZVKSED), 381 __RISCV_ISA_EXT_DATA(zvksh, RISCV_ISA_EXT_ZVKSH), 382 __RISCV_ISA_EXT_BUNDLE(zvksg, riscv_zvksg_bundled_exts), 383 __RISCV_ISA_EXT_DATA(zvkt, RISCV_ISA_EXT_ZVKT), 384 __RISCV_ISA_EXT_DATA(smaia, RISCV_ISA_EXT_SMAIA), 385 __RISCV_ISA_EXT_DATA(smmpm, RISCV_ISA_EXT_SMMPM), 386 __RISCV_ISA_EXT_SUPERSET(smnpm, RISCV_ISA_EXT_SMNPM, riscv_xlinuxenvcfg_exts), 387 __RISCV_ISA_EXT_DATA(smstateen, RISCV_ISA_EXT_SMSTATEEN), 388 __RISCV_ISA_EXT_DATA(ssaia, RISCV_ISA_EXT_SSAIA), 389 __RISCV_ISA_EXT_DATA(sscofpmf, RISCV_ISA_EXT_SSCOFPMF), 390 __RISCV_ISA_EXT_SUPERSET(ssnpm, RISCV_ISA_EXT_SSNPM, riscv_xlinuxenvcfg_exts), 391 __RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC), 392 __RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL), 393 __RISCV_ISA_EXT_DATA(svnapot, RISCV_ISA_EXT_SVNAPOT), 394 __RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT), 395 __RISCV_ISA_EXT_DATA(svvptc, RISCV_ISA_EXT_SVVPTC), 396 }; 397 398 const size_t riscv_isa_ext_count = ARRAY_SIZE(riscv_isa_ext); 399 400 static void riscv_isa_set_ext(const struct riscv_isa_ext_data *ext, unsigned long *bitmap) 401 { 402 if (ext->id != RISCV_ISA_EXT_INVALID) 403 set_bit(ext->id, bitmap); 404 405 for (int i = 0; i < ext->subset_ext_size; i++) { 406 if (ext->subset_ext_ids[i] != RISCV_ISA_EXT_INVALID) 407 set_bit(ext->subset_ext_ids[i], bitmap); 408 } 409 } 410 411 static const struct riscv_isa_ext_data *riscv_get_isa_ext_data(unsigned int ext_id) 412 { 413 for (int i = 0; i < riscv_isa_ext_count; i++) { 414 if (riscv_isa_ext[i].id == ext_id) 415 return &riscv_isa_ext[i]; 416 } 417 418 return NULL; 419 } 420 421 /* 422 * "Resolve" a source ISA bitmap into one that matches kernel configuration as 423 * well as correct extension dependencies. Some extensions depends on specific 424 * kernel configuration to be usable (V needs CONFIG_RISCV_ISA_V for instance) 425 * and this function will actually validate all the extensions provided in 426 * source_isa into the resolved_isa based on extensions validate() callbacks. 427 */ 428 static void __init riscv_resolve_isa(unsigned long *source_isa, 429 unsigned long *resolved_isa, unsigned long *this_hwcap, 430 unsigned long *isa2hwcap) 431 { 432 bool loop; 433 const struct riscv_isa_ext_data *ext; 434 DECLARE_BITMAP(prev_resolved_isa, RISCV_ISA_EXT_MAX); 435 int max_loop_count = riscv_isa_ext_count, ret; 436 unsigned int bit; 437 438 do { 439 loop = false; 440 if (max_loop_count-- < 0) { 441 pr_err("Failed to reach a stable ISA state\n"); 442 return; 443 } 444 bitmap_copy(prev_resolved_isa, resolved_isa, RISCV_ISA_EXT_MAX); 445 for_each_set_bit(bit, source_isa, RISCV_ISA_EXT_MAX) { 446 ext = riscv_get_isa_ext_data(bit); 447 448 if (ext && ext->validate) { 449 ret = ext->validate(ext, resolved_isa); 450 if (ret == -EPROBE_DEFER) { 451 loop = true; 452 continue; 453 } else if (ret) { 454 /* Disable the extension entirely */ 455 clear_bit(bit, source_isa); 456 continue; 457 } 458 } 459 460 set_bit(bit, resolved_isa); 461 /* No need to keep it in source isa now that it is enabled */ 462 clear_bit(bit, source_isa); 463 464 /* Single letter extensions get set in hwcap */ 465 if (bit < RISCV_ISA_EXT_BASE) 466 *this_hwcap |= isa2hwcap[bit]; 467 } 468 } while (loop && memcmp(prev_resolved_isa, resolved_isa, sizeof(prev_resolved_isa))); 469 } 470 471 static void __init match_isa_ext(const char *name, const char *name_end, unsigned long *bitmap) 472 { 473 for (int i = 0; i < riscv_isa_ext_count; i++) { 474 const struct riscv_isa_ext_data *ext = &riscv_isa_ext[i]; 475 476 if ((name_end - name == strlen(ext->name)) && 477 !strncasecmp(name, ext->name, name_end - name)) { 478 riscv_isa_set_ext(ext, bitmap); 479 break; 480 } 481 } 482 } 483 484 static void __init riscv_parse_isa_string(const char *isa, unsigned long *bitmap) 485 { 486 /* 487 * For all possible cpus, we have already validated in 488 * the boot process that they at least contain "rv" and 489 * whichever of "32"/"64" this kernel supports, and so this 490 * section can be skipped. 491 */ 492 isa += 4; 493 494 while (*isa) { 495 const char *ext = isa++; 496 const char *ext_end = isa; 497 bool ext_err = false; 498 499 switch (*ext) { 500 case 'x': 501 case 'X': 502 if (acpi_disabled) 503 pr_warn_once("Vendor extensions are ignored in riscv,isa. Use riscv,isa-extensions instead."); 504 /* 505 * To skip an extension, we find its end. 506 * As multi-letter extensions must be split from other multi-letter 507 * extensions with an "_", the end of a multi-letter extension will 508 * either be the null character or the "_" at the start of the next 509 * multi-letter extension. 510 */ 511 for (; *isa && *isa != '_'; ++isa) 512 ; 513 ext_err = true; 514 break; 515 case 's': 516 /* 517 * Workaround for invalid single-letter 's' & 'u' (QEMU). 518 * No need to set the bit in riscv_isa as 's' & 'u' are 519 * not valid ISA extensions. It works unless the first 520 * multi-letter extension in the ISA string begins with 521 * "Su" and is not prefixed with an underscore. 522 */ 523 if (ext[-1] != '_' && ext[1] == 'u') { 524 ++isa; 525 ext_err = true; 526 break; 527 } 528 fallthrough; 529 case 'S': 530 case 'z': 531 case 'Z': 532 /* 533 * Before attempting to parse the extension itself, we find its end. 534 * As multi-letter extensions must be split from other multi-letter 535 * extensions with an "_", the end of a multi-letter extension will 536 * either be the null character or the "_" at the start of the next 537 * multi-letter extension. 538 * 539 * Next, as the extensions version is currently ignored, we 540 * eliminate that portion. This is done by parsing backwards from 541 * the end of the extension, removing any numbers. This may be a 542 * major or minor number however, so the process is repeated if a 543 * minor number was found. 544 * 545 * ext_end is intended to represent the first character *after* the 546 * name portion of an extension, but will be decremented to the last 547 * character itself while eliminating the extensions version number. 548 * A simple re-increment solves this problem. 549 */ 550 for (; *isa && *isa != '_'; ++isa) 551 if (unlikely(!isalnum(*isa))) 552 ext_err = true; 553 554 ext_end = isa; 555 if (unlikely(ext_err)) 556 break; 557 558 if (!isdigit(ext_end[-1])) 559 break; 560 561 while (isdigit(*--ext_end)) 562 ; 563 564 if (tolower(ext_end[0]) != 'p' || !isdigit(ext_end[-1])) { 565 ++ext_end; 566 break; 567 } 568 569 while (isdigit(*--ext_end)) 570 ; 571 572 ++ext_end; 573 break; 574 default: 575 /* 576 * Things are a little easier for single-letter extensions, as they 577 * are parsed forwards. 578 * 579 * After checking that our starting position is valid, we need to 580 * ensure that, when isa was incremented at the start of the loop, 581 * that it arrived at the start of the next extension. 582 * 583 * If we are already on a non-digit, there is nothing to do. Either 584 * we have a multi-letter extension's _, or the start of an 585 * extension. 586 * 587 * Otherwise we have found the current extension's major version 588 * number. Parse past it, and a subsequent p/minor version number 589 * if present. The `p` extension must not appear immediately after 590 * a number, so there is no fear of missing it. 591 * 592 */ 593 if (unlikely(!isalpha(*ext))) { 594 ext_err = true; 595 break; 596 } 597 598 if (!isdigit(*isa)) 599 break; 600 601 while (isdigit(*++isa)) 602 ; 603 604 if (tolower(*isa) != 'p') 605 break; 606 607 if (!isdigit(*++isa)) { 608 --isa; 609 break; 610 } 611 612 while (isdigit(*++isa)) 613 ; 614 615 break; 616 } 617 618 /* 619 * The parser expects that at the start of an iteration isa points to the 620 * first character of the next extension. As we stop parsing an extension 621 * on meeting a non-alphanumeric character, an extra increment is needed 622 * where the succeeding extension is a multi-letter prefixed with an "_". 623 */ 624 if (*isa == '_') 625 ++isa; 626 627 if (unlikely(ext_err)) 628 continue; 629 630 match_isa_ext(ext, ext_end, bitmap); 631 } 632 } 633 634 static void __init riscv_fill_hwcap_from_isa_string(unsigned long *isa2hwcap) 635 { 636 struct device_node *node; 637 const char *isa; 638 int rc; 639 struct acpi_table_header *rhct; 640 acpi_status status; 641 unsigned int cpu; 642 u64 boot_vendorid; 643 u64 boot_archid; 644 645 if (!acpi_disabled) { 646 status = acpi_get_table(ACPI_SIG_RHCT, 0, &rhct); 647 if (ACPI_FAILURE(status)) 648 return; 649 } 650 651 boot_vendorid = riscv_get_mvendorid(); 652 boot_archid = riscv_get_marchid(); 653 654 for_each_possible_cpu(cpu) { 655 struct riscv_isainfo *isainfo = &hart_isa[cpu]; 656 unsigned long this_hwcap = 0; 657 DECLARE_BITMAP(source_isa, RISCV_ISA_EXT_MAX) = { 0 }; 658 659 if (acpi_disabled) { 660 node = of_cpu_device_node_get(cpu); 661 if (!node) { 662 pr_warn("Unable to find cpu node\n"); 663 continue; 664 } 665 666 rc = of_property_read_string(node, "riscv,isa", &isa); 667 of_node_put(node); 668 if (rc) { 669 pr_warn("Unable to find \"riscv,isa\" devicetree entry\n"); 670 continue; 671 } 672 } else { 673 rc = acpi_get_riscv_isa(rhct, cpu, &isa); 674 if (rc < 0) { 675 pr_warn("Unable to get ISA for the hart - %d\n", cpu); 676 continue; 677 } 678 } 679 680 riscv_parse_isa_string(isa, source_isa); 681 682 /* 683 * These ones were as they were part of the base ISA when the 684 * port & dt-bindings were upstreamed, and so can be set 685 * unconditionally where `i` is in riscv,isa on DT systems. 686 */ 687 if (acpi_disabled) { 688 set_bit(RISCV_ISA_EXT_ZICSR, source_isa); 689 set_bit(RISCV_ISA_EXT_ZIFENCEI, source_isa); 690 set_bit(RISCV_ISA_EXT_ZICNTR, source_isa); 691 set_bit(RISCV_ISA_EXT_ZIHPM, source_isa); 692 } 693 694 /* 695 * "V" in ISA strings is ambiguous in practice: it should mean 696 * just the standard V-1.0 but vendors aren't well behaved. 697 * Many vendors with T-Head CPU cores which implement the 0.7.1 698 * version of the vector specification put "v" into their DTs. 699 * CPU cores with the ratified spec will contain non-zero 700 * marchid. 701 */ 702 if (acpi_disabled && boot_vendorid == THEAD_VENDOR_ID && boot_archid == 0x0) { 703 this_hwcap &= ~isa2hwcap[RISCV_ISA_EXT_v]; 704 clear_bit(RISCV_ISA_EXT_v, source_isa); 705 } 706 707 riscv_resolve_isa(source_isa, isainfo->isa, &this_hwcap, isa2hwcap); 708 709 /* 710 * All "okay" hart should have same isa. Set HWCAP based on 711 * common capabilities of every "okay" hart, in case they don't 712 * have. 713 */ 714 if (elf_hwcap) 715 elf_hwcap &= this_hwcap; 716 else 717 elf_hwcap = this_hwcap; 718 719 if (bitmap_empty(riscv_isa, RISCV_ISA_EXT_MAX)) 720 bitmap_copy(riscv_isa, isainfo->isa, RISCV_ISA_EXT_MAX); 721 else 722 bitmap_and(riscv_isa, riscv_isa, isainfo->isa, RISCV_ISA_EXT_MAX); 723 } 724 725 if (!acpi_disabled && rhct) 726 acpi_put_table((struct acpi_table_header *)rhct); 727 } 728 729 static void __init riscv_fill_cpu_vendor_ext(struct device_node *cpu_node, int cpu) 730 { 731 if (!IS_ENABLED(CONFIG_RISCV_ISA_VENDOR_EXT)) 732 return; 733 734 for (int i = 0; i < riscv_isa_vendor_ext_list_size; i++) { 735 struct riscv_isa_vendor_ext_data_list *ext_list = riscv_isa_vendor_ext_list[i]; 736 737 for (int j = 0; j < ext_list->ext_data_count; j++) { 738 const struct riscv_isa_ext_data ext = ext_list->ext_data[j]; 739 struct riscv_isavendorinfo *isavendorinfo = &ext_list->per_hart_isa_bitmap[cpu]; 740 741 if (of_property_match_string(cpu_node, "riscv,isa-extensions", 742 ext.property) < 0) 743 continue; 744 745 /* 746 * Assume that subset extensions are all members of the 747 * same vendor. 748 */ 749 if (ext.subset_ext_size) 750 for (int k = 0; k < ext.subset_ext_size; k++) 751 set_bit(ext.subset_ext_ids[k], isavendorinfo->isa); 752 753 set_bit(ext.id, isavendorinfo->isa); 754 } 755 } 756 } 757 758 /* 759 * Populate all_harts_isa_bitmap for each vendor with all of the extensions that 760 * are shared across CPUs for that vendor. 761 */ 762 static void __init riscv_fill_vendor_ext_list(int cpu) 763 { 764 if (!IS_ENABLED(CONFIG_RISCV_ISA_VENDOR_EXT)) 765 return; 766 767 for (int i = 0; i < riscv_isa_vendor_ext_list_size; i++) { 768 struct riscv_isa_vendor_ext_data_list *ext_list = riscv_isa_vendor_ext_list[i]; 769 770 if (!ext_list->is_initialized) { 771 bitmap_copy(ext_list->all_harts_isa_bitmap.isa, 772 ext_list->per_hart_isa_bitmap[cpu].isa, 773 RISCV_ISA_VENDOR_EXT_MAX); 774 ext_list->is_initialized = true; 775 } else { 776 bitmap_and(ext_list->all_harts_isa_bitmap.isa, 777 ext_list->all_harts_isa_bitmap.isa, 778 ext_list->per_hart_isa_bitmap[cpu].isa, 779 RISCV_ISA_VENDOR_EXT_MAX); 780 } 781 } 782 } 783 784 static int has_thead_homogeneous_vlenb(void) 785 { 786 int cpu; 787 u32 prev_vlenb = 0; 788 u32 vlenb; 789 790 /* Ignore thead,vlenb property if xtheavector is not enabled in the kernel */ 791 if (!IS_ENABLED(CONFIG_RISCV_ISA_XTHEADVECTOR)) 792 return 0; 793 794 for_each_possible_cpu(cpu) { 795 struct device_node *cpu_node; 796 797 cpu_node = of_cpu_device_node_get(cpu); 798 if (!cpu_node) { 799 pr_warn("Unable to find cpu node\n"); 800 return -ENOENT; 801 } 802 803 if (of_property_read_u32(cpu_node, "thead,vlenb", &vlenb)) { 804 of_node_put(cpu_node); 805 806 if (prev_vlenb) 807 return -ENOENT; 808 continue; 809 } 810 811 if (prev_vlenb && vlenb != prev_vlenb) { 812 of_node_put(cpu_node); 813 return -ENOENT; 814 } 815 816 prev_vlenb = vlenb; 817 of_node_put(cpu_node); 818 } 819 820 thead_vlenb_of = vlenb; 821 return 0; 822 } 823 824 static int __init riscv_fill_hwcap_from_ext_list(unsigned long *isa2hwcap) 825 { 826 unsigned int cpu; 827 828 for_each_possible_cpu(cpu) { 829 unsigned long this_hwcap = 0; 830 struct device_node *cpu_node; 831 struct riscv_isainfo *isainfo = &hart_isa[cpu]; 832 DECLARE_BITMAP(source_isa, RISCV_ISA_EXT_MAX) = { 0 }; 833 834 cpu_node = of_cpu_device_node_get(cpu); 835 if (!cpu_node) { 836 pr_warn("Unable to find cpu node\n"); 837 continue; 838 } 839 840 if (!of_property_present(cpu_node, "riscv,isa-extensions")) { 841 of_node_put(cpu_node); 842 continue; 843 } 844 845 for (int i = 0; i < riscv_isa_ext_count; i++) { 846 const struct riscv_isa_ext_data *ext = &riscv_isa_ext[i]; 847 848 if (of_property_match_string(cpu_node, "riscv,isa-extensions", 849 ext->property) < 0) 850 continue; 851 852 riscv_isa_set_ext(ext, source_isa); 853 } 854 855 riscv_resolve_isa(source_isa, isainfo->isa, &this_hwcap, isa2hwcap); 856 riscv_fill_cpu_vendor_ext(cpu_node, cpu); 857 858 of_node_put(cpu_node); 859 860 /* 861 * All "okay" harts should have same isa. Set HWCAP based on 862 * common capabilities of every "okay" hart, in case they don't. 863 */ 864 if (elf_hwcap) 865 elf_hwcap &= this_hwcap; 866 else 867 elf_hwcap = this_hwcap; 868 869 if (bitmap_empty(riscv_isa, RISCV_ISA_EXT_MAX)) 870 bitmap_copy(riscv_isa, isainfo->isa, RISCV_ISA_EXT_MAX); 871 else 872 bitmap_and(riscv_isa, riscv_isa, isainfo->isa, RISCV_ISA_EXT_MAX); 873 874 riscv_fill_vendor_ext_list(cpu); 875 } 876 877 if (has_xtheadvector_no_alternatives() && has_thead_homogeneous_vlenb() < 0) { 878 pr_warn("Unsupported heterogeneous vlenb detected, vector extension disabled.\n"); 879 disable_xtheadvector(); 880 } 881 882 if (bitmap_empty(riscv_isa, RISCV_ISA_EXT_MAX)) 883 return -ENOENT; 884 885 return 0; 886 } 887 888 #ifdef CONFIG_RISCV_ISA_FALLBACK 889 bool __initdata riscv_isa_fallback = true; 890 #else 891 bool __initdata riscv_isa_fallback; 892 static int __init riscv_isa_fallback_setup(char *__unused) 893 { 894 riscv_isa_fallback = true; 895 return 1; 896 } 897 early_param("riscv_isa_fallback", riscv_isa_fallback_setup); 898 #endif 899 900 void __init riscv_fill_hwcap(void) 901 { 902 char print_str[NUM_ALPHA_EXTS + 1]; 903 unsigned long isa2hwcap[26] = {0}; 904 int i, j; 905 906 isa2hwcap['i' - 'a'] = COMPAT_HWCAP_ISA_I; 907 isa2hwcap['m' - 'a'] = COMPAT_HWCAP_ISA_M; 908 isa2hwcap['a' - 'a'] = COMPAT_HWCAP_ISA_A; 909 isa2hwcap['f' - 'a'] = COMPAT_HWCAP_ISA_F; 910 isa2hwcap['d' - 'a'] = COMPAT_HWCAP_ISA_D; 911 isa2hwcap['c' - 'a'] = COMPAT_HWCAP_ISA_C; 912 isa2hwcap['v' - 'a'] = COMPAT_HWCAP_ISA_V; 913 914 if (!acpi_disabled) { 915 riscv_fill_hwcap_from_isa_string(isa2hwcap); 916 } else { 917 int ret = riscv_fill_hwcap_from_ext_list(isa2hwcap); 918 919 if (ret && riscv_isa_fallback) { 920 pr_info("Falling back to deprecated \"riscv,isa\"\n"); 921 riscv_fill_hwcap_from_isa_string(isa2hwcap); 922 } 923 } 924 925 /* 926 * We don't support systems with F but without D, so mask those out 927 * here. 928 */ 929 if ((elf_hwcap & COMPAT_HWCAP_ISA_F) && !(elf_hwcap & COMPAT_HWCAP_ISA_D)) { 930 pr_info("This kernel does not support systems with F but not D\n"); 931 elf_hwcap &= ~COMPAT_HWCAP_ISA_F; 932 } 933 934 if (__riscv_isa_extension_available(NULL, RISCV_ISA_EXT_ZVE32X) || 935 has_xtheadvector_no_alternatives()) { 936 /* 937 * This cannot fail when called on the boot hart 938 */ 939 riscv_v_setup_vsize(); 940 } 941 942 if (elf_hwcap & COMPAT_HWCAP_ISA_V) { 943 /* 944 * ISA string in device tree might have 'v' flag, but 945 * CONFIG_RISCV_ISA_V is disabled in kernel. 946 * Clear V flag in elf_hwcap if CONFIG_RISCV_ISA_V is disabled. 947 */ 948 if (!IS_ENABLED(CONFIG_RISCV_ISA_V)) 949 elf_hwcap &= ~COMPAT_HWCAP_ISA_V; 950 } 951 952 memset(print_str, 0, sizeof(print_str)); 953 for (i = 0, j = 0; i < NUM_ALPHA_EXTS; i++) 954 if (riscv_isa[0] & BIT_MASK(i)) 955 print_str[j++] = (char)('a' + i); 956 pr_info("riscv: base ISA extensions %s\n", print_str); 957 958 memset(print_str, 0, sizeof(print_str)); 959 for (i = 0, j = 0; i < NUM_ALPHA_EXTS; i++) 960 if (elf_hwcap & BIT_MASK(i)) 961 print_str[j++] = (char)('a' + i); 962 pr_info("riscv: ELF capabilities %s\n", print_str); 963 } 964 965 unsigned long riscv_get_elf_hwcap(void) 966 { 967 unsigned long hwcap; 968 969 hwcap = (elf_hwcap & ((1UL << RISCV_ISA_EXT_BASE) - 1)); 970 971 if (!riscv_v_vstate_ctrl_user_allowed()) 972 hwcap &= ~COMPAT_HWCAP_ISA_V; 973 974 return hwcap; 975 } 976 977 void __init riscv_user_isa_enable(void) 978 { 979 if (riscv_has_extension_unlikely(RISCV_ISA_EXT_ZICBOZ)) 980 current->thread.envcfg |= ENVCFG_CBZE; 981 else if (any_cpu_has_zicboz) 982 pr_warn("Zicboz disabled as it is unavailable on some harts\n"); 983 } 984 985 #ifdef CONFIG_RISCV_ALTERNATIVE 986 /* 987 * Alternative patch sites consider 48 bits when determining when to patch 988 * the old instruction sequence with the new. These bits are broken into a 989 * 16-bit vendor ID and a 32-bit patch ID. A non-zero vendor ID means the 990 * patch site is for an erratum, identified by the 32-bit patch ID. When 991 * the vendor ID is zero, the patch site is for a cpufeature. cpufeatures 992 * further break down patch ID into two 16-bit numbers. The lower 16 bits 993 * are the cpufeature ID and the upper 16 bits are used for a value specific 994 * to the cpufeature and patch site. If the upper 16 bits are zero, then it 995 * implies no specific value is specified. cpufeatures that want to control 996 * patching on a per-site basis will provide non-zero values and implement 997 * checks here. The checks return true when patching should be done, and 998 * false otherwise. 999 */ 1000 static bool riscv_cpufeature_patch_check(u16 id, u16 value) 1001 { 1002 if (!value) 1003 return true; 1004 1005 switch (id) { 1006 case RISCV_ISA_EXT_ZICBOZ: 1007 /* 1008 * Zicboz alternative applications provide the maximum 1009 * supported block size order, or zero when it doesn't 1010 * matter. If the current block size exceeds the maximum, 1011 * then the alternative cannot be applied. 1012 */ 1013 return riscv_cboz_block_size <= (1U << value); 1014 } 1015 1016 return false; 1017 } 1018 1019 void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin, 1020 struct alt_entry *end, 1021 unsigned int stage) 1022 { 1023 struct alt_entry *alt; 1024 void *oldptr, *altptr; 1025 u16 id, value, vendor; 1026 1027 if (stage == RISCV_ALTERNATIVES_EARLY_BOOT) 1028 return; 1029 1030 for (alt = begin; alt < end; alt++) { 1031 id = PATCH_ID_CPUFEATURE_ID(alt->patch_id); 1032 vendor = PATCH_ID_CPUFEATURE_ID(alt->vendor_id); 1033 1034 /* 1035 * Any alternative with a patch_id that is less than 1036 * RISCV_ISA_EXT_MAX is interpreted as a standard extension. 1037 * 1038 * Any alternative with patch_id that is greater than or equal 1039 * to RISCV_VENDOR_EXT_ALTERNATIVES_BASE is interpreted as a 1040 * vendor extension. 1041 */ 1042 if (id < RISCV_ISA_EXT_MAX) { 1043 /* 1044 * This patch should be treated as errata so skip 1045 * processing here. 1046 */ 1047 if (alt->vendor_id != 0) 1048 continue; 1049 1050 if (!__riscv_isa_extension_available(NULL, id)) 1051 continue; 1052 1053 value = PATCH_ID_CPUFEATURE_VALUE(alt->patch_id); 1054 if (!riscv_cpufeature_patch_check(id, value)) 1055 continue; 1056 } else if (id >= RISCV_VENDOR_EXT_ALTERNATIVES_BASE) { 1057 if (!__riscv_isa_vendor_extension_available(VENDOR_EXT_ALL_CPUS, vendor, 1058 id - RISCV_VENDOR_EXT_ALTERNATIVES_BASE)) 1059 continue; 1060 } else { 1061 WARN(1, "This extension id:%d is not in ISA extension list", id); 1062 continue; 1063 } 1064 1065 oldptr = ALT_OLD_PTR(alt); 1066 altptr = ALT_ALT_PTR(alt); 1067 1068 mutex_lock(&text_mutex); 1069 patch_text_nosync(oldptr, altptr, alt->alt_len); 1070 riscv_alternative_fix_offsets(oldptr, alt->alt_len, oldptr - altptr); 1071 mutex_unlock(&text_mutex); 1072 } 1073 } 1074 #endif 1075