1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Intel CPU Microcode Update Driver for Linux 4 * 5 * Copyright (C) 2000-2006 Tigran Aivazian <[email protected]> 6 * 2006 Shaohua Li <[email protected]> 7 * 8 * Intel CPU microcode early update for Linux 9 * 10 * Copyright (C) 2012 Fenghua Yu <[email protected]> 11 * H Peter Anvin" <[email protected]> 12 */ 13 #define pr_fmt(fmt) "microcode: " fmt 14 #include <linux/earlycpio.h> 15 #include <linux/firmware.h> 16 #include <linux/uaccess.h> 17 #include <linux/vmalloc.h> 18 #include <linux/initrd.h> 19 #include <linux/kernel.h> 20 #include <linux/slab.h> 21 #include <linux/cpu.h> 22 #include <linux/uio.h> 23 #include <linux/mm.h> 24 25 #include <asm/intel-family.h> 26 #include <asm/processor.h> 27 #include <asm/tlbflush.h> 28 #include <asm/setup.h> 29 #include <asm/msr.h> 30 31 #include "internal.h" 32 33 static const char ucode_path[] = "kernel/x86/microcode/GenuineIntel.bin"; 34 35 /* Current microcode patch used in early patching on the APs. */ 36 static struct microcode_intel *intel_ucode_patch __read_mostly; 37 38 /* last level cache size per core */ 39 static int llc_size_per_core __ro_after_init; 40 41 /* microcode format is extended from prescott processors */ 42 struct extended_signature { 43 unsigned int sig; 44 unsigned int pf; 45 unsigned int cksum; 46 }; 47 48 struct extended_sigtable { 49 unsigned int count; 50 unsigned int cksum; 51 unsigned int reserved[3]; 52 struct extended_signature sigs[]; 53 }; 54 55 #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE) 56 #define EXT_HEADER_SIZE (sizeof(struct extended_sigtable)) 57 #define EXT_SIGNATURE_SIZE (sizeof(struct extended_signature)) 58 59 static inline unsigned int get_totalsize(struct microcode_header_intel *hdr) 60 { 61 return hdr->datasize ? hdr->totalsize : DEFAULT_UCODE_TOTALSIZE; 62 } 63 64 static inline unsigned int exttable_size(struct extended_sigtable *et) 65 { 66 return et->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE; 67 } 68 69 int intel_cpu_collect_info(struct ucode_cpu_info *uci) 70 { 71 unsigned int val[2]; 72 unsigned int family, model; 73 struct cpu_signature csig = { 0 }; 74 unsigned int eax, ebx, ecx, edx; 75 76 memset(uci, 0, sizeof(*uci)); 77 78 eax = 0x00000001; 79 ecx = 0; 80 native_cpuid(&eax, &ebx, &ecx, &edx); 81 csig.sig = eax; 82 83 family = x86_family(eax); 84 model = x86_model(eax); 85 86 if (model >= 5 || family > 6) { 87 /* get processor flags from MSR 0x17 */ 88 native_rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]); 89 csig.pf = 1 << ((val[1] >> 18) & 7); 90 } 91 92 csig.rev = intel_get_microcode_revision(); 93 94 uci->cpu_sig = csig; 95 96 return 0; 97 } 98 EXPORT_SYMBOL_GPL(intel_cpu_collect_info); 99 100 /* 101 * Returns 1 if update has been found, 0 otherwise. 102 */ 103 int intel_find_matching_signature(void *mc, unsigned int csig, int cpf) 104 { 105 struct microcode_header_intel *mc_hdr = mc; 106 struct extended_sigtable *ext_hdr; 107 struct extended_signature *ext_sig; 108 int i; 109 110 if (intel_cpu_signatures_match(csig, cpf, mc_hdr->sig, mc_hdr->pf)) 111 return 1; 112 113 /* Look for ext. headers: */ 114 if (get_totalsize(mc_hdr) <= intel_microcode_get_datasize(mc_hdr) + MC_HEADER_SIZE) 115 return 0; 116 117 ext_hdr = mc + intel_microcode_get_datasize(mc_hdr) + MC_HEADER_SIZE; 118 ext_sig = (void *)ext_hdr + EXT_HEADER_SIZE; 119 120 for (i = 0; i < ext_hdr->count; i++) { 121 if (intel_cpu_signatures_match(csig, cpf, ext_sig->sig, ext_sig->pf)) 122 return 1; 123 ext_sig++; 124 } 125 return 0; 126 } 127 EXPORT_SYMBOL_GPL(intel_find_matching_signature); 128 129 /** 130 * intel_microcode_sanity_check() - Sanity check microcode file. 131 * @mc: Pointer to the microcode file contents. 132 * @print_err: Display failure reason if true, silent if false. 133 * @hdr_type: Type of file, i.e. normal microcode file or In Field Scan file. 134 * Validate if the microcode header type matches with the type 135 * specified here. 136 * 137 * Validate certain header fields and verify if computed checksum matches 138 * with the one specified in the header. 139 * 140 * Return: 0 if the file passes all the checks, -EINVAL if any of the checks 141 * fail. 142 */ 143 int intel_microcode_sanity_check(void *mc, bool print_err, int hdr_type) 144 { 145 unsigned long total_size, data_size, ext_table_size; 146 struct microcode_header_intel *mc_header = mc; 147 struct extended_sigtable *ext_header = NULL; 148 u32 sum, orig_sum, ext_sigcount = 0, i; 149 struct extended_signature *ext_sig; 150 151 total_size = get_totalsize(mc_header); 152 data_size = intel_microcode_get_datasize(mc_header); 153 154 if (data_size + MC_HEADER_SIZE > total_size) { 155 if (print_err) 156 pr_err("Error: bad microcode data file size.\n"); 157 return -EINVAL; 158 } 159 160 if (mc_header->ldrver != 1 || mc_header->hdrver != hdr_type) { 161 if (print_err) 162 pr_err("Error: invalid/unknown microcode update format. Header type %d\n", 163 mc_header->hdrver); 164 return -EINVAL; 165 } 166 167 ext_table_size = total_size - (MC_HEADER_SIZE + data_size); 168 if (ext_table_size) { 169 u32 ext_table_sum = 0; 170 u32 *ext_tablep; 171 172 if (ext_table_size < EXT_HEADER_SIZE || 173 ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) { 174 if (print_err) 175 pr_err("Error: truncated extended signature table.\n"); 176 return -EINVAL; 177 } 178 179 ext_header = mc + MC_HEADER_SIZE + data_size; 180 if (ext_table_size != exttable_size(ext_header)) { 181 if (print_err) 182 pr_err("Error: extended signature table size mismatch.\n"); 183 return -EFAULT; 184 } 185 186 ext_sigcount = ext_header->count; 187 188 /* 189 * Check extended table checksum: the sum of all dwords that 190 * comprise a valid table must be 0. 191 */ 192 ext_tablep = (u32 *)ext_header; 193 194 i = ext_table_size / sizeof(u32); 195 while (i--) 196 ext_table_sum += ext_tablep[i]; 197 198 if (ext_table_sum) { 199 if (print_err) 200 pr_warn("Bad extended signature table checksum, aborting.\n"); 201 return -EINVAL; 202 } 203 } 204 205 /* 206 * Calculate the checksum of update data and header. The checksum of 207 * valid update data and header including the extended signature table 208 * must be 0. 209 */ 210 orig_sum = 0; 211 i = (MC_HEADER_SIZE + data_size) / sizeof(u32); 212 while (i--) 213 orig_sum += ((u32 *)mc)[i]; 214 215 if (orig_sum) { 216 if (print_err) 217 pr_err("Bad microcode data checksum, aborting.\n"); 218 return -EINVAL; 219 } 220 221 if (!ext_table_size) 222 return 0; 223 224 /* 225 * Check extended signature checksum: 0 => valid. 226 */ 227 for (i = 0; i < ext_sigcount; i++) { 228 ext_sig = (void *)ext_header + EXT_HEADER_SIZE + 229 EXT_SIGNATURE_SIZE * i; 230 231 sum = (mc_header->sig + mc_header->pf + mc_header->cksum) - 232 (ext_sig->sig + ext_sig->pf + ext_sig->cksum); 233 if (sum) { 234 if (print_err) 235 pr_err("Bad extended signature checksum, aborting.\n"); 236 return -EINVAL; 237 } 238 } 239 return 0; 240 } 241 EXPORT_SYMBOL_GPL(intel_microcode_sanity_check); 242 243 /* 244 * Returns 1 if update has been found, 0 otherwise. 245 */ 246 static int has_newer_microcode(void *mc, unsigned int csig, int cpf, int new_rev) 247 { 248 struct microcode_header_intel *mc_hdr = mc; 249 250 if (mc_hdr->rev <= new_rev) 251 return 0; 252 253 return intel_find_matching_signature(mc, csig, cpf); 254 } 255 256 static void save_microcode_patch(void *data, unsigned int size) 257 { 258 struct microcode_header_intel *p; 259 260 p = kmemdup(data, size, GFP_KERNEL); 261 if (!p) 262 return; 263 264 kfree(intel_ucode_patch); 265 /* Save for early loading */ 266 intel_ucode_patch = (struct microcode_intel *)p; 267 } 268 269 /* Scan CPIO for microcode matching the boot CPU's family, model, stepping */ 270 static struct microcode_intel *scan_microcode(void *data, size_t size, 271 struct ucode_cpu_info *uci, bool save) 272 { 273 struct microcode_header_intel *mc_header; 274 struct microcode_intel *patch = NULL; 275 u32 cur_rev = uci->cpu_sig.rev; 276 unsigned int mc_size; 277 278 for (; size >= sizeof(struct microcode_header_intel); size -= mc_size, data += mc_size) { 279 mc_header = (struct microcode_header_intel *)data; 280 281 mc_size = get_totalsize(mc_header); 282 if (!mc_size || mc_size > size || 283 intel_microcode_sanity_check(data, false, MC_HEADER_TYPE_MICROCODE) < 0) 284 break; 285 286 if (!intel_find_matching_signature(data, uci->cpu_sig.sig, uci->cpu_sig.pf)) 287 continue; 288 289 /* BSP scan: Check whether there is newer microcode */ 290 if (!save && cur_rev >= mc_header->rev) 291 continue; 292 293 /* Save scan: Check whether there is newer or matching microcode */ 294 if (save && cur_rev != mc_header->rev) 295 continue; 296 297 patch = data; 298 cur_rev = mc_header->rev; 299 } 300 301 if (size) 302 return NULL; 303 304 if (save && patch) 305 save_microcode_patch(patch, mc_size); 306 307 return patch; 308 } 309 310 static bool load_builtin_intel_microcode(struct cpio_data *cp) 311 { 312 unsigned int eax = 1, ebx, ecx = 0, edx; 313 struct firmware fw; 314 char name[30]; 315 316 if (IS_ENABLED(CONFIG_X86_32)) 317 return false; 318 319 native_cpuid(&eax, &ebx, &ecx, &edx); 320 321 sprintf(name, "intel-ucode/%02x-%02x-%02x", 322 x86_family(eax), x86_model(eax), x86_stepping(eax)); 323 324 if (firmware_request_builtin(&fw, name)) { 325 cp->size = fw.size; 326 cp->data = (void *)fw.data; 327 return true; 328 } 329 330 return false; 331 } 332 333 static int apply_microcode_early(struct ucode_cpu_info *uci) 334 { 335 struct microcode_intel *mc; 336 u32 rev, old_rev, date; 337 338 mc = uci->mc; 339 if (!mc) 340 return 0; 341 342 /* 343 * Save us the MSR write below - which is a particular expensive 344 * operation - when the other hyperthread has updated the microcode 345 * already. 346 */ 347 rev = intel_get_microcode_revision(); 348 if (rev >= mc->hdr.rev) { 349 uci->cpu_sig.rev = rev; 350 return UCODE_OK; 351 } 352 353 old_rev = rev; 354 355 /* 356 * Writeback and invalidate caches before updating microcode to avoid 357 * internal issues depending on what the microcode is updating. 358 */ 359 native_wbinvd(); 360 361 /* write microcode via MSR 0x79 */ 362 native_wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits); 363 364 rev = intel_get_microcode_revision(); 365 if (rev != mc->hdr.rev) 366 return -1; 367 368 uci->cpu_sig.rev = rev; 369 370 date = mc->hdr.date; 371 pr_info_once("updated early: 0x%x -> 0x%x, date = %04x-%02x-%02x\n", 372 old_rev, rev, date & 0xffff, date >> 24, (date >> 16) & 0xff); 373 return 0; 374 } 375 376 int __init save_microcode_in_initrd_intel(void) 377 { 378 struct ucode_cpu_info uci; 379 struct cpio_data cp; 380 381 /* 382 * initrd is going away, clear patch ptr. We will scan the microcode one 383 * last time before jettisoning and save a patch, if found. Then we will 384 * update that pointer too, with a stable patch address to use when 385 * resuming the cores. 386 */ 387 intel_ucode_patch = NULL; 388 389 if (!load_builtin_intel_microcode(&cp)) 390 cp = find_microcode_in_initrd(ucode_path); 391 392 if (!(cp.data && cp.size)) 393 return 0; 394 395 intel_cpu_collect_info(&uci); 396 397 scan_microcode(cp.data, cp.size, &uci, true); 398 return 0; 399 } 400 401 /* 402 * @res_patch, output: a pointer to the patch we found. 403 */ 404 static struct microcode_intel *__load_ucode_intel(struct ucode_cpu_info *uci) 405 { 406 struct cpio_data cp; 407 408 /* try built-in microcode first */ 409 if (!load_builtin_intel_microcode(&cp)) 410 cp = find_microcode_in_initrd(ucode_path); 411 412 if (!(cp.data && cp.size)) 413 return NULL; 414 415 intel_cpu_collect_info(uci); 416 417 return scan_microcode(cp.data, cp.size, uci, false); 418 } 419 420 void __init load_ucode_intel_bsp(void) 421 { 422 struct microcode_intel *patch; 423 struct ucode_cpu_info uci; 424 425 patch = __load_ucode_intel(&uci); 426 if (!patch) 427 return; 428 429 uci.mc = patch; 430 431 apply_microcode_early(&uci); 432 } 433 434 void load_ucode_intel_ap(void) 435 { 436 struct ucode_cpu_info uci; 437 438 if (!intel_ucode_patch) { 439 intel_ucode_patch = __load_ucode_intel(&uci); 440 if (!intel_ucode_patch) 441 return; 442 } 443 444 uci.mc = intel_ucode_patch; 445 apply_microcode_early(&uci); 446 } 447 448 /* Accessor for microcode pointer */ 449 static struct microcode_intel *ucode_get_patch(void) 450 { 451 return intel_ucode_patch; 452 } 453 454 void reload_ucode_intel(void) 455 { 456 struct microcode_intel *p; 457 struct ucode_cpu_info uci; 458 459 intel_cpu_collect_info(&uci); 460 461 p = ucode_get_patch(); 462 if (!p) 463 return; 464 465 uci.mc = p; 466 467 apply_microcode_early(&uci); 468 } 469 470 static int collect_cpu_info(int cpu_num, struct cpu_signature *csig) 471 { 472 struct cpuinfo_x86 *c = &cpu_data(cpu_num); 473 unsigned int val[2]; 474 475 memset(csig, 0, sizeof(*csig)); 476 477 csig->sig = cpuid_eax(0x00000001); 478 479 if ((c->x86_model >= 5) || (c->x86 > 6)) { 480 /* get processor flags from MSR 0x17 */ 481 rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]); 482 csig->pf = 1 << ((val[1] >> 18) & 7); 483 } 484 485 csig->rev = c->microcode; 486 487 return 0; 488 } 489 490 static enum ucode_state apply_microcode_intel(int cpu) 491 { 492 struct ucode_cpu_info *uci = ucode_cpu_info + cpu; 493 struct cpuinfo_x86 *c = &cpu_data(cpu); 494 bool bsp = c->cpu_index == boot_cpu_data.cpu_index; 495 struct microcode_intel *mc; 496 enum ucode_state ret; 497 static int prev_rev; 498 u32 rev; 499 500 /* We should bind the task to the CPU */ 501 if (WARN_ON(raw_smp_processor_id() != cpu)) 502 return UCODE_ERROR; 503 504 /* Look for a newer patch in our cache: */ 505 mc = ucode_get_patch(); 506 if (!mc) { 507 mc = uci->mc; 508 if (!mc) 509 return UCODE_NFOUND; 510 } 511 512 /* 513 * Save us the MSR write below - which is a particular expensive 514 * operation - when the other hyperthread has updated the microcode 515 * already. 516 */ 517 rev = intel_get_microcode_revision(); 518 if (rev >= mc->hdr.rev) { 519 ret = UCODE_OK; 520 goto out; 521 } 522 523 /* 524 * Writeback and invalidate caches before updating microcode to avoid 525 * internal issues depending on what the microcode is updating. 526 */ 527 native_wbinvd(); 528 529 /* write microcode via MSR 0x79 */ 530 wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits); 531 532 rev = intel_get_microcode_revision(); 533 534 if (rev != mc->hdr.rev) { 535 pr_err("CPU%d update to revision 0x%x failed\n", 536 cpu, mc->hdr.rev); 537 return UCODE_ERROR; 538 } 539 540 if (bsp && rev != prev_rev) { 541 pr_info("updated to revision 0x%x, date = %04x-%02x-%02x\n", 542 rev, 543 mc->hdr.date & 0xffff, 544 mc->hdr.date >> 24, 545 (mc->hdr.date >> 16) & 0xff); 546 prev_rev = rev; 547 } 548 549 ret = UCODE_UPDATED; 550 551 out: 552 uci->cpu_sig.rev = rev; 553 c->microcode = rev; 554 555 /* Update boot_cpu_data's revision too, if we're on the BSP: */ 556 if (bsp) 557 boot_cpu_data.microcode = rev; 558 559 return ret; 560 } 561 562 static enum ucode_state generic_load_microcode(int cpu, struct iov_iter *iter) 563 { 564 struct ucode_cpu_info *uci = ucode_cpu_info + cpu; 565 unsigned int curr_mc_size = 0, new_mc_size = 0; 566 enum ucode_state ret = UCODE_OK; 567 int new_rev = uci->cpu_sig.rev; 568 u8 *new_mc = NULL, *mc = NULL; 569 unsigned int csig, cpf; 570 571 while (iov_iter_count(iter)) { 572 struct microcode_header_intel mc_header; 573 unsigned int mc_size, data_size; 574 u8 *data; 575 576 if (!copy_from_iter_full(&mc_header, sizeof(mc_header), iter)) { 577 pr_err("error! Truncated or inaccessible header in microcode data file\n"); 578 break; 579 } 580 581 mc_size = get_totalsize(&mc_header); 582 if (mc_size < sizeof(mc_header)) { 583 pr_err("error! Bad data in microcode data file (totalsize too small)\n"); 584 break; 585 } 586 data_size = mc_size - sizeof(mc_header); 587 if (data_size > iov_iter_count(iter)) { 588 pr_err("error! Bad data in microcode data file (truncated file?)\n"); 589 break; 590 } 591 592 /* For performance reasons, reuse mc area when possible */ 593 if (!mc || mc_size > curr_mc_size) { 594 vfree(mc); 595 mc = vmalloc(mc_size); 596 if (!mc) 597 break; 598 curr_mc_size = mc_size; 599 } 600 601 memcpy(mc, &mc_header, sizeof(mc_header)); 602 data = mc + sizeof(mc_header); 603 if (!copy_from_iter_full(data, data_size, iter) || 604 intel_microcode_sanity_check(mc, true, MC_HEADER_TYPE_MICROCODE) < 0) { 605 break; 606 } 607 608 csig = uci->cpu_sig.sig; 609 cpf = uci->cpu_sig.pf; 610 if (has_newer_microcode(mc, csig, cpf, new_rev)) { 611 vfree(new_mc); 612 new_rev = mc_header.rev; 613 new_mc = mc; 614 new_mc_size = mc_size; 615 mc = NULL; /* trigger new vmalloc */ 616 ret = UCODE_NEW; 617 } 618 } 619 620 vfree(mc); 621 622 if (iov_iter_count(iter)) { 623 vfree(new_mc); 624 return UCODE_ERROR; 625 } 626 627 if (!new_mc) 628 return UCODE_NFOUND; 629 630 vfree(uci->mc); 631 uci->mc = (struct microcode_intel *)new_mc; 632 633 /* Save for CPU hotplug */ 634 save_microcode_patch(new_mc, new_mc_size); 635 636 pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n", 637 cpu, new_rev, uci->cpu_sig.rev); 638 639 return ret; 640 } 641 642 static bool is_blacklisted(unsigned int cpu) 643 { 644 struct cpuinfo_x86 *c = &cpu_data(cpu); 645 646 /* 647 * Late loading on model 79 with microcode revision less than 0x0b000021 648 * and LLC size per core bigger than 2.5MB may result in a system hang. 649 * This behavior is documented in item BDF90, #334165 (Intel Xeon 650 * Processor E7-8800/4800 v4 Product Family). 651 */ 652 if (c->x86 == 6 && 653 c->x86_model == INTEL_FAM6_BROADWELL_X && 654 c->x86_stepping == 0x01 && 655 llc_size_per_core > 2621440 && 656 c->microcode < 0x0b000021) { 657 pr_err_once("Erratum BDF90: late loading with revision < 0x0b000021 (0x%x) disabled.\n", c->microcode); 658 pr_err_once("Please consider either early loading through initrd/built-in or a potential BIOS update.\n"); 659 return true; 660 } 661 662 return false; 663 } 664 665 static enum ucode_state request_microcode_fw(int cpu, struct device *device) 666 { 667 struct cpuinfo_x86 *c = &cpu_data(cpu); 668 const struct firmware *firmware; 669 struct iov_iter iter; 670 enum ucode_state ret; 671 struct kvec kvec; 672 char name[30]; 673 674 if (is_blacklisted(cpu)) 675 return UCODE_NFOUND; 676 677 sprintf(name, "intel-ucode/%02x-%02x-%02x", 678 c->x86, c->x86_model, c->x86_stepping); 679 680 if (request_firmware_direct(&firmware, name, device)) { 681 pr_debug("data file %s load failed\n", name); 682 return UCODE_NFOUND; 683 } 684 685 kvec.iov_base = (void *)firmware->data; 686 kvec.iov_len = firmware->size; 687 iov_iter_kvec(&iter, ITER_SOURCE, &kvec, 1, firmware->size); 688 ret = generic_load_microcode(cpu, &iter); 689 690 release_firmware(firmware); 691 692 return ret; 693 } 694 695 static struct microcode_ops microcode_intel_ops = { 696 .request_microcode_fw = request_microcode_fw, 697 .collect_cpu_info = collect_cpu_info, 698 .apply_microcode = apply_microcode_intel, 699 }; 700 701 static int __init calc_llc_size_per_core(struct cpuinfo_x86 *c) 702 { 703 u64 llc_size = c->x86_cache_size * 1024ULL; 704 705 do_div(llc_size, c->x86_max_cores); 706 707 return (int)llc_size; 708 } 709 710 struct microcode_ops * __init init_intel_microcode(void) 711 { 712 struct cpuinfo_x86 *c = &boot_cpu_data; 713 714 if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 || 715 cpu_has(c, X86_FEATURE_IA64)) { 716 pr_err("Intel CPU family 0x%x not supported\n", c->x86); 717 return NULL; 718 } 719 720 llc_size_per_core = calc_llc_size_per_core(c); 721 722 return µcode_intel_ops; 723 } 724