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; 37 38 /* last level cache size per core */ 39 static int llc_size_per_core; 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 struct ucode_patch *memdup_patch(void *data, unsigned int size) 257 { 258 struct ucode_patch *p; 259 260 p = kzalloc(sizeof(struct ucode_patch), GFP_KERNEL); 261 if (!p) 262 return NULL; 263 264 p->data = kmemdup(data, size, GFP_KERNEL); 265 if (!p->data) { 266 kfree(p); 267 return NULL; 268 } 269 270 return p; 271 } 272 273 static void save_microcode_patch(struct ucode_cpu_info *uci, void *data, unsigned int size) 274 { 275 struct microcode_header_intel *mc_hdr, *mc_saved_hdr; 276 struct ucode_patch *iter, *tmp, *p = NULL; 277 bool prev_found = false; 278 unsigned int sig, pf; 279 280 mc_hdr = (struct microcode_header_intel *)data; 281 282 list_for_each_entry_safe(iter, tmp, µcode_cache, plist) { 283 mc_saved_hdr = (struct microcode_header_intel *)iter->data; 284 sig = mc_saved_hdr->sig; 285 pf = mc_saved_hdr->pf; 286 287 if (intel_find_matching_signature(data, sig, pf)) { 288 prev_found = true; 289 290 if (mc_hdr->rev <= mc_saved_hdr->rev) 291 continue; 292 293 p = memdup_patch(data, size); 294 if (!p) 295 pr_err("Error allocating buffer %p\n", data); 296 else { 297 list_replace(&iter->plist, &p->plist); 298 kfree(iter->data); 299 kfree(iter); 300 } 301 } 302 } 303 304 /* 305 * There weren't any previous patches found in the list cache; save the 306 * newly found. 307 */ 308 if (!prev_found) { 309 p = memdup_patch(data, size); 310 if (!p) 311 pr_err("Error allocating buffer for %p\n", data); 312 else 313 list_add_tail(&p->plist, µcode_cache); 314 } 315 316 if (!p) 317 return; 318 319 if (!intel_find_matching_signature(p->data, uci->cpu_sig.sig, uci->cpu_sig.pf)) 320 return; 321 322 /* 323 * Save for early loading. On 32-bit, that needs to be a physical 324 * address as the APs are running from physical addresses, before 325 * paging has been enabled. 326 */ 327 if (IS_ENABLED(CONFIG_X86_32)) 328 intel_ucode_patch = (struct microcode_intel *)__pa_nodebug(p->data); 329 else 330 intel_ucode_patch = p->data; 331 } 332 333 /* 334 * Get microcode matching with BSP's model. Only CPUs with the same model as 335 * BSP can stay in the platform. 336 */ 337 static struct microcode_intel * 338 scan_microcode(void *data, size_t size, struct ucode_cpu_info *uci, bool save) 339 { 340 struct microcode_header_intel *mc_header; 341 struct microcode_intel *patch = NULL; 342 unsigned int mc_size; 343 344 while (size) { 345 if (size < sizeof(struct microcode_header_intel)) 346 break; 347 348 mc_header = (struct microcode_header_intel *)data; 349 350 mc_size = get_totalsize(mc_header); 351 if (!mc_size || 352 mc_size > size || 353 intel_microcode_sanity_check(data, false, MC_HEADER_TYPE_MICROCODE) < 0) 354 break; 355 356 size -= mc_size; 357 358 if (!intel_find_matching_signature(data, uci->cpu_sig.sig, 359 uci->cpu_sig.pf)) { 360 data += mc_size; 361 continue; 362 } 363 364 if (save) { 365 save_microcode_patch(uci, data, mc_size); 366 goto next; 367 } 368 369 370 if (!patch) { 371 if (!has_newer_microcode(data, 372 uci->cpu_sig.sig, 373 uci->cpu_sig.pf, 374 uci->cpu_sig.rev)) 375 goto next; 376 377 } else { 378 struct microcode_header_intel *phdr = &patch->hdr; 379 380 if (!has_newer_microcode(data, 381 phdr->sig, 382 phdr->pf, 383 phdr->rev)) 384 goto next; 385 } 386 387 /* We have a newer patch, save it. */ 388 patch = data; 389 390 next: 391 data += mc_size; 392 } 393 394 if (size) 395 return NULL; 396 397 return patch; 398 } 399 400 /* 401 * Save this microcode patch. It will be loaded early when a CPU is 402 * hot-added or resumes. 403 */ 404 static void save_mc_for_early(struct ucode_cpu_info *uci, u8 *mc, unsigned int size) 405 { 406 /* Synchronization during CPU hotplug. */ 407 static DEFINE_MUTEX(x86_cpu_microcode_mutex); 408 409 mutex_lock(&x86_cpu_microcode_mutex); 410 411 save_microcode_patch(uci, mc, size); 412 413 mutex_unlock(&x86_cpu_microcode_mutex); 414 } 415 416 static bool load_builtin_intel_microcode(struct cpio_data *cp) 417 { 418 unsigned int eax = 1, ebx, ecx = 0, edx; 419 struct firmware fw; 420 char name[30]; 421 422 if (IS_ENABLED(CONFIG_X86_32)) 423 return false; 424 425 native_cpuid(&eax, &ebx, &ecx, &edx); 426 427 sprintf(name, "intel-ucode/%02x-%02x-%02x", 428 x86_family(eax), x86_model(eax), x86_stepping(eax)); 429 430 if (firmware_request_builtin(&fw, name)) { 431 cp->size = fw.size; 432 cp->data = (void *)fw.data; 433 return true; 434 } 435 436 return false; 437 } 438 439 static void print_ucode_info(int old_rev, int new_rev, unsigned int date) 440 { 441 pr_info_once("updated early: 0x%x -> 0x%x, date = %04x-%02x-%02x\n", 442 old_rev, 443 new_rev, 444 date & 0xffff, 445 date >> 24, 446 (date >> 16) & 0xff); 447 } 448 449 #ifdef CONFIG_X86_32 450 451 static int delay_ucode_info; 452 static int current_mc_date; 453 static int early_old_rev; 454 455 /* 456 * Print early updated ucode info after printk works. This is delayed info dump. 457 */ 458 void show_ucode_info_early(void) 459 { 460 struct ucode_cpu_info uci; 461 462 if (delay_ucode_info) { 463 intel_cpu_collect_info(&uci); 464 print_ucode_info(early_old_rev, uci.cpu_sig.rev, current_mc_date); 465 delay_ucode_info = 0; 466 } 467 } 468 469 /* 470 * At this point, we can not call printk() yet. Delay printing microcode info in 471 * show_ucode_info_early() until printk() works. 472 */ 473 static void print_ucode(int old_rev, int new_rev, int date) 474 { 475 int *delay_ucode_info_p; 476 int *current_mc_date_p; 477 int *early_old_rev_p; 478 479 delay_ucode_info_p = (int *)__pa_nodebug(&delay_ucode_info); 480 current_mc_date_p = (int *)__pa_nodebug(¤t_mc_date); 481 early_old_rev_p = (int *)__pa_nodebug(&early_old_rev); 482 483 *delay_ucode_info_p = 1; 484 *current_mc_date_p = date; 485 *early_old_rev_p = old_rev; 486 } 487 #else 488 489 static inline void print_ucode(int old_rev, int new_rev, int date) 490 { 491 print_ucode_info(old_rev, new_rev, date); 492 } 493 #endif 494 495 static int apply_microcode_early(struct ucode_cpu_info *uci, bool early) 496 { 497 struct microcode_intel *mc; 498 u32 rev, old_rev; 499 500 mc = uci->mc; 501 if (!mc) 502 return 0; 503 504 /* 505 * Save us the MSR write below - which is a particular expensive 506 * operation - when the other hyperthread has updated the microcode 507 * already. 508 */ 509 rev = intel_get_microcode_revision(); 510 if (rev >= mc->hdr.rev) { 511 uci->cpu_sig.rev = rev; 512 return UCODE_OK; 513 } 514 515 old_rev = rev; 516 517 /* 518 * Writeback and invalidate caches before updating microcode to avoid 519 * internal issues depending on what the microcode is updating. 520 */ 521 native_wbinvd(); 522 523 /* write microcode via MSR 0x79 */ 524 native_wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits); 525 526 rev = intel_get_microcode_revision(); 527 if (rev != mc->hdr.rev) 528 return -1; 529 530 uci->cpu_sig.rev = rev; 531 532 if (early) 533 print_ucode(old_rev, uci->cpu_sig.rev, mc->hdr.date); 534 else 535 print_ucode_info(old_rev, uci->cpu_sig.rev, mc->hdr.date); 536 537 return 0; 538 } 539 540 int __init save_microcode_in_initrd_intel(void) 541 { 542 struct ucode_cpu_info uci; 543 struct cpio_data cp; 544 545 /* 546 * initrd is going away, clear patch ptr. We will scan the microcode one 547 * last time before jettisoning and save a patch, if found. Then we will 548 * update that pointer too, with a stable patch address to use when 549 * resuming the cores. 550 */ 551 intel_ucode_patch = NULL; 552 553 if (!load_builtin_intel_microcode(&cp)) 554 cp = find_microcode_in_initrd(ucode_path, false); 555 556 if (!(cp.data && cp.size)) 557 return 0; 558 559 intel_cpu_collect_info(&uci); 560 561 scan_microcode(cp.data, cp.size, &uci, true); 562 return 0; 563 } 564 565 /* 566 * @res_patch, output: a pointer to the patch we found. 567 */ 568 static struct microcode_intel *__load_ucode_intel(struct ucode_cpu_info *uci) 569 { 570 static const char *path; 571 struct cpio_data cp; 572 bool use_pa; 573 574 if (IS_ENABLED(CONFIG_X86_32)) { 575 path = (const char *)__pa_nodebug(ucode_path); 576 use_pa = true; 577 } else { 578 path = ucode_path; 579 use_pa = false; 580 } 581 582 /* try built-in microcode first */ 583 if (!load_builtin_intel_microcode(&cp)) 584 cp = find_microcode_in_initrd(path, use_pa); 585 586 if (!(cp.data && cp.size)) 587 return NULL; 588 589 intel_cpu_collect_info(uci); 590 591 return scan_microcode(cp.data, cp.size, uci, false); 592 } 593 594 void __init load_ucode_intel_bsp(void) 595 { 596 struct microcode_intel *patch; 597 struct ucode_cpu_info uci; 598 599 patch = __load_ucode_intel(&uci); 600 if (!patch) 601 return; 602 603 uci.mc = patch; 604 605 apply_microcode_early(&uci, true); 606 } 607 608 void load_ucode_intel_ap(void) 609 { 610 struct microcode_intel *patch, **iup; 611 struct ucode_cpu_info uci; 612 613 if (IS_ENABLED(CONFIG_X86_32)) 614 iup = (struct microcode_intel **) __pa_nodebug(&intel_ucode_patch); 615 else 616 iup = &intel_ucode_patch; 617 618 if (!*iup) { 619 patch = __load_ucode_intel(&uci); 620 if (!patch) 621 return; 622 623 *iup = patch; 624 } 625 626 uci.mc = *iup; 627 628 apply_microcode_early(&uci, true); 629 } 630 631 static struct microcode_intel *find_patch(struct ucode_cpu_info *uci) 632 { 633 struct microcode_header_intel *phdr; 634 struct ucode_patch *iter, *tmp; 635 636 list_for_each_entry_safe(iter, tmp, µcode_cache, plist) { 637 638 phdr = (struct microcode_header_intel *)iter->data; 639 640 if (phdr->rev <= uci->cpu_sig.rev) 641 continue; 642 643 if (!intel_find_matching_signature(phdr, 644 uci->cpu_sig.sig, 645 uci->cpu_sig.pf)) 646 continue; 647 648 return iter->data; 649 } 650 return NULL; 651 } 652 653 void reload_ucode_intel(void) 654 { 655 struct microcode_intel *p; 656 struct ucode_cpu_info uci; 657 658 intel_cpu_collect_info(&uci); 659 660 p = find_patch(&uci); 661 if (!p) 662 return; 663 664 uci.mc = p; 665 666 apply_microcode_early(&uci, false); 667 } 668 669 static int collect_cpu_info(int cpu_num, struct cpu_signature *csig) 670 { 671 struct cpuinfo_x86 *c = &cpu_data(cpu_num); 672 unsigned int val[2]; 673 674 memset(csig, 0, sizeof(*csig)); 675 676 csig->sig = cpuid_eax(0x00000001); 677 678 if ((c->x86_model >= 5) || (c->x86 > 6)) { 679 /* get processor flags from MSR 0x17 */ 680 rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]); 681 csig->pf = 1 << ((val[1] >> 18) & 7); 682 } 683 684 csig->rev = c->microcode; 685 686 return 0; 687 } 688 689 static enum ucode_state apply_microcode_intel(int cpu) 690 { 691 struct ucode_cpu_info *uci = ucode_cpu_info + cpu; 692 struct cpuinfo_x86 *c = &cpu_data(cpu); 693 bool bsp = c->cpu_index == boot_cpu_data.cpu_index; 694 struct microcode_intel *mc; 695 enum ucode_state ret; 696 static int prev_rev; 697 u32 rev; 698 699 /* We should bind the task to the CPU */ 700 if (WARN_ON(raw_smp_processor_id() != cpu)) 701 return UCODE_ERROR; 702 703 /* Look for a newer patch in our cache: */ 704 mc = find_patch(uci); 705 if (!mc) { 706 mc = uci->mc; 707 if (!mc) 708 return UCODE_NFOUND; 709 } 710 711 /* 712 * Save us the MSR write below - which is a particular expensive 713 * operation - when the other hyperthread has updated the microcode 714 * already. 715 */ 716 rev = intel_get_microcode_revision(); 717 if (rev >= mc->hdr.rev) { 718 ret = UCODE_OK; 719 goto out; 720 } 721 722 /* 723 * Writeback and invalidate caches before updating microcode to avoid 724 * internal issues depending on what the microcode is updating. 725 */ 726 native_wbinvd(); 727 728 /* write microcode via MSR 0x79 */ 729 wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits); 730 731 rev = intel_get_microcode_revision(); 732 733 if (rev != mc->hdr.rev) { 734 pr_err("CPU%d update to revision 0x%x failed\n", 735 cpu, mc->hdr.rev); 736 return UCODE_ERROR; 737 } 738 739 if (bsp && rev != prev_rev) { 740 pr_info("updated to revision 0x%x, date = %04x-%02x-%02x\n", 741 rev, 742 mc->hdr.date & 0xffff, 743 mc->hdr.date >> 24, 744 (mc->hdr.date >> 16) & 0xff); 745 prev_rev = rev; 746 } 747 748 ret = UCODE_UPDATED; 749 750 out: 751 uci->cpu_sig.rev = rev; 752 c->microcode = rev; 753 754 /* Update boot_cpu_data's revision too, if we're on the BSP: */ 755 if (bsp) 756 boot_cpu_data.microcode = rev; 757 758 return ret; 759 } 760 761 static enum ucode_state generic_load_microcode(int cpu, struct iov_iter *iter) 762 { 763 struct ucode_cpu_info *uci = ucode_cpu_info + cpu; 764 unsigned int curr_mc_size = 0, new_mc_size = 0; 765 enum ucode_state ret = UCODE_OK; 766 int new_rev = uci->cpu_sig.rev; 767 u8 *new_mc = NULL, *mc = NULL; 768 unsigned int csig, cpf; 769 770 while (iov_iter_count(iter)) { 771 struct microcode_header_intel mc_header; 772 unsigned int mc_size, data_size; 773 u8 *data; 774 775 if (!copy_from_iter_full(&mc_header, sizeof(mc_header), iter)) { 776 pr_err("error! Truncated or inaccessible header in microcode data file\n"); 777 break; 778 } 779 780 mc_size = get_totalsize(&mc_header); 781 if (mc_size < sizeof(mc_header)) { 782 pr_err("error! Bad data in microcode data file (totalsize too small)\n"); 783 break; 784 } 785 data_size = mc_size - sizeof(mc_header); 786 if (data_size > iov_iter_count(iter)) { 787 pr_err("error! Bad data in microcode data file (truncated file?)\n"); 788 break; 789 } 790 791 /* For performance reasons, reuse mc area when possible */ 792 if (!mc || mc_size > curr_mc_size) { 793 vfree(mc); 794 mc = vmalloc(mc_size); 795 if (!mc) 796 break; 797 curr_mc_size = mc_size; 798 } 799 800 memcpy(mc, &mc_header, sizeof(mc_header)); 801 data = mc + sizeof(mc_header); 802 if (!copy_from_iter_full(data, data_size, iter) || 803 intel_microcode_sanity_check(mc, true, MC_HEADER_TYPE_MICROCODE) < 0) { 804 break; 805 } 806 807 csig = uci->cpu_sig.sig; 808 cpf = uci->cpu_sig.pf; 809 if (has_newer_microcode(mc, csig, cpf, new_rev)) { 810 vfree(new_mc); 811 new_rev = mc_header.rev; 812 new_mc = mc; 813 new_mc_size = mc_size; 814 mc = NULL; /* trigger new vmalloc */ 815 ret = UCODE_NEW; 816 } 817 } 818 819 vfree(mc); 820 821 if (iov_iter_count(iter)) { 822 vfree(new_mc); 823 return UCODE_ERROR; 824 } 825 826 if (!new_mc) 827 return UCODE_NFOUND; 828 829 vfree(uci->mc); 830 uci->mc = (struct microcode_intel *)new_mc; 831 832 /* 833 * If early loading microcode is supported, save this mc into 834 * permanent memory. So it will be loaded early when a CPU is hot added 835 * or resumes. 836 */ 837 save_mc_for_early(uci, new_mc, new_mc_size); 838 839 pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n", 840 cpu, new_rev, uci->cpu_sig.rev); 841 842 return ret; 843 } 844 845 static bool is_blacklisted(unsigned int cpu) 846 { 847 struct cpuinfo_x86 *c = &cpu_data(cpu); 848 849 /* 850 * Late loading on model 79 with microcode revision less than 0x0b000021 851 * and LLC size per core bigger than 2.5MB may result in a system hang. 852 * This behavior is documented in item BDF90, #334165 (Intel Xeon 853 * Processor E7-8800/4800 v4 Product Family). 854 */ 855 if (c->x86 == 6 && 856 c->x86_model == INTEL_FAM6_BROADWELL_X && 857 c->x86_stepping == 0x01 && 858 llc_size_per_core > 2621440 && 859 c->microcode < 0x0b000021) { 860 pr_err_once("Erratum BDF90: late loading with revision < 0x0b000021 (0x%x) disabled.\n", c->microcode); 861 pr_err_once("Please consider either early loading through initrd/built-in or a potential BIOS update.\n"); 862 return true; 863 } 864 865 return false; 866 } 867 868 static enum ucode_state request_microcode_fw(int cpu, struct device *device) 869 { 870 struct cpuinfo_x86 *c = &cpu_data(cpu); 871 const struct firmware *firmware; 872 struct iov_iter iter; 873 enum ucode_state ret; 874 struct kvec kvec; 875 char name[30]; 876 877 if (is_blacklisted(cpu)) 878 return UCODE_NFOUND; 879 880 sprintf(name, "intel-ucode/%02x-%02x-%02x", 881 c->x86, c->x86_model, c->x86_stepping); 882 883 if (request_firmware_direct(&firmware, name, device)) { 884 pr_debug("data file %s load failed\n", name); 885 return UCODE_NFOUND; 886 } 887 888 kvec.iov_base = (void *)firmware->data; 889 kvec.iov_len = firmware->size; 890 iov_iter_kvec(&iter, ITER_SOURCE, &kvec, 1, firmware->size); 891 ret = generic_load_microcode(cpu, &iter); 892 893 release_firmware(firmware); 894 895 return ret; 896 } 897 898 static struct microcode_ops microcode_intel_ops = { 899 .request_microcode_fw = request_microcode_fw, 900 .collect_cpu_info = collect_cpu_info, 901 .apply_microcode = apply_microcode_intel, 902 }; 903 904 static int __init calc_llc_size_per_core(struct cpuinfo_x86 *c) 905 { 906 u64 llc_size = c->x86_cache_size * 1024ULL; 907 908 do_div(llc_size, c->x86_max_cores); 909 910 return (int)llc_size; 911 } 912 913 struct microcode_ops * __init init_intel_microcode(void) 914 { 915 struct cpuinfo_x86 *c = &boot_cpu_data; 916 917 if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 || 918 cpu_has(c, X86_FEATURE_IA64)) { 919 pr_err("Intel CPU family 0x%x not supported\n", c->x86); 920 return NULL; 921 } 922 923 llc_size_per_core = calc_llc_size_per_core(c); 924 925 return µcode_intel_ops; 926 } 927