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