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 <aivazian.tigran@gmail.com> 6 * 2006 Shaohua Li <shaohua.li@intel.com> 7 * 8 * Intel CPU microcode early update for Linux 9 * 10 * Copyright (C) 2012 Fenghua Yu <fenghua.yu@intel.com> 11 * H Peter Anvin" <hpa@zytor.com> 12 */ 13 #define pr_fmt(fmt) "microcode: " fmt 14 #include <linux/earlycpio.h> 15 #include <linux/firmware.h> 16 #include <linux/pci_ids.h> 17 #include <linux/uaccess.h> 18 #include <linux/initrd.h> 19 #include <linux/kernel.h> 20 #include <linux/delay.h> 21 #include <linux/slab.h> 22 #include <linux/cpu.h> 23 #include <linux/uio.h> 24 #include <linux/io.h> 25 #include <linux/mm.h> 26 27 #include <asm/cpu_device_id.h> 28 #include <asm/cpuid/api.h> 29 #include <asm/processor.h> 30 #include <asm/tlbflush.h> 31 #include <asm/setup.h> 32 #include <asm/msr.h> 33 34 #include "internal.h" 35 36 static const char ucode_path[] = "kernel/x86/microcode/GenuineIntel.bin"; 37 38 #define UCODE_BSP_LOADED ((struct microcode_intel *)0x1UL) 39 40 /* Defines for the microcode staging mailbox interface */ 41 #define MBOX_REG_NUM 4 42 #define MBOX_REG_SIZE sizeof(u32) 43 44 #define MBOX_CONTROL_OFFSET 0x0 45 #define MBOX_STATUS_OFFSET 0x4 46 #define MBOX_WRDATA_OFFSET 0x8 47 #define MBOX_RDDATA_OFFSET 0xc 48 49 #define MASK_MBOX_CTRL_ABORT BIT(0) 50 #define MASK_MBOX_CTRL_GO BIT(31) 51 52 #define MASK_MBOX_STATUS_ERROR BIT(2) 53 #define MASK_MBOX_STATUS_READY BIT(31) 54 55 #define MASK_MBOX_RESP_SUCCESS BIT(0) 56 #define MASK_MBOX_RESP_PROGRESS BIT(1) 57 #define MASK_MBOX_RESP_ERROR BIT(2) 58 59 #define MBOX_CMD_LOAD 0x3 60 #define MBOX_OBJ_STAGING 0xb 61 #define MBOX_HEADER(size) ((PCI_VENDOR_ID_INTEL) | \ 62 (MBOX_OBJ_STAGING << 16) | \ 63 ((u64)((size) / sizeof(u32)) << 32)) 64 65 /* The size of each mailbox header */ 66 #define MBOX_HEADER_SIZE sizeof(u64) 67 /* The size of staging hardware response */ 68 #define MBOX_RESPONSE_SIZE sizeof(u64) 69 70 #define MBOX_XACTION_TIMEOUT_MS (10 * MSEC_PER_SEC) 71 72 /* Current microcode patch used in early patching on the APs. */ 73 static struct microcode_intel *ucode_patch_va __read_mostly; 74 static struct microcode_intel *ucode_patch_late __read_mostly; 75 76 /* last level cache size per core */ 77 static unsigned int llc_size_per_core __ro_after_init; 78 79 /* microcode format is extended from prescott processors */ 80 struct extended_signature { 81 unsigned int sig; 82 unsigned int pf; 83 unsigned int cksum; 84 }; 85 86 struct extended_sigtable { 87 unsigned int count; 88 unsigned int cksum; 89 unsigned int reserved[3]; 90 struct extended_signature sigs[]; 91 }; 92 93 /** 94 * struct staging_state - Track the current staging process state 95 * 96 * @mmio_base: MMIO base address for staging 97 * @ucode_len: Total size of the microcode image 98 * @chunk_size: Size of each data piece 99 * @bytes_sent: Total bytes transmitted so far 100 * @offset: Current offset in the microcode image 101 */ 102 struct staging_state { 103 void __iomem *mmio_base; 104 unsigned int ucode_len; 105 unsigned int chunk_size; 106 unsigned int bytes_sent; 107 unsigned int offset; 108 }; 109 110 #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE) 111 #define EXT_HEADER_SIZE (sizeof(struct extended_sigtable)) 112 #define EXT_SIGNATURE_SIZE (sizeof(struct extended_signature)) 113 114 static inline unsigned int get_totalsize(struct microcode_header_intel *hdr) 115 { 116 return hdr->datasize ? hdr->totalsize : DEFAULT_UCODE_TOTALSIZE; 117 } 118 119 static inline unsigned int exttable_size(struct extended_sigtable *et) 120 { 121 return et->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE; 122 } 123 124 125 /* 126 * Use CPUID to generate a "vfm" value. Useful before cpuinfo_x86 127 * structures are populated. 128 */ 129 static u32 intel_cpuid_vfm(void) 130 { 131 u32 eax = cpuid_eax(1); 132 u32 fam = x86_family(eax); 133 u32 model = x86_model(eax); 134 135 return IFM(fam, model); 136 } 137 138 u32 intel_get_platform_id(void) 139 { 140 unsigned int val[2]; 141 142 /* 143 * This can be called early. Use CPUID directly instead of 144 * relying on cpuinfo_x86 which may not be fully initialized. 145 * The PII does not have MSR_IA32_PLATFORM_ID. Everything 146 * before _it_ has no microcode (for Linux at least). 147 */ 148 if (intel_cpuid_vfm() <= INTEL_PENTIUM_II_KLAMATH) 149 return 0; 150 151 /* get processor flags from MSR 0x17 */ 152 native_rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]); 153 154 return (val[1] >> 18) & 7; 155 } 156 157 void intel_collect_cpu_info(struct cpu_signature *sig) 158 { 159 sig->sig = cpuid_eax(1); 160 sig->rev = intel_get_microcode_revision(); 161 sig->pf = 1 << intel_get_platform_id(); 162 } 163 EXPORT_SYMBOL_GPL(intel_collect_cpu_info); 164 165 static inline bool cpu_signatures_match(struct cpu_signature *s1, unsigned int sig2, 166 unsigned int pf2) 167 { 168 if (s1->sig != sig2) 169 return false; 170 171 /* 172 * Consider an empty mask to match everything. This 173 * should only occur for one CPU model, the PII. 174 */ 175 if (!pf2) 176 return true; 177 178 /* Is the CPU's platform ID in the signature mask? */ 179 return s1->pf & pf2; 180 } 181 182 bool intel_find_matching_signature(void *mc, struct cpu_signature *sig) 183 { 184 struct microcode_header_intel *mc_hdr = mc; 185 struct extended_signature *ext_sig; 186 struct extended_sigtable *ext_hdr; 187 int i; 188 189 if (cpu_signatures_match(sig, mc_hdr->sig, mc_hdr->pf)) 190 return true; 191 192 /* Look for ext. headers: */ 193 if (get_totalsize(mc_hdr) <= intel_microcode_get_datasize(mc_hdr) + MC_HEADER_SIZE) 194 return false; 195 196 ext_hdr = mc + intel_microcode_get_datasize(mc_hdr) + MC_HEADER_SIZE; 197 ext_sig = (void *)ext_hdr + EXT_HEADER_SIZE; 198 199 for (i = 0; i < ext_hdr->count; i++) { 200 if (cpu_signatures_match(sig, ext_sig->sig, ext_sig->pf)) 201 return true; 202 ext_sig++; 203 } 204 return 0; 205 } 206 EXPORT_SYMBOL_GPL(intel_find_matching_signature); 207 208 /** 209 * intel_microcode_sanity_check() - Sanity check microcode file. 210 * @mc: Pointer to the microcode file contents. 211 * @print_err: Display failure reason if true, silent if false. 212 * @hdr_type: Type of file, i.e. normal microcode file or In Field Scan file. 213 * Validate if the microcode header type matches with the type 214 * specified here. 215 * 216 * Validate certain header fields and verify if computed checksum matches 217 * with the one specified in the header. 218 * 219 * Return: 0 if the file passes all the checks, -EINVAL if any of the checks 220 * fail. 221 */ 222 int intel_microcode_sanity_check(void *mc, bool print_err, int hdr_type) 223 { 224 unsigned long total_size, data_size, ext_table_size; 225 struct microcode_header_intel *mc_header = mc; 226 struct extended_sigtable *ext_header = NULL; 227 u32 sum, orig_sum, ext_sigcount = 0, i; 228 struct extended_signature *ext_sig; 229 230 total_size = get_totalsize(mc_header); 231 data_size = intel_microcode_get_datasize(mc_header); 232 233 if (data_size + MC_HEADER_SIZE > total_size) { 234 if (print_err) 235 pr_err("Error: bad microcode data file size.\n"); 236 return -EINVAL; 237 } 238 239 if (mc_header->ldrver != 1 || mc_header->hdrver != hdr_type) { 240 if (print_err) 241 pr_err("Error: invalid/unknown microcode update format. Header type %d\n", 242 mc_header->hdrver); 243 return -EINVAL; 244 } 245 246 ext_table_size = total_size - (MC_HEADER_SIZE + data_size); 247 if (ext_table_size) { 248 u32 ext_table_sum = 0; 249 u32 *ext_tablep; 250 251 if (ext_table_size < EXT_HEADER_SIZE || 252 ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) { 253 if (print_err) 254 pr_err("Error: truncated extended signature table.\n"); 255 return -EINVAL; 256 } 257 258 ext_header = mc + MC_HEADER_SIZE + data_size; 259 if (ext_table_size != exttable_size(ext_header)) { 260 if (print_err) 261 pr_err("Error: extended signature table size mismatch.\n"); 262 return -EFAULT; 263 } 264 265 ext_sigcount = ext_header->count; 266 267 /* 268 * Check extended table checksum: the sum of all dwords that 269 * comprise a valid table must be 0. 270 */ 271 ext_tablep = (u32 *)ext_header; 272 273 i = ext_table_size / sizeof(u32); 274 while (i--) 275 ext_table_sum += ext_tablep[i]; 276 277 if (ext_table_sum) { 278 if (print_err) 279 pr_warn("Bad extended signature table checksum, aborting.\n"); 280 return -EINVAL; 281 } 282 } 283 284 /* 285 * Calculate the checksum of update data and header. The checksum of 286 * valid update data and header including the extended signature table 287 * must be 0. 288 */ 289 orig_sum = 0; 290 i = (MC_HEADER_SIZE + data_size) / sizeof(u32); 291 while (i--) 292 orig_sum += ((u32 *)mc)[i]; 293 294 if (orig_sum) { 295 if (print_err) 296 pr_err("Bad microcode data checksum, aborting.\n"); 297 return -EINVAL; 298 } 299 300 if (!ext_table_size) 301 return 0; 302 303 /* 304 * Check extended signature checksum: 0 => valid. 305 */ 306 for (i = 0; i < ext_sigcount; i++) { 307 ext_sig = (void *)ext_header + EXT_HEADER_SIZE + 308 EXT_SIGNATURE_SIZE * i; 309 310 sum = (mc_header->sig + mc_header->pf + mc_header->cksum) - 311 (ext_sig->sig + ext_sig->pf + ext_sig->cksum); 312 if (sum) { 313 if (print_err) 314 pr_err("Bad extended signature checksum, aborting.\n"); 315 return -EINVAL; 316 } 317 } 318 return 0; 319 } 320 EXPORT_SYMBOL_GPL(intel_microcode_sanity_check); 321 322 static void update_ucode_pointer(struct microcode_intel *mc) 323 { 324 kvfree(ucode_patch_va); 325 326 /* 327 * Save the virtual address for early loading and for eventual free 328 * on late loading. 329 */ 330 ucode_patch_va = mc; 331 } 332 333 static void save_microcode_patch(struct microcode_intel *patch) 334 { 335 unsigned int size = get_totalsize(&patch->hdr); 336 struct microcode_intel *mc; 337 338 mc = kvmemdup(patch, size, GFP_KERNEL); 339 if (mc) 340 update_ucode_pointer(mc); 341 else 342 pr_err("Unable to allocate microcode memory size: %u\n", size); 343 } 344 345 /* Scan blob for microcode matching the boot CPUs family, model, stepping */ 346 static __init struct microcode_intel *scan_microcode(void *data, size_t size, 347 struct ucode_cpu_info *uci, 348 bool save) 349 { 350 struct microcode_header_intel *mc_header; 351 struct microcode_intel *patch = NULL; 352 u32 cur_rev = uci->cpu_sig.rev; 353 unsigned int mc_size; 354 355 for (; size >= sizeof(struct microcode_header_intel); size -= mc_size, data += mc_size) { 356 mc_header = (struct microcode_header_intel *)data; 357 358 mc_size = get_totalsize(mc_header); 359 if (!mc_size || mc_size > size || 360 intel_microcode_sanity_check(data, false, MC_HEADER_TYPE_MICROCODE) < 0) 361 break; 362 363 if (!intel_find_matching_signature(data, &uci->cpu_sig)) 364 continue; 365 366 /* 367 * For saving the early microcode, find the matching revision which 368 * was loaded on the BSP. 369 * 370 * On the BSP during early boot, find a newer revision than 371 * actually loaded in the CPU. 372 */ 373 if (save) { 374 if (cur_rev != mc_header->rev) 375 continue; 376 } else if (cur_rev >= mc_header->rev) { 377 continue; 378 } 379 380 patch = data; 381 cur_rev = mc_header->rev; 382 } 383 384 return size ? NULL : patch; 385 } 386 387 static inline u32 read_mbox_dword(void __iomem *mmio_base) 388 { 389 u32 dword = readl(mmio_base + MBOX_RDDATA_OFFSET); 390 391 /* Acknowledge read completion to the staging hardware */ 392 writel(0, mmio_base + MBOX_RDDATA_OFFSET); 393 return dword; 394 } 395 396 static inline void write_mbox_dword(void __iomem *mmio_base, u32 dword) 397 { 398 writel(dword, mmio_base + MBOX_WRDATA_OFFSET); 399 } 400 401 static inline u64 read_mbox_header(void __iomem *mmio_base) 402 { 403 u32 high, low; 404 405 low = read_mbox_dword(mmio_base); 406 high = read_mbox_dword(mmio_base); 407 408 return ((u64)high << 32) | low; 409 } 410 411 static inline void write_mbox_header(void __iomem *mmio_base, u64 value) 412 { 413 write_mbox_dword(mmio_base, value); 414 write_mbox_dword(mmio_base, value >> 32); 415 } 416 417 static void write_mbox_data(void __iomem *mmio_base, u32 *chunk, unsigned int chunk_bytes) 418 { 419 int i; 420 421 /* 422 * The MMIO space is mapped as Uncached (UC). Each write arrives 423 * at the device as an individual transaction in program order. 424 * The device can then reassemble the sequence accordingly. 425 */ 426 for (i = 0; i < chunk_bytes / sizeof(u32); i++) 427 write_mbox_dword(mmio_base, chunk[i]); 428 } 429 430 /* 431 * Prepare for a new microcode transfer: reset hardware and record the 432 * image size. 433 */ 434 static void init_stage(struct staging_state *ss) 435 { 436 ss->ucode_len = get_totalsize(&ucode_patch_late->hdr); 437 438 /* 439 * Abort any ongoing process, effectively resetting the device. 440 * Unlike regular mailbox data processing requests, this 441 * operation does not require a status check. 442 */ 443 writel(MASK_MBOX_CTRL_ABORT, ss->mmio_base + MBOX_CONTROL_OFFSET); 444 } 445 446 /* 447 * Update the chunk size and decide whether another chunk can be sent. 448 * This accounts for remaining data and retry limits. 449 */ 450 static bool can_send_next_chunk(struct staging_state *ss, int *err) 451 { 452 /* A page size or remaining bytes if this is the final chunk */ 453 ss->chunk_size = min(PAGE_SIZE, ss->ucode_len - ss->offset); 454 455 /* 456 * Each microcode image is divided into chunks, each at most 457 * one page size. A 10-chunk image would typically require 10 458 * transactions. 459 * 460 * However, the hardware managing the mailbox has limited 461 * resources and may not cache the entire image, potentially 462 * requesting the same chunk multiple times. 463 * 464 * To tolerate this behavior, allow up to twice the expected 465 * number of transactions (i.e., a 10-chunk image can take up to 466 * 20 attempts). 467 * 468 * If the number of attempts exceeds this limit, treat it as 469 * exceeding the maximum allowed transfer size. 470 */ 471 if (ss->bytes_sent + ss->chunk_size > ss->ucode_len * 2) { 472 *err = -EMSGSIZE; 473 return false; 474 } 475 476 *err = 0; 477 return true; 478 } 479 480 /* 481 * The hardware indicates completion by returning a sentinel end offset. 482 */ 483 static inline bool is_end_offset(u32 offset) 484 { 485 return offset == UINT_MAX; 486 } 487 488 /* 489 * Determine whether staging is complete: either the hardware signaled 490 * the end offset, or no more transactions are permitted (retry limit 491 * reached). 492 */ 493 static inline bool staging_is_complete(struct staging_state *ss, int *err) 494 { 495 return is_end_offset(ss->offset) || !can_send_next_chunk(ss, err); 496 } 497 498 /* 499 * Wait for the hardware to complete a transaction. 500 * Return 0 on success, or an error code on failure. 501 */ 502 static int wait_for_transaction(struct staging_state *ss) 503 { 504 u32 timeout, status; 505 506 /* Allow time for hardware to complete the operation: */ 507 for (timeout = 0; timeout < MBOX_XACTION_TIMEOUT_MS; timeout++) { 508 msleep(1); 509 510 status = readl(ss->mmio_base + MBOX_STATUS_OFFSET); 511 /* Break out early if the hardware is ready: */ 512 if (status & MASK_MBOX_STATUS_READY) 513 break; 514 } 515 516 /* Check for explicit error response */ 517 if (status & MASK_MBOX_STATUS_ERROR) 518 return -EIO; 519 520 /* 521 * Hardware has neither responded to the action nor signaled any 522 * error. Treat this as a timeout. 523 */ 524 if (!(status & MASK_MBOX_STATUS_READY)) 525 return -ETIMEDOUT; 526 527 return 0; 528 } 529 530 /* 531 * Transmit a chunk of the microcode image to the hardware. 532 * Return 0 on success, or an error code on failure. 533 */ 534 static int send_data_chunk(struct staging_state *ss, void *ucode_ptr) 535 { 536 u32 *src_chunk = ucode_ptr + ss->offset; 537 u16 mbox_size; 538 539 /* 540 * Write a 'request' mailbox object in this order: 541 * 1. Mailbox header includes total size 542 * 2. Command header specifies the load operation 543 * 3. Data section contains a microcode chunk 544 * 545 * Thus, the mailbox size is two headers plus the chunk size. 546 */ 547 mbox_size = MBOX_HEADER_SIZE * 2 + ss->chunk_size; 548 write_mbox_header(ss->mmio_base, MBOX_HEADER(mbox_size)); 549 write_mbox_header(ss->mmio_base, MBOX_CMD_LOAD); 550 write_mbox_data(ss->mmio_base, src_chunk, ss->chunk_size); 551 ss->bytes_sent += ss->chunk_size; 552 553 /* Notify the hardware that the mailbox is ready for processing. */ 554 writel(MASK_MBOX_CTRL_GO, ss->mmio_base + MBOX_CONTROL_OFFSET); 555 556 return wait_for_transaction(ss); 557 } 558 559 /* 560 * Retrieve the next offset from the hardware response. 561 * Return 0 on success, or an error code on failure. 562 */ 563 static int fetch_next_offset(struct staging_state *ss) 564 { 565 const u64 expected_header = MBOX_HEADER(MBOX_HEADER_SIZE + MBOX_RESPONSE_SIZE); 566 u32 offset, status; 567 u64 header; 568 569 /* 570 * The 'response' mailbox returns three fields, in order: 571 * 1. Header 572 * 2. Next offset in the microcode image 573 * 3. Status flags 574 */ 575 header = read_mbox_header(ss->mmio_base); 576 offset = read_mbox_dword(ss->mmio_base); 577 status = read_mbox_dword(ss->mmio_base); 578 579 /* All valid responses must start with the expected header. */ 580 if (header != expected_header) { 581 pr_err_once("staging: invalid response header (0x%llx)\n", header); 582 return -EBADR; 583 } 584 585 /* 586 * Verify the offset: If not at the end marker, it must not 587 * exceed the microcode image length. 588 */ 589 if (!is_end_offset(offset) && offset > ss->ucode_len) { 590 pr_err_once("staging: invalid offset (%u) past the image end (%u)\n", 591 offset, ss->ucode_len); 592 return -EINVAL; 593 } 594 595 /* Hardware may report errors explicitly in the status field */ 596 if (status & MASK_MBOX_RESP_ERROR) 597 return -EPROTO; 598 599 ss->offset = offset; 600 return 0; 601 } 602 603 /* 604 * Handle the staging process using the mailbox MMIO interface. The 605 * microcode image is transferred in chunks until completion. 606 * Return 0 on success or an error code on failure. 607 */ 608 static int do_stage(u64 mmio_pa) 609 { 610 struct staging_state ss = {}; 611 int err; 612 613 ss.mmio_base = ioremap(mmio_pa, MBOX_REG_NUM * MBOX_REG_SIZE); 614 if (WARN_ON_ONCE(!ss.mmio_base)) 615 return -EADDRNOTAVAIL; 616 617 init_stage(&ss); 618 619 /* Perform the staging process while within the retry limit */ 620 while (!staging_is_complete(&ss, &err)) { 621 /* Send a chunk of microcode each time: */ 622 err = send_data_chunk(&ss, ucode_patch_late); 623 if (err) 624 break; 625 /* 626 * Then, ask the hardware which piece of the image it 627 * needs next. The same piece may be sent more than once. 628 */ 629 err = fetch_next_offset(&ss); 630 if (err) 631 break; 632 } 633 634 iounmap(ss.mmio_base); 635 636 return err; 637 } 638 639 static void stage_microcode(void) 640 { 641 unsigned int pkg_id = UINT_MAX; 642 int cpu, err; 643 u64 mmio_pa; 644 645 if (!IS_ALIGNED(get_totalsize(&ucode_patch_late->hdr), sizeof(u32))) { 646 pr_err("Microcode image 32-bit misaligned (0x%x), staging failed.\n", 647 get_totalsize(&ucode_patch_late->hdr)); 648 return; 649 } 650 651 lockdep_assert_cpus_held(); 652 653 /* 654 * The MMIO address is unique per package, and all the SMT 655 * primary threads are online here. Find each MMIO space by 656 * their package IDs to avoid duplicate staging. 657 */ 658 for_each_cpu(cpu, cpu_primary_thread_mask) { 659 if (topology_logical_package_id(cpu) == pkg_id) 660 continue; 661 662 pkg_id = topology_logical_package_id(cpu); 663 664 err = rdmsrq_on_cpu(cpu, MSR_IA32_MCU_STAGING_MBOX_ADDR, &mmio_pa); 665 if (WARN_ON_ONCE(err)) 666 return; 667 668 err = do_stage(mmio_pa); 669 if (err) { 670 pr_err("Error: staging failed (%d) for CPU%d at package %u.\n", 671 err, cpu, pkg_id); 672 return; 673 } 674 } 675 676 pr_info("Staging of patch revision 0x%x succeeded.\n", ucode_patch_late->hdr.rev); 677 } 678 679 static enum ucode_state __apply_microcode(struct ucode_cpu_info *uci, 680 struct microcode_intel *mc, 681 u32 *cur_rev) 682 { 683 u32 rev; 684 685 if (!mc) 686 return UCODE_NFOUND; 687 688 /* 689 * Save us the MSR write below - which is a particular expensive 690 * operation - when the other hyperthread has updated the microcode 691 * already. 692 */ 693 *cur_rev = intel_get_microcode_revision(); 694 if (*cur_rev >= mc->hdr.rev) { 695 uci->cpu_sig.rev = *cur_rev; 696 return UCODE_OK; 697 } 698 699 /* write microcode via MSR 0x79 */ 700 native_wrmsrq(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits); 701 702 rev = intel_get_microcode_revision(); 703 if (rev != mc->hdr.rev) 704 return UCODE_ERROR; 705 706 uci->cpu_sig.rev = rev; 707 return UCODE_UPDATED; 708 } 709 710 static enum ucode_state apply_microcode_early(struct ucode_cpu_info *uci) 711 { 712 struct microcode_intel *mc = uci->mc; 713 u32 cur_rev; 714 715 return __apply_microcode(uci, mc, &cur_rev); 716 } 717 718 static __init bool load_builtin_intel_microcode(struct cpio_data *cp) 719 { 720 unsigned int eax = 1, ebx, ecx = 0, edx; 721 struct firmware fw; 722 char name[30]; 723 724 if (IS_ENABLED(CONFIG_X86_32)) 725 return false; 726 727 native_cpuid(&eax, &ebx, &ecx, &edx); 728 729 sprintf(name, "intel-ucode/%02x-%02x-%02x", 730 x86_family(eax), x86_model(eax), x86_stepping(eax)); 731 732 if (firmware_request_builtin(&fw, name)) { 733 cp->size = fw.size; 734 cp->data = (void *)fw.data; 735 return true; 736 } 737 return false; 738 } 739 740 static __init struct microcode_intel *get_microcode_blob(struct ucode_cpu_info *uci, bool save) 741 { 742 struct cpio_data cp; 743 744 intel_collect_cpu_info(&uci->cpu_sig); 745 746 if (!load_builtin_intel_microcode(&cp)) 747 cp = find_microcode_in_initrd(ucode_path); 748 749 if (!(cp.data && cp.size)) 750 return NULL; 751 752 return scan_microcode(cp.data, cp.size, uci, save); 753 } 754 755 /* 756 * Invoked from an early init call to save the microcode blob which was 757 * selected during early boot when mm was not usable. The microcode must be 758 * saved because initrd is going away. It's an early init call so the APs 759 * just can use the pointer and do not have to scan initrd/builtin firmware 760 * again. 761 */ 762 static int __init save_builtin_microcode(void) 763 { 764 struct ucode_cpu_info uci; 765 766 if (xchg(&ucode_patch_va, NULL) != UCODE_BSP_LOADED) 767 return 0; 768 769 if (microcode_loader_disabled() || boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) 770 return 0; 771 772 uci.mc = get_microcode_blob(&uci, true); 773 if (uci.mc) 774 save_microcode_patch(uci.mc); 775 return 0; 776 } 777 early_initcall(save_builtin_microcode); 778 779 /* Load microcode on BSP from initrd or builtin blobs */ 780 void __init load_ucode_intel_bsp(struct early_load_data *ed) 781 { 782 struct ucode_cpu_info uci; 783 784 uci.mc = get_microcode_blob(&uci, false); 785 ed->old_rev = uci.cpu_sig.rev; 786 787 if (uci.mc && apply_microcode_early(&uci) == UCODE_UPDATED) { 788 ucode_patch_va = UCODE_BSP_LOADED; 789 ed->new_rev = uci.cpu_sig.rev; 790 } 791 } 792 793 void load_ucode_intel_ap(void) 794 { 795 struct ucode_cpu_info uci; 796 797 uci.mc = ucode_patch_va; 798 if (uci.mc) 799 apply_microcode_early(&uci); 800 } 801 802 /* Reload microcode on resume */ 803 void reload_ucode_intel(void) 804 { 805 struct ucode_cpu_info uci = { .mc = ucode_patch_va, }; 806 807 if (uci.mc) 808 apply_microcode_early(&uci); 809 } 810 811 static int collect_cpu_info(int cpu_num, struct cpu_signature *csig) 812 { 813 intel_collect_cpu_info(csig); 814 return 0; 815 } 816 817 static enum ucode_state apply_microcode_late(int cpu) 818 { 819 struct ucode_cpu_info *uci = ucode_cpu_info + cpu; 820 struct microcode_intel *mc = ucode_patch_late; 821 enum ucode_state ret; 822 u32 cur_rev; 823 824 if (WARN_ON_ONCE(smp_processor_id() != cpu)) 825 return UCODE_ERROR; 826 827 ret = __apply_microcode(uci, mc, &cur_rev); 828 if (ret != UCODE_UPDATED && ret != UCODE_OK) 829 return ret; 830 831 cpu_data(cpu).microcode = uci->cpu_sig.rev; 832 if (!cpu) 833 boot_cpu_data.microcode = uci->cpu_sig.rev; 834 835 return ret; 836 } 837 838 static bool ucode_validate_minrev(struct microcode_header_intel *mc_header) 839 { 840 int cur_rev = boot_cpu_data.microcode; 841 842 /* 843 * When late-loading, ensure the header declares a minimum revision 844 * required to perform a late-load. The previously reserved field 845 * is 0 in older microcode blobs. 846 */ 847 if (!mc_header->min_req_ver) { 848 pr_info("Unsafe microcode update: Microcode header does not specify a required min version\n"); 849 return false; 850 } 851 852 /* 853 * Check whether the current revision is either greater or equal to 854 * to the minimum revision specified in the header. 855 */ 856 if (cur_rev < mc_header->min_req_ver) { 857 pr_info("Unsafe microcode update: Current revision 0x%x too old\n", cur_rev); 858 pr_info("Current should be at 0x%x or higher. Use early loading instead\n", mc_header->min_req_ver); 859 return false; 860 } 861 return true; 862 } 863 864 static enum ucode_state parse_microcode_blobs(int cpu, struct iov_iter *iter) 865 { 866 struct ucode_cpu_info *uci = ucode_cpu_info + cpu; 867 bool is_safe, new_is_safe = false; 868 int cur_rev = uci->cpu_sig.rev; 869 unsigned int curr_mc_size = 0; 870 u8 *new_mc = NULL, *mc = NULL; 871 872 while (iov_iter_count(iter)) { 873 struct microcode_header_intel mc_header; 874 unsigned int mc_size, data_size; 875 u8 *data; 876 877 if (!copy_from_iter_full(&mc_header, sizeof(mc_header), iter)) { 878 pr_err("error! Truncated or inaccessible header in microcode data file\n"); 879 goto fail; 880 } 881 882 mc_size = get_totalsize(&mc_header); 883 if (mc_size < sizeof(mc_header)) { 884 pr_err("error! Bad data in microcode data file (totalsize too small)\n"); 885 goto fail; 886 } 887 data_size = mc_size - sizeof(mc_header); 888 if (data_size > iov_iter_count(iter)) { 889 pr_err("error! Bad data in microcode data file (truncated file?)\n"); 890 goto fail; 891 } 892 893 /* For performance reasons, reuse mc area when possible */ 894 if (!mc || mc_size > curr_mc_size) { 895 kvfree(mc); 896 mc = kvmalloc(mc_size, GFP_KERNEL); 897 if (!mc) 898 goto fail; 899 curr_mc_size = mc_size; 900 } 901 902 memcpy(mc, &mc_header, sizeof(mc_header)); 903 data = mc + sizeof(mc_header); 904 if (!copy_from_iter_full(data, data_size, iter) || 905 intel_microcode_sanity_check(mc, true, MC_HEADER_TYPE_MICROCODE) < 0) 906 goto fail; 907 908 if (cur_rev >= mc_header.rev) 909 continue; 910 911 if (!intel_find_matching_signature(mc, &uci->cpu_sig)) 912 continue; 913 914 is_safe = ucode_validate_minrev(&mc_header); 915 if (force_minrev && !is_safe) 916 continue; 917 918 kvfree(new_mc); 919 cur_rev = mc_header.rev; 920 new_mc = mc; 921 new_is_safe = is_safe; 922 mc = NULL; 923 } 924 925 if (iov_iter_count(iter)) 926 goto fail; 927 928 kvfree(mc); 929 if (!new_mc) 930 return UCODE_NFOUND; 931 932 ucode_patch_late = (struct microcode_intel *)new_mc; 933 return new_is_safe ? UCODE_NEW_SAFE : UCODE_NEW; 934 935 fail: 936 kvfree(mc); 937 kvfree(new_mc); 938 return UCODE_ERROR; 939 } 940 941 static bool is_blacklisted(unsigned int cpu) 942 { 943 struct cpuinfo_x86 *c = &cpu_data(cpu); 944 945 /* 946 * Late loading on model 79 with microcode revision less than 0x0b000021 947 * and LLC size per core bigger than 2.5MB may result in a system hang. 948 * This behavior is documented in item BDX90, #334165 (Intel Xeon 949 * Processor E7-8800/4800 v4 Product Family). 950 */ 951 if (c->x86_vfm == INTEL_BROADWELL_X && 952 c->x86_stepping == 0x01 && 953 llc_size_per_core > 2621440 && 954 c->microcode < 0x0b000021) { 955 pr_err_once("Erratum BDX90: late loading with revision < 0x0b000021 (0x%x) disabled.\n", c->microcode); 956 pr_err_once("Please consider either early loading through initrd/built-in or a potential BIOS update.\n"); 957 return true; 958 } 959 960 return false; 961 } 962 963 static enum ucode_state request_microcode_fw(int cpu, struct device *device) 964 { 965 struct cpuinfo_x86 *c = &cpu_data(cpu); 966 const struct firmware *firmware; 967 struct iov_iter iter; 968 enum ucode_state ret; 969 struct kvec kvec; 970 char name[30]; 971 972 if (is_blacklisted(cpu)) 973 return UCODE_NFOUND; 974 975 sprintf(name, "intel-ucode/%02x-%02x-%02x", 976 c->x86, c->x86_model, c->x86_stepping); 977 978 if (request_firmware_direct(&firmware, name, device)) { 979 pr_debug("data file %s load failed\n", name); 980 return UCODE_NFOUND; 981 } 982 983 kvec.iov_base = (void *)firmware->data; 984 kvec.iov_len = firmware->size; 985 iov_iter_kvec(&iter, ITER_SOURCE, &kvec, 1, firmware->size); 986 ret = parse_microcode_blobs(cpu, &iter); 987 988 release_firmware(firmware); 989 990 return ret; 991 } 992 993 static void finalize_late_load(int result) 994 { 995 if (!result) 996 update_ucode_pointer(ucode_patch_late); 997 else 998 kvfree(ucode_patch_late); 999 ucode_patch_late = NULL; 1000 } 1001 1002 static struct microcode_ops microcode_intel_ops = { 1003 .request_microcode_fw = request_microcode_fw, 1004 .collect_cpu_info = collect_cpu_info, 1005 .apply_microcode = apply_microcode_late, 1006 .finalize_late_load = finalize_late_load, 1007 .stage_microcode = stage_microcode, 1008 .use_nmi = IS_ENABLED(CONFIG_X86_64), 1009 }; 1010 1011 static __init void calc_llc_size_per_core(struct cpuinfo_x86 *c) 1012 { 1013 u64 llc_size = c->x86_cache_size * 1024ULL; 1014 1015 do_div(llc_size, topology_num_cores_per_package()); 1016 llc_size_per_core = (unsigned int)llc_size; 1017 } 1018 1019 static __init bool staging_available(void) 1020 { 1021 u64 val; 1022 1023 val = x86_read_arch_cap_msr(); 1024 if (!(val & ARCH_CAP_MCU_ENUM)) 1025 return false; 1026 1027 rdmsrq(MSR_IA32_MCU_ENUMERATION, val); 1028 return !!(val & MCU_STAGING); 1029 } 1030 1031 struct microcode_ops * __init init_intel_microcode(void) 1032 { 1033 struct cpuinfo_x86 *c = &boot_cpu_data; 1034 1035 if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 || 1036 cpu_has(c, X86_FEATURE_IA64)) { 1037 pr_err("Intel CPU family 0x%x not supported\n", c->x86); 1038 return NULL; 1039 } 1040 1041 if (staging_available()) { 1042 microcode_intel_ops.use_staging = true; 1043 pr_info("Enabled staging feature.\n"); 1044 } 1045 1046 calc_llc_size_per_core(c); 1047 1048 return µcode_intel_ops; 1049 } 1050