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