1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * AMD CPU Microcode Update Driver for Linux 4 * 5 * This driver allows to upgrade microcode on F10h AMD 6 * CPUs and later. 7 * 8 * Copyright (C) 2008-2011 Advanced Micro Devices Inc. 9 * 2013-2018 Borislav Petkov <[email protected]> 10 * 11 * Author: Peter Oruba <[email protected]> 12 * 13 * Based on work by: 14 * Tigran Aivazian <[email protected]> 15 * 16 * early loader: 17 * Copyright (C) 2013 Advanced Micro Devices, Inc. 18 * 19 * Author: Jacob Shin <[email protected]> 20 * Fixes: Borislav Petkov <[email protected]> 21 */ 22 #define pr_fmt(fmt) "microcode: " fmt 23 24 #include <linux/earlycpio.h> 25 #include <linux/firmware.h> 26 #include <linux/uaccess.h> 27 #include <linux/vmalloc.h> 28 #include <linux/initrd.h> 29 #include <linux/kernel.h> 30 #include <linux/pci.h> 31 32 #include <asm/microcode.h> 33 #include <asm/processor.h> 34 #include <asm/setup.h> 35 #include <asm/cpu.h> 36 #include <asm/msr.h> 37 #include <asm/tlb.h> 38 39 #include "internal.h" 40 41 struct ucode_patch { 42 struct list_head plist; 43 void *data; 44 unsigned int size; 45 u32 patch_id; 46 u16 equiv_cpu; 47 }; 48 49 static LIST_HEAD(microcode_cache); 50 51 #define UCODE_MAGIC 0x00414d44 52 #define UCODE_EQUIV_CPU_TABLE_TYPE 0x00000000 53 #define UCODE_UCODE_TYPE 0x00000001 54 55 #define SECTION_HDR_SIZE 8 56 #define CONTAINER_HDR_SZ 12 57 58 struct equiv_cpu_entry { 59 u32 installed_cpu; 60 u32 fixed_errata_mask; 61 u32 fixed_errata_compare; 62 u16 equiv_cpu; 63 u16 res; 64 } __packed; 65 66 struct microcode_header_amd { 67 u32 data_code; 68 u32 patch_id; 69 u16 mc_patch_data_id; 70 u8 mc_patch_data_len; 71 u8 init_flag; 72 u32 mc_patch_data_checksum; 73 u32 nb_dev_id; 74 u32 sb_dev_id; 75 u16 processor_rev_id; 76 u8 nb_rev_id; 77 u8 sb_rev_id; 78 u8 bios_api_rev; 79 u8 reserved1[3]; 80 u32 match_reg[8]; 81 } __packed; 82 83 struct microcode_amd { 84 struct microcode_header_amd hdr; 85 unsigned int mpb[]; 86 }; 87 88 static struct equiv_cpu_table { 89 unsigned int num_entries; 90 struct equiv_cpu_entry *entry; 91 } equiv_table; 92 93 union zen_patch_rev { 94 struct { 95 __u32 rev : 8, 96 stepping : 4, 97 model : 4, 98 __reserved : 4, 99 ext_model : 4, 100 ext_fam : 8; 101 }; 102 __u32 ucode_rev; 103 }; 104 105 union cpuid_1_eax { 106 struct { 107 __u32 stepping : 4, 108 model : 4, 109 family : 4, 110 __reserved0 : 4, 111 ext_model : 4, 112 ext_fam : 8, 113 __reserved1 : 4; 114 }; 115 __u32 full; 116 }; 117 118 /* 119 * This points to the current valid container of microcode patches which we will 120 * save from the initrd/builtin before jettisoning its contents. @mc is the 121 * microcode patch we found to match. 122 */ 123 struct cont_desc { 124 struct microcode_amd *mc; 125 u32 psize; 126 u8 *data; 127 size_t size; 128 }; 129 130 /* 131 * Microcode patch container file is prepended to the initrd in cpio 132 * format. See Documentation/arch/x86/microcode.rst 133 */ 134 static const char 135 ucode_path[] __maybe_unused = "kernel/x86/microcode/AuthenticAMD.bin"; 136 137 /* 138 * This is CPUID(1).EAX on the BSP. It is used in two ways: 139 * 140 * 1. To ignore the equivalence table on Zen1 and newer. 141 * 142 * 2. To match which patches to load because the patch revision ID 143 * already contains the f/m/s for which the microcode is destined 144 * for. 145 */ 146 static u32 bsp_cpuid_1_eax __ro_after_init; 147 148 static union cpuid_1_eax ucode_rev_to_cpuid(unsigned int val) 149 { 150 union zen_patch_rev p; 151 union cpuid_1_eax c; 152 153 p.ucode_rev = val; 154 c.full = 0; 155 156 c.stepping = p.stepping; 157 c.model = p.model; 158 c.ext_model = p.ext_model; 159 c.family = 0xf; 160 c.ext_fam = p.ext_fam; 161 162 return c; 163 } 164 165 static u16 find_equiv_id(struct equiv_cpu_table *et, u32 sig) 166 { 167 unsigned int i; 168 169 /* Zen and newer do not need an equivalence table. */ 170 if (x86_family(bsp_cpuid_1_eax) >= 0x17) 171 return 0; 172 173 if (!et || !et->num_entries) 174 return 0; 175 176 for (i = 0; i < et->num_entries; i++) { 177 struct equiv_cpu_entry *e = &et->entry[i]; 178 179 if (sig == e->installed_cpu) 180 return e->equiv_cpu; 181 } 182 return 0; 183 } 184 185 /* 186 * Check whether there is a valid microcode container file at the beginning 187 * of @buf of size @buf_size. 188 */ 189 static bool verify_container(const u8 *buf, size_t buf_size) 190 { 191 u32 cont_magic; 192 193 if (buf_size <= CONTAINER_HDR_SZ) { 194 pr_debug("Truncated microcode container header.\n"); 195 return false; 196 } 197 198 cont_magic = *(const u32 *)buf; 199 if (cont_magic != UCODE_MAGIC) { 200 pr_debug("Invalid magic value (0x%08x).\n", cont_magic); 201 return false; 202 } 203 204 return true; 205 } 206 207 /* 208 * Check whether there is a valid, non-truncated CPU equivalence table at the 209 * beginning of @buf of size @buf_size. 210 */ 211 static bool verify_equivalence_table(const u8 *buf, size_t buf_size) 212 { 213 const u32 *hdr = (const u32 *)buf; 214 u32 cont_type, equiv_tbl_len; 215 216 if (!verify_container(buf, buf_size)) 217 return false; 218 219 /* Zen and newer do not need an equivalence table. */ 220 if (x86_family(bsp_cpuid_1_eax) >= 0x17) 221 return true; 222 223 cont_type = hdr[1]; 224 if (cont_type != UCODE_EQUIV_CPU_TABLE_TYPE) { 225 pr_debug("Wrong microcode container equivalence table type: %u.\n", 226 cont_type); 227 return false; 228 } 229 230 buf_size -= CONTAINER_HDR_SZ; 231 232 equiv_tbl_len = hdr[2]; 233 if (equiv_tbl_len < sizeof(struct equiv_cpu_entry) || 234 buf_size < equiv_tbl_len) { 235 pr_debug("Truncated equivalence table.\n"); 236 return false; 237 } 238 239 return true; 240 } 241 242 /* 243 * Check whether there is a valid, non-truncated microcode patch section at the 244 * beginning of @buf of size @buf_size. 245 * 246 * On success, @sh_psize returns the patch size according to the section header, 247 * to the caller. 248 */ 249 static bool 250 __verify_patch_section(const u8 *buf, size_t buf_size, u32 *sh_psize) 251 { 252 u32 p_type, p_size; 253 const u32 *hdr; 254 255 if (buf_size < SECTION_HDR_SIZE) { 256 pr_debug("Truncated patch section.\n"); 257 return false; 258 } 259 260 hdr = (const u32 *)buf; 261 p_type = hdr[0]; 262 p_size = hdr[1]; 263 264 if (p_type != UCODE_UCODE_TYPE) { 265 pr_debug("Invalid type field (0x%x) in container file section header.\n", 266 p_type); 267 return false; 268 } 269 270 if (p_size < sizeof(struct microcode_header_amd)) { 271 pr_debug("Patch of size %u too short.\n", p_size); 272 return false; 273 } 274 275 *sh_psize = p_size; 276 277 return true; 278 } 279 280 /* 281 * Check whether the passed remaining file @buf_size is large enough to contain 282 * a patch of the indicated @sh_psize (and also whether this size does not 283 * exceed the per-family maximum). @sh_psize is the size read from the section 284 * header. 285 */ 286 static bool __verify_patch_size(u32 sh_psize, size_t buf_size) 287 { 288 u8 family = x86_family(bsp_cpuid_1_eax); 289 u32 max_size; 290 291 if (family >= 0x15) 292 goto ret; 293 294 #define F1XH_MPB_MAX_SIZE 2048 295 #define F14H_MPB_MAX_SIZE 1824 296 297 switch (family) { 298 case 0x10 ... 0x12: 299 max_size = F1XH_MPB_MAX_SIZE; 300 break; 301 case 0x14: 302 max_size = F14H_MPB_MAX_SIZE; 303 break; 304 default: 305 WARN(1, "%s: WTF family: 0x%x\n", __func__, family); 306 return false; 307 } 308 309 if (sh_psize > max_size) 310 return false; 311 312 ret: 313 /* Working with the whole buffer so < is ok. */ 314 return sh_psize <= buf_size; 315 } 316 317 /* 318 * Verify the patch in @buf. 319 * 320 * Returns: 321 * negative: on error 322 * positive: patch is not for this family, skip it 323 * 0: success 324 */ 325 static int verify_patch(const u8 *buf, size_t buf_size, u32 *patch_size) 326 { 327 u8 family = x86_family(bsp_cpuid_1_eax); 328 struct microcode_header_amd *mc_hdr; 329 u32 sh_psize; 330 u16 proc_id; 331 u8 patch_fam; 332 333 if (!__verify_patch_section(buf, buf_size, &sh_psize)) 334 return -1; 335 336 /* 337 * The section header length is not included in this indicated size 338 * but is present in the leftover file length so we need to subtract 339 * it before passing this value to the function below. 340 */ 341 buf_size -= SECTION_HDR_SIZE; 342 343 /* 344 * Check if the remaining buffer is big enough to contain a patch of 345 * size sh_psize, as the section claims. 346 */ 347 if (buf_size < sh_psize) { 348 pr_debug("Patch of size %u truncated.\n", sh_psize); 349 return -1; 350 } 351 352 if (!__verify_patch_size(sh_psize, buf_size)) { 353 pr_debug("Per-family patch size mismatch.\n"); 354 return -1; 355 } 356 357 *patch_size = sh_psize; 358 359 mc_hdr = (struct microcode_header_amd *)(buf + SECTION_HDR_SIZE); 360 if (mc_hdr->nb_dev_id || mc_hdr->sb_dev_id) { 361 pr_err("Patch-ID 0x%08x: chipset-specific code unsupported.\n", mc_hdr->patch_id); 362 return -1; 363 } 364 365 proc_id = mc_hdr->processor_rev_id; 366 patch_fam = 0xf + (proc_id >> 12); 367 if (patch_fam != family) 368 return 1; 369 370 return 0; 371 } 372 373 static bool mc_patch_matches(struct microcode_amd *mc, u16 eq_id) 374 { 375 /* Zen and newer do not need an equivalence table. */ 376 if (x86_family(bsp_cpuid_1_eax) >= 0x17) 377 return ucode_rev_to_cpuid(mc->hdr.patch_id).full == bsp_cpuid_1_eax; 378 else 379 return eq_id == mc->hdr.processor_rev_id; 380 } 381 382 /* 383 * This scans the ucode blob for the proper container as we can have multiple 384 * containers glued together. 385 * 386 * Returns the amount of bytes consumed while scanning. @desc contains all the 387 * data we're going to use in later stages of the application. 388 */ 389 static size_t parse_container(u8 *ucode, size_t size, struct cont_desc *desc) 390 { 391 struct equiv_cpu_table table; 392 size_t orig_size = size; 393 u32 *hdr = (u32 *)ucode; 394 u16 eq_id; 395 u8 *buf; 396 397 if (!verify_equivalence_table(ucode, size)) 398 return 0; 399 400 buf = ucode; 401 402 table.entry = (struct equiv_cpu_entry *)(buf + CONTAINER_HDR_SZ); 403 table.num_entries = hdr[2] / sizeof(struct equiv_cpu_entry); 404 405 /* 406 * Find the equivalence ID of our CPU in this table. Even if this table 407 * doesn't contain a patch for the CPU, scan through the whole container 408 * so that it can be skipped in case there are other containers appended. 409 */ 410 eq_id = find_equiv_id(&table, bsp_cpuid_1_eax); 411 412 buf += hdr[2] + CONTAINER_HDR_SZ; 413 size -= hdr[2] + CONTAINER_HDR_SZ; 414 415 /* 416 * Scan through the rest of the container to find where it ends. We do 417 * some basic sanity-checking too. 418 */ 419 while (size > 0) { 420 struct microcode_amd *mc; 421 u32 patch_size; 422 int ret; 423 424 ret = verify_patch(buf, size, &patch_size); 425 if (ret < 0) { 426 /* 427 * Patch verification failed, skip to the next container, if 428 * there is one. Before exit, check whether that container has 429 * found a patch already. If so, use it. 430 */ 431 goto out; 432 } else if (ret > 0) { 433 goto skip; 434 } 435 436 mc = (struct microcode_amd *)(buf + SECTION_HDR_SIZE); 437 if (mc_patch_matches(mc, eq_id)) { 438 desc->psize = patch_size; 439 desc->mc = mc; 440 } 441 442 skip: 443 /* Skip patch section header too: */ 444 buf += patch_size + SECTION_HDR_SIZE; 445 size -= patch_size + SECTION_HDR_SIZE; 446 } 447 448 out: 449 /* 450 * If we have found a patch (desc->mc), it means we're looking at the 451 * container which has a patch for this CPU so return 0 to mean, @ucode 452 * already points to the proper container. Otherwise, we return the size 453 * we scanned so that we can advance to the next container in the 454 * buffer. 455 */ 456 if (desc->mc) { 457 desc->data = ucode; 458 desc->size = orig_size - size; 459 460 return 0; 461 } 462 463 return orig_size - size; 464 } 465 466 /* 467 * Scan the ucode blob for the proper container as we can have multiple 468 * containers glued together. 469 */ 470 static void scan_containers(u8 *ucode, size_t size, struct cont_desc *desc) 471 { 472 while (size) { 473 size_t s = parse_container(ucode, size, desc); 474 if (!s) 475 return; 476 477 /* catch wraparound */ 478 if (size >= s) { 479 ucode += s; 480 size -= s; 481 } else { 482 return; 483 } 484 } 485 } 486 487 static int __apply_microcode_amd(struct microcode_amd *mc, unsigned int psize) 488 { 489 unsigned long p_addr = (unsigned long)&mc->hdr.data_code; 490 u32 rev, dummy; 491 492 native_wrmsrl(MSR_AMD64_PATCH_LOADER, p_addr); 493 494 if (x86_family(bsp_cpuid_1_eax) == 0x17) { 495 unsigned long p_addr_end = p_addr + psize - 1; 496 497 invlpg(p_addr); 498 499 /* 500 * Flush next page too if patch image is crossing a page 501 * boundary. 502 */ 503 if (p_addr >> PAGE_SHIFT != p_addr_end >> PAGE_SHIFT) 504 invlpg(p_addr_end); 505 } 506 507 /* verify patch application was successful */ 508 native_rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy); 509 510 if (rev != mc->hdr.patch_id) 511 return -1; 512 513 return 0; 514 } 515 516 /* 517 * Early load occurs before we can vmalloc(). So we look for the microcode 518 * patch container file in initrd, traverse equivalent cpu table, look for a 519 * matching microcode patch, and update, all in initrd memory in place. 520 * When vmalloc() is available for use later -- on 64-bit during first AP load, 521 * and on 32-bit during save_microcode_in_initrd_amd() -- we can call 522 * load_microcode_amd() to save equivalent cpu table and microcode patches in 523 * kernel heap memory. 524 * 525 * Returns true if container found (sets @desc), false otherwise. 526 */ 527 static bool early_apply_microcode(u32 old_rev, void *ucode, size_t size) 528 { 529 struct cont_desc desc = { 0 }; 530 struct microcode_amd *mc; 531 bool ret = false; 532 533 scan_containers(ucode, size, &desc); 534 535 mc = desc.mc; 536 if (!mc) 537 return ret; 538 539 /* 540 * Allow application of the same revision to pick up SMT-specific 541 * changes even if the revision of the other SMT thread is already 542 * up-to-date. 543 */ 544 if (old_rev > mc->hdr.patch_id) 545 return ret; 546 547 return !__apply_microcode_amd(mc, desc.psize); 548 } 549 550 static bool get_builtin_microcode(struct cpio_data *cp) 551 { 552 char fw_name[36] = "amd-ucode/microcode_amd.bin"; 553 u8 family = x86_family(bsp_cpuid_1_eax); 554 struct firmware fw; 555 556 if (IS_ENABLED(CONFIG_X86_32)) 557 return false; 558 559 if (family >= 0x15) 560 snprintf(fw_name, sizeof(fw_name), 561 "amd-ucode/microcode_amd_fam%02hhxh.bin", family); 562 563 if (firmware_request_builtin(&fw, fw_name)) { 564 cp->size = fw.size; 565 cp->data = (void *)fw.data; 566 return true; 567 } 568 569 return false; 570 } 571 572 static bool __init find_blobs_in_containers(struct cpio_data *ret) 573 { 574 struct cpio_data cp; 575 bool found; 576 577 if (!get_builtin_microcode(&cp)) 578 cp = find_microcode_in_initrd(ucode_path); 579 580 found = cp.data && cp.size; 581 if (found) 582 *ret = cp; 583 584 return found; 585 } 586 587 void __init load_ucode_amd_bsp(struct early_load_data *ed, unsigned int cpuid_1_eax) 588 { 589 struct cpio_data cp = { }; 590 u32 dummy; 591 592 bsp_cpuid_1_eax = cpuid_1_eax; 593 594 native_rdmsr(MSR_AMD64_PATCH_LEVEL, ed->old_rev, dummy); 595 596 /* Needed in load_microcode_amd() */ 597 ucode_cpu_info[0].cpu_sig.sig = cpuid_1_eax; 598 599 if (!find_blobs_in_containers(&cp)) 600 return; 601 602 if (early_apply_microcode(ed->old_rev, cp.data, cp.size)) 603 native_rdmsr(MSR_AMD64_PATCH_LEVEL, ed->new_rev, dummy); 604 } 605 606 static enum ucode_state _load_microcode_amd(u8 family, const u8 *data, size_t size); 607 608 static int __init save_microcode_in_initrd(void) 609 { 610 unsigned int cpuid_1_eax = native_cpuid_eax(1); 611 struct cpuinfo_x86 *c = &boot_cpu_data; 612 struct cont_desc desc = { 0 }; 613 enum ucode_state ret; 614 struct cpio_data cp; 615 616 if (dis_ucode_ldr || c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) 617 return 0; 618 619 if (!find_blobs_in_containers(&cp)) 620 return -EINVAL; 621 622 scan_containers(cp.data, cp.size, &desc); 623 if (!desc.mc) 624 return -EINVAL; 625 626 ret = _load_microcode_amd(x86_family(cpuid_1_eax), desc.data, desc.size); 627 if (ret > UCODE_UPDATED) 628 return -EINVAL; 629 630 return 0; 631 } 632 early_initcall(save_microcode_in_initrd); 633 634 static inline bool patch_cpus_equivalent(struct ucode_patch *p, 635 struct ucode_patch *n, 636 bool ignore_stepping) 637 { 638 /* Zen and newer hardcode the f/m/s in the patch ID */ 639 if (x86_family(bsp_cpuid_1_eax) >= 0x17) { 640 union cpuid_1_eax p_cid = ucode_rev_to_cpuid(p->patch_id); 641 union cpuid_1_eax n_cid = ucode_rev_to_cpuid(n->patch_id); 642 643 if (ignore_stepping) { 644 p_cid.stepping = 0; 645 n_cid.stepping = 0; 646 } 647 648 return p_cid.full == n_cid.full; 649 } else { 650 return p->equiv_cpu == n->equiv_cpu; 651 } 652 } 653 654 /* 655 * a small, trivial cache of per-family ucode patches 656 */ 657 static struct ucode_patch *cache_find_patch(struct ucode_cpu_info *uci, u16 equiv_cpu) 658 { 659 struct ucode_patch *p; 660 struct ucode_patch n; 661 662 n.equiv_cpu = equiv_cpu; 663 n.patch_id = uci->cpu_sig.rev; 664 665 WARN_ON_ONCE(!n.patch_id); 666 667 list_for_each_entry(p, µcode_cache, plist) 668 if (patch_cpus_equivalent(p, &n, false)) 669 return p; 670 671 return NULL; 672 } 673 674 static inline int patch_newer(struct ucode_patch *p, struct ucode_patch *n) 675 { 676 /* Zen and newer hardcode the f/m/s in the patch ID */ 677 if (x86_family(bsp_cpuid_1_eax) >= 0x17) { 678 union zen_patch_rev zp, zn; 679 680 zp.ucode_rev = p->patch_id; 681 zn.ucode_rev = n->patch_id; 682 683 if (zn.stepping != zp.stepping) 684 return -1; 685 686 return zn.rev > zp.rev; 687 } else { 688 return n->patch_id > p->patch_id; 689 } 690 } 691 692 static void update_cache(struct ucode_patch *new_patch) 693 { 694 struct ucode_patch *p; 695 int ret; 696 697 list_for_each_entry(p, µcode_cache, plist) { 698 if (patch_cpus_equivalent(p, new_patch, true)) { 699 ret = patch_newer(p, new_patch); 700 if (ret < 0) 701 continue; 702 else if (!ret) { 703 /* we already have the latest patch */ 704 kfree(new_patch->data); 705 kfree(new_patch); 706 return; 707 } 708 709 list_replace(&p->plist, &new_patch->plist); 710 kfree(p->data); 711 kfree(p); 712 return; 713 } 714 } 715 /* no patch found, add it */ 716 list_add_tail(&new_patch->plist, µcode_cache); 717 } 718 719 static void free_cache(void) 720 { 721 struct ucode_patch *p, *tmp; 722 723 list_for_each_entry_safe(p, tmp, µcode_cache, plist) { 724 __list_del(p->plist.prev, p->plist.next); 725 kfree(p->data); 726 kfree(p); 727 } 728 } 729 730 static struct ucode_patch *find_patch(unsigned int cpu) 731 { 732 struct ucode_cpu_info *uci = ucode_cpu_info + cpu; 733 u32 rev, dummy __always_unused; 734 u16 equiv_id = 0; 735 736 /* fetch rev if not populated yet: */ 737 if (!uci->cpu_sig.rev) { 738 rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy); 739 uci->cpu_sig.rev = rev; 740 } 741 742 if (x86_family(bsp_cpuid_1_eax) < 0x17) { 743 equiv_id = find_equiv_id(&equiv_table, uci->cpu_sig.sig); 744 if (!equiv_id) 745 return NULL; 746 } 747 748 return cache_find_patch(uci, equiv_id); 749 } 750 751 void reload_ucode_amd(unsigned int cpu) 752 { 753 u32 rev, dummy __always_unused; 754 struct microcode_amd *mc; 755 struct ucode_patch *p; 756 757 p = find_patch(cpu); 758 if (!p) 759 return; 760 761 mc = p->data; 762 763 rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy); 764 765 if (rev < mc->hdr.patch_id) { 766 if (!__apply_microcode_amd(mc, p->size)) 767 pr_info_once("reload revision: 0x%08x\n", mc->hdr.patch_id); 768 } 769 } 770 771 static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig) 772 { 773 struct cpuinfo_x86 *c = &cpu_data(cpu); 774 struct ucode_cpu_info *uci = ucode_cpu_info + cpu; 775 struct ucode_patch *p; 776 777 csig->sig = cpuid_eax(0x00000001); 778 csig->rev = c->microcode; 779 780 /* 781 * a patch could have been loaded early, set uci->mc so that 782 * mc_bp_resume() can call apply_microcode() 783 */ 784 p = find_patch(cpu); 785 if (p && (p->patch_id == csig->rev)) 786 uci->mc = p->data; 787 788 return 0; 789 } 790 791 static enum ucode_state apply_microcode_amd(int cpu) 792 { 793 struct cpuinfo_x86 *c = &cpu_data(cpu); 794 struct microcode_amd *mc_amd; 795 struct ucode_cpu_info *uci; 796 struct ucode_patch *p; 797 enum ucode_state ret; 798 u32 rev; 799 800 BUG_ON(raw_smp_processor_id() != cpu); 801 802 uci = ucode_cpu_info + cpu; 803 804 p = find_patch(cpu); 805 if (!p) 806 return UCODE_NFOUND; 807 808 rev = uci->cpu_sig.rev; 809 810 mc_amd = p->data; 811 uci->mc = p->data; 812 813 /* need to apply patch? */ 814 if (rev > mc_amd->hdr.patch_id) { 815 ret = UCODE_OK; 816 goto out; 817 } 818 819 if (__apply_microcode_amd(mc_amd, p->size)) { 820 pr_err("CPU%d: update failed for patch_level=0x%08x\n", 821 cpu, mc_amd->hdr.patch_id); 822 return UCODE_ERROR; 823 } 824 825 rev = mc_amd->hdr.patch_id; 826 ret = UCODE_UPDATED; 827 828 out: 829 uci->cpu_sig.rev = rev; 830 c->microcode = rev; 831 832 /* Update boot_cpu_data's revision too, if we're on the BSP: */ 833 if (c->cpu_index == boot_cpu_data.cpu_index) 834 boot_cpu_data.microcode = rev; 835 836 return ret; 837 } 838 839 void load_ucode_amd_ap(unsigned int cpuid_1_eax) 840 { 841 unsigned int cpu = smp_processor_id(); 842 843 ucode_cpu_info[cpu].cpu_sig.sig = cpuid_1_eax; 844 apply_microcode_amd(cpu); 845 } 846 847 static size_t install_equiv_cpu_table(const u8 *buf, size_t buf_size) 848 { 849 u32 equiv_tbl_len; 850 const u32 *hdr; 851 852 if (!verify_equivalence_table(buf, buf_size)) 853 return 0; 854 855 hdr = (const u32 *)buf; 856 equiv_tbl_len = hdr[2]; 857 858 /* Zen and newer do not need an equivalence table. */ 859 if (x86_family(bsp_cpuid_1_eax) >= 0x17) 860 goto out; 861 862 equiv_table.entry = vmalloc(equiv_tbl_len); 863 if (!equiv_table.entry) { 864 pr_err("failed to allocate equivalent CPU table\n"); 865 return 0; 866 } 867 868 memcpy(equiv_table.entry, buf + CONTAINER_HDR_SZ, equiv_tbl_len); 869 equiv_table.num_entries = equiv_tbl_len / sizeof(struct equiv_cpu_entry); 870 871 out: 872 /* add header length */ 873 return equiv_tbl_len + CONTAINER_HDR_SZ; 874 } 875 876 static void free_equiv_cpu_table(void) 877 { 878 if (x86_family(bsp_cpuid_1_eax) >= 0x17) 879 return; 880 881 vfree(equiv_table.entry); 882 memset(&equiv_table, 0, sizeof(equiv_table)); 883 } 884 885 static void cleanup(void) 886 { 887 free_equiv_cpu_table(); 888 free_cache(); 889 } 890 891 /* 892 * Return a non-negative value even if some of the checks failed so that 893 * we can skip over the next patch. If we return a negative value, we 894 * signal a grave error like a memory allocation has failed and the 895 * driver cannot continue functioning normally. In such cases, we tear 896 * down everything we've used up so far and exit. 897 */ 898 static int verify_and_add_patch(u8 family, u8 *fw, unsigned int leftover, 899 unsigned int *patch_size) 900 { 901 struct microcode_header_amd *mc_hdr; 902 struct ucode_patch *patch; 903 u16 proc_id; 904 int ret; 905 906 ret = verify_patch(fw, leftover, patch_size); 907 if (ret) 908 return ret; 909 910 patch = kzalloc(sizeof(*patch), GFP_KERNEL); 911 if (!patch) { 912 pr_err("Patch allocation failure.\n"); 913 return -EINVAL; 914 } 915 916 patch->data = kmemdup(fw + SECTION_HDR_SIZE, *patch_size, GFP_KERNEL); 917 if (!patch->data) { 918 pr_err("Patch data allocation failure.\n"); 919 kfree(patch); 920 return -EINVAL; 921 } 922 patch->size = *patch_size; 923 924 mc_hdr = (struct microcode_header_amd *)(fw + SECTION_HDR_SIZE); 925 proc_id = mc_hdr->processor_rev_id; 926 927 INIT_LIST_HEAD(&patch->plist); 928 patch->patch_id = mc_hdr->patch_id; 929 patch->equiv_cpu = proc_id; 930 931 pr_debug("%s: Adding patch_id: 0x%08x, proc_id: 0x%04x\n", 932 __func__, patch->patch_id, proc_id); 933 934 /* ... and add to cache. */ 935 update_cache(patch); 936 937 return 0; 938 } 939 940 /* Scan the blob in @data and add microcode patches to the cache. */ 941 static enum ucode_state __load_microcode_amd(u8 family, const u8 *data, 942 size_t size) 943 { 944 u8 *fw = (u8 *)data; 945 size_t offset; 946 947 offset = install_equiv_cpu_table(data, size); 948 if (!offset) 949 return UCODE_ERROR; 950 951 fw += offset; 952 size -= offset; 953 954 if (*(u32 *)fw != UCODE_UCODE_TYPE) { 955 pr_err("invalid type field in container file section header\n"); 956 free_equiv_cpu_table(); 957 return UCODE_ERROR; 958 } 959 960 while (size > 0) { 961 unsigned int crnt_size = 0; 962 int ret; 963 964 ret = verify_and_add_patch(family, fw, size, &crnt_size); 965 if (ret < 0) 966 return UCODE_ERROR; 967 968 fw += crnt_size + SECTION_HDR_SIZE; 969 size -= (crnt_size + SECTION_HDR_SIZE); 970 } 971 972 return UCODE_OK; 973 } 974 975 static enum ucode_state _load_microcode_amd(u8 family, const u8 *data, size_t size) 976 { 977 enum ucode_state ret; 978 979 /* free old equiv table */ 980 free_equiv_cpu_table(); 981 982 ret = __load_microcode_amd(family, data, size); 983 if (ret != UCODE_OK) 984 cleanup(); 985 986 return ret; 987 } 988 989 static enum ucode_state load_microcode_amd(u8 family, const u8 *data, size_t size) 990 { 991 struct cpuinfo_x86 *c; 992 unsigned int nid, cpu; 993 struct ucode_patch *p; 994 enum ucode_state ret; 995 996 ret = _load_microcode_amd(family, data, size); 997 if (ret != UCODE_OK) 998 return ret; 999 1000 for_each_node(nid) { 1001 cpu = cpumask_first(cpumask_of_node(nid)); 1002 c = &cpu_data(cpu); 1003 1004 p = find_patch(cpu); 1005 if (!p) 1006 continue; 1007 1008 if (c->microcode >= p->patch_id) 1009 continue; 1010 1011 ret = UCODE_NEW; 1012 } 1013 1014 return ret; 1015 } 1016 1017 /* 1018 * AMD microcode firmware naming convention, up to family 15h they are in 1019 * the legacy file: 1020 * 1021 * amd-ucode/microcode_amd.bin 1022 * 1023 * This legacy file is always smaller than 2K in size. 1024 * 1025 * Beginning with family 15h, they are in family-specific firmware files: 1026 * 1027 * amd-ucode/microcode_amd_fam15h.bin 1028 * amd-ucode/microcode_amd_fam16h.bin 1029 * ... 1030 * 1031 * These might be larger than 2K. 1032 */ 1033 static enum ucode_state request_microcode_amd(int cpu, struct device *device) 1034 { 1035 char fw_name[36] = "amd-ucode/microcode_amd.bin"; 1036 struct cpuinfo_x86 *c = &cpu_data(cpu); 1037 enum ucode_state ret = UCODE_NFOUND; 1038 const struct firmware *fw; 1039 1040 if (force_minrev) 1041 return UCODE_NFOUND; 1042 1043 if (c->x86 >= 0x15) 1044 snprintf(fw_name, sizeof(fw_name), "amd-ucode/microcode_amd_fam%.2xh.bin", c->x86); 1045 1046 if (request_firmware_direct(&fw, (const char *)fw_name, device)) { 1047 pr_debug("failed to load file %s\n", fw_name); 1048 goto out; 1049 } 1050 1051 ret = UCODE_ERROR; 1052 if (!verify_container(fw->data, fw->size)) 1053 goto fw_release; 1054 1055 ret = load_microcode_amd(c->x86, fw->data, fw->size); 1056 1057 fw_release: 1058 release_firmware(fw); 1059 1060 out: 1061 return ret; 1062 } 1063 1064 static void microcode_fini_cpu_amd(int cpu) 1065 { 1066 struct ucode_cpu_info *uci = ucode_cpu_info + cpu; 1067 1068 uci->mc = NULL; 1069 } 1070 1071 static struct microcode_ops microcode_amd_ops = { 1072 .request_microcode_fw = request_microcode_amd, 1073 .collect_cpu_info = collect_cpu_info_amd, 1074 .apply_microcode = apply_microcode_amd, 1075 .microcode_fini_cpu = microcode_fini_cpu_amd, 1076 .nmi_safe = true, 1077 }; 1078 1079 struct microcode_ops * __init init_amd_microcode(void) 1080 { 1081 struct cpuinfo_x86 *c = &boot_cpu_data; 1082 1083 if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) { 1084 pr_warn("AMD CPU family 0x%x not supported\n", c->x86); 1085 return NULL; 1086 } 1087 return µcode_amd_ops; 1088 } 1089 1090 void __exit exit_amd_microcode(void) 1091 { 1092 cleanup(); 1093 } 1094