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