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