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 __verify_patch_section(const u8 *buf, size_t buf_size, u32 *sh_psize) 250 { 251 u32 p_type, p_size; 252 const u32 *hdr; 253 254 if (buf_size < SECTION_HDR_SIZE) { 255 pr_debug("Truncated patch section.\n"); 256 return false; 257 } 258 259 hdr = (const u32 *)buf; 260 p_type = hdr[0]; 261 p_size = hdr[1]; 262 263 if (p_type != UCODE_UCODE_TYPE) { 264 pr_debug("Invalid type field (0x%x) in container file section header.\n", 265 p_type); 266 return false; 267 } 268 269 if (p_size < sizeof(struct microcode_header_amd)) { 270 pr_debug("Patch of size %u too short.\n", p_size); 271 return false; 272 } 273 274 *sh_psize = p_size; 275 276 return true; 277 } 278 279 /* 280 * Check whether the passed remaining file @buf_size is large enough to contain 281 * a patch of the indicated @sh_psize (and also whether this size does not 282 * exceed the per-family maximum). @sh_psize is the size read from the section 283 * header. 284 */ 285 static bool __verify_patch_size(u32 sh_psize, size_t buf_size) 286 { 287 u8 family = x86_family(bsp_cpuid_1_eax); 288 u32 max_size; 289 290 if (family >= 0x15) 291 goto ret; 292 293 #define F1XH_MPB_MAX_SIZE 2048 294 #define F14H_MPB_MAX_SIZE 1824 295 296 switch (family) { 297 case 0x10 ... 0x12: 298 max_size = F1XH_MPB_MAX_SIZE; 299 break; 300 case 0x14: 301 max_size = F14H_MPB_MAX_SIZE; 302 break; 303 default: 304 WARN(1, "%s: WTF family: 0x%x\n", __func__, family); 305 return false; 306 } 307 308 if (sh_psize > max_size) 309 return false; 310 311 ret: 312 /* Working with the whole buffer so < is ok. */ 313 return sh_psize <= buf_size; 314 } 315 316 /* 317 * Verify the patch in @buf. 318 * 319 * Returns: 320 * negative: on error 321 * positive: patch is not for this family, skip it 322 * 0: success 323 */ 324 static int verify_patch(const u8 *buf, size_t buf_size, u32 *patch_size) 325 { 326 u8 family = x86_family(bsp_cpuid_1_eax); 327 struct microcode_header_amd *mc_hdr; 328 u32 sh_psize; 329 u16 proc_id; 330 u8 patch_fam; 331 332 if (!__verify_patch_section(buf, buf_size, &sh_psize)) 333 return -1; 334 335 /* 336 * The section header length is not included in this indicated size 337 * but is present in the leftover file length so we need to subtract 338 * it before passing this value to the function below. 339 */ 340 buf_size -= SECTION_HDR_SIZE; 341 342 /* 343 * Check if the remaining buffer is big enough to contain a patch of 344 * size sh_psize, as the section claims. 345 */ 346 if (buf_size < sh_psize) { 347 pr_debug("Patch of size %u truncated.\n", sh_psize); 348 return -1; 349 } 350 351 if (!__verify_patch_size(sh_psize, buf_size)) { 352 pr_debug("Per-family patch size mismatch.\n"); 353 return -1; 354 } 355 356 *patch_size = sh_psize; 357 358 mc_hdr = (struct microcode_header_amd *)(buf + SECTION_HDR_SIZE); 359 if (mc_hdr->nb_dev_id || mc_hdr->sb_dev_id) { 360 pr_err("Patch-ID 0x%08x: chipset-specific code unsupported.\n", mc_hdr->patch_id); 361 return -1; 362 } 363 364 proc_id = mc_hdr->processor_rev_id; 365 patch_fam = 0xf + (proc_id >> 12); 366 if (patch_fam != family) 367 return 1; 368 369 return 0; 370 } 371 372 static bool mc_patch_matches(struct microcode_amd *mc, u16 eq_id) 373 { 374 /* Zen and newer do not need an equivalence table. */ 375 if (x86_family(bsp_cpuid_1_eax) >= 0x17) 376 return ucode_rev_to_cpuid(mc->hdr.patch_id).full == bsp_cpuid_1_eax; 377 else 378 return eq_id == mc->hdr.processor_rev_id; 379 } 380 381 /* 382 * This scans the ucode blob for the proper container as we can have multiple 383 * containers glued together. 384 * 385 * Returns the amount of bytes consumed while scanning. @desc contains all the 386 * data we're going to use in later stages of the application. 387 */ 388 static size_t parse_container(u8 *ucode, size_t size, struct cont_desc *desc) 389 { 390 struct equiv_cpu_table table; 391 size_t orig_size = size; 392 u32 *hdr = (u32 *)ucode; 393 u16 eq_id; 394 u8 *buf; 395 396 if (!verify_equivalence_table(ucode, size)) 397 return 0; 398 399 buf = ucode; 400 401 table.entry = (struct equiv_cpu_entry *)(buf + CONTAINER_HDR_SZ); 402 table.num_entries = hdr[2] / sizeof(struct equiv_cpu_entry); 403 404 /* 405 * Find the equivalence ID of our CPU in this table. Even if this table 406 * doesn't contain a patch for the CPU, scan through the whole container 407 * so that it can be skipped in case there are other containers appended. 408 */ 409 eq_id = find_equiv_id(&table, bsp_cpuid_1_eax); 410 411 buf += hdr[2] + CONTAINER_HDR_SZ; 412 size -= hdr[2] + CONTAINER_HDR_SZ; 413 414 /* 415 * Scan through the rest of the container to find where it ends. We do 416 * some basic sanity-checking too. 417 */ 418 while (size > 0) { 419 struct microcode_amd *mc; 420 u32 patch_size; 421 int ret; 422 423 ret = verify_patch(buf, size, &patch_size); 424 if (ret < 0) { 425 /* 426 * Patch verification failed, skip to the next container, if 427 * there is one. Before exit, check whether that container has 428 * found a patch already. If so, use it. 429 */ 430 goto out; 431 } else if (ret > 0) { 432 goto skip; 433 } 434 435 mc = (struct microcode_amd *)(buf + SECTION_HDR_SIZE); 436 if (mc_patch_matches(mc, eq_id)) { 437 desc->psize = patch_size; 438 desc->mc = mc; 439 } 440 441 skip: 442 /* Skip patch section header too: */ 443 buf += patch_size + SECTION_HDR_SIZE; 444 size -= patch_size + SECTION_HDR_SIZE; 445 } 446 447 out: 448 /* 449 * If we have found a patch (desc->mc), it means we're looking at the 450 * container which has a patch for this CPU so return 0 to mean, @ucode 451 * already points to the proper container. Otherwise, we return the size 452 * we scanned so that we can advance to the next container in the 453 * buffer. 454 */ 455 if (desc->mc) { 456 desc->data = ucode; 457 desc->size = orig_size - size; 458 459 return 0; 460 } 461 462 return orig_size - size; 463 } 464 465 /* 466 * Scan the ucode blob for the proper container as we can have multiple 467 * containers glued together. 468 */ 469 static void scan_containers(u8 *ucode, size_t size, struct cont_desc *desc) 470 { 471 while (size) { 472 size_t s = parse_container(ucode, size, desc); 473 if (!s) 474 return; 475 476 /* catch wraparound */ 477 if (size >= s) { 478 ucode += s; 479 size -= s; 480 } else { 481 return; 482 } 483 } 484 } 485 486 static bool __apply_microcode_amd(struct microcode_amd *mc, unsigned int psize) 487 { 488 unsigned long p_addr = (unsigned long)&mc->hdr.data_code; 489 u32 rev, dummy; 490 491 native_wrmsrl(MSR_AMD64_PATCH_LOADER, p_addr); 492 493 if (x86_family(bsp_cpuid_1_eax) == 0x17) { 494 unsigned long p_addr_end = p_addr + psize - 1; 495 496 invlpg(p_addr); 497 498 /* 499 * Flush next page too if patch image is crossing a page 500 * boundary. 501 */ 502 if (p_addr >> PAGE_SHIFT != p_addr_end >> PAGE_SHIFT) 503 invlpg(p_addr_end); 504 } 505 506 /* verify patch application was successful */ 507 native_rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy); 508 509 if (rev != mc->hdr.patch_id) 510 return false; 511 512 return true; 513 } 514 515 /* 516 * Early load occurs before we can vmalloc(). So we look for the microcode 517 * patch container file in initrd, traverse equivalent cpu table, look for a 518 * matching microcode patch, and update, all in initrd memory in place. 519 * When vmalloc() is available for use later -- on 64-bit during first AP load, 520 * and on 32-bit during save_microcode_in_initrd() -- we can call 521 * load_microcode_amd() to save equivalent cpu table and microcode patches in 522 * kernel heap memory. 523 * 524 * Returns true if container found (sets @desc), false otherwise. 525 */ 526 static bool early_apply_microcode(u32 old_rev, void *ucode, size_t size) 527 { 528 struct cont_desc desc = { 0 }; 529 struct microcode_amd *mc; 530 531 scan_containers(ucode, size, &desc); 532 533 mc = desc.mc; 534 if (!mc) 535 return false; 536 537 /* 538 * Allow application of the same revision to pick up SMT-specific 539 * changes even if the revision of the other SMT thread is already 540 * up-to-date. 541 */ 542 if (old_rev > mc->hdr.patch_id) 543 return false; 544 545 return __apply_microcode_amd(mc, desc.psize); 546 } 547 548 static bool get_builtin_microcode(struct cpio_data *cp) 549 { 550 char fw_name[36] = "amd-ucode/microcode_amd.bin"; 551 u8 family = x86_family(bsp_cpuid_1_eax); 552 struct firmware fw; 553 554 if (IS_ENABLED(CONFIG_X86_32)) 555 return false; 556 557 if (family >= 0x15) 558 snprintf(fw_name, sizeof(fw_name), 559 "amd-ucode/microcode_amd_fam%02hhxh.bin", family); 560 561 if (firmware_request_builtin(&fw, fw_name)) { 562 cp->size = fw.size; 563 cp->data = (void *)fw.data; 564 return true; 565 } 566 567 return false; 568 } 569 570 static bool __init find_blobs_in_containers(struct cpio_data *ret) 571 { 572 struct cpio_data cp; 573 bool found; 574 575 if (!get_builtin_microcode(&cp)) 576 cp = find_microcode_in_initrd(ucode_path); 577 578 found = cp.data && cp.size; 579 if (found) 580 *ret = cp; 581 582 return found; 583 } 584 585 void __init load_ucode_amd_bsp(struct early_load_data *ed, unsigned int cpuid_1_eax) 586 { 587 struct cpio_data cp = { }; 588 u32 dummy; 589 590 bsp_cpuid_1_eax = cpuid_1_eax; 591 592 native_rdmsr(MSR_AMD64_PATCH_LEVEL, ed->old_rev, dummy); 593 594 /* Needed in load_microcode_amd() */ 595 ucode_cpu_info[0].cpu_sig.sig = cpuid_1_eax; 596 597 if (!find_blobs_in_containers(&cp)) 598 return; 599 600 if (early_apply_microcode(ed->old_rev, cp.data, cp.size)) 601 native_rdmsr(MSR_AMD64_PATCH_LEVEL, ed->new_rev, dummy); 602 } 603 604 static enum ucode_state _load_microcode_amd(u8 family, const u8 *data, size_t size); 605 606 static int __init save_microcode_in_initrd(void) 607 { 608 unsigned int cpuid_1_eax = native_cpuid_eax(1); 609 struct cpuinfo_x86 *c = &boot_cpu_data; 610 struct cont_desc desc = { 0 }; 611 enum ucode_state ret; 612 struct cpio_data cp; 613 614 if (dis_ucode_ldr || c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) 615 return 0; 616 617 if (!find_blobs_in_containers(&cp)) 618 return -EINVAL; 619 620 scan_containers(cp.data, cp.size, &desc); 621 if (!desc.mc) 622 return -EINVAL; 623 624 ret = _load_microcode_amd(x86_family(cpuid_1_eax), desc.data, desc.size); 625 if (ret > UCODE_UPDATED) 626 return -EINVAL; 627 628 return 0; 629 } 630 early_initcall(save_microcode_in_initrd); 631 632 static inline bool patch_cpus_equivalent(struct ucode_patch *p, 633 struct ucode_patch *n, 634 bool ignore_stepping) 635 { 636 /* Zen and newer hardcode the f/m/s in the patch ID */ 637 if (x86_family(bsp_cpuid_1_eax) >= 0x17) { 638 union cpuid_1_eax p_cid = ucode_rev_to_cpuid(p->patch_id); 639 union cpuid_1_eax n_cid = ucode_rev_to_cpuid(n->patch_id); 640 641 if (ignore_stepping) { 642 p_cid.stepping = 0; 643 n_cid.stepping = 0; 644 } 645 646 return p_cid.full == n_cid.full; 647 } else { 648 return p->equiv_cpu == n->equiv_cpu; 649 } 650 } 651 652 /* 653 * a small, trivial cache of per-family ucode patches 654 */ 655 static struct ucode_patch *cache_find_patch(struct ucode_cpu_info *uci, u16 equiv_cpu) 656 { 657 struct ucode_patch *p; 658 struct ucode_patch n; 659 660 n.equiv_cpu = equiv_cpu; 661 n.patch_id = uci->cpu_sig.rev; 662 663 WARN_ON_ONCE(!n.patch_id); 664 665 list_for_each_entry(p, µcode_cache, plist) 666 if (patch_cpus_equivalent(p, &n, false)) 667 return p; 668 669 return NULL; 670 } 671 672 static inline int patch_newer(struct ucode_patch *p, struct ucode_patch *n) 673 { 674 /* Zen and newer hardcode the f/m/s in the patch ID */ 675 if (x86_family(bsp_cpuid_1_eax) >= 0x17) { 676 union zen_patch_rev zp, zn; 677 678 zp.ucode_rev = p->patch_id; 679 zn.ucode_rev = n->patch_id; 680 681 if (zn.stepping != zp.stepping) 682 return -1; 683 684 return zn.rev > zp.rev; 685 } else { 686 return n->patch_id > p->patch_id; 687 } 688 } 689 690 static void update_cache(struct ucode_patch *new_patch) 691 { 692 struct ucode_patch *p; 693 int ret; 694 695 list_for_each_entry(p, µcode_cache, plist) { 696 if (patch_cpus_equivalent(p, new_patch, true)) { 697 ret = patch_newer(p, new_patch); 698 if (ret < 0) 699 continue; 700 else if (!ret) { 701 /* we already have the latest patch */ 702 kfree(new_patch->data); 703 kfree(new_patch); 704 return; 705 } 706 707 list_replace(&p->plist, &new_patch->plist); 708 kfree(p->data); 709 kfree(p); 710 return; 711 } 712 } 713 /* no patch found, add it */ 714 list_add_tail(&new_patch->plist, µcode_cache); 715 } 716 717 static void free_cache(void) 718 { 719 struct ucode_patch *p, *tmp; 720 721 list_for_each_entry_safe(p, tmp, µcode_cache, plist) { 722 __list_del(p->plist.prev, p->plist.next); 723 kfree(p->data); 724 kfree(p); 725 } 726 } 727 728 static struct ucode_patch *find_patch(unsigned int cpu) 729 { 730 struct ucode_cpu_info *uci = ucode_cpu_info + cpu; 731 u32 rev, dummy __always_unused; 732 u16 equiv_id = 0; 733 734 /* fetch rev if not populated yet: */ 735 if (!uci->cpu_sig.rev) { 736 rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy); 737 uci->cpu_sig.rev = rev; 738 } 739 740 if (x86_family(bsp_cpuid_1_eax) < 0x17) { 741 equiv_id = find_equiv_id(&equiv_table, uci->cpu_sig.sig); 742 if (!equiv_id) 743 return NULL; 744 } 745 746 return cache_find_patch(uci, equiv_id); 747 } 748 749 void reload_ucode_amd(unsigned int cpu) 750 { 751 u32 rev, dummy __always_unused; 752 struct microcode_amd *mc; 753 struct ucode_patch *p; 754 755 p = find_patch(cpu); 756 if (!p) 757 return; 758 759 mc = p->data; 760 761 rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy); 762 763 if (rev < mc->hdr.patch_id) { 764 if (__apply_microcode_amd(mc, p->size)) 765 pr_info_once("reload revision: 0x%08x\n", mc->hdr.patch_id); 766 } 767 } 768 769 static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig) 770 { 771 struct cpuinfo_x86 *c = &cpu_data(cpu); 772 struct ucode_cpu_info *uci = ucode_cpu_info + cpu; 773 struct ucode_patch *p; 774 775 csig->sig = cpuid_eax(0x00000001); 776 csig->rev = c->microcode; 777 778 /* 779 * a patch could have been loaded early, set uci->mc so that 780 * mc_bp_resume() can call apply_microcode() 781 */ 782 p = find_patch(cpu); 783 if (p && (p->patch_id == csig->rev)) 784 uci->mc = p->data; 785 786 return 0; 787 } 788 789 static enum ucode_state apply_microcode_amd(int cpu) 790 { 791 struct cpuinfo_x86 *c = &cpu_data(cpu); 792 struct microcode_amd *mc_amd; 793 struct ucode_cpu_info *uci; 794 struct ucode_patch *p; 795 enum ucode_state ret; 796 u32 rev; 797 798 BUG_ON(raw_smp_processor_id() != cpu); 799 800 uci = ucode_cpu_info + cpu; 801 802 p = find_patch(cpu); 803 if (!p) 804 return UCODE_NFOUND; 805 806 rev = uci->cpu_sig.rev; 807 808 mc_amd = p->data; 809 uci->mc = p->data; 810 811 /* need to apply patch? */ 812 if (rev > mc_amd->hdr.patch_id) { 813 ret = UCODE_OK; 814 goto out; 815 } 816 817 if (!__apply_microcode_amd(mc_amd, p->size)) { 818 pr_err("CPU%d: update failed for patch_level=0x%08x\n", 819 cpu, mc_amd->hdr.patch_id); 820 return UCODE_ERROR; 821 } 822 823 rev = mc_amd->hdr.patch_id; 824 ret = UCODE_UPDATED; 825 826 out: 827 uci->cpu_sig.rev = rev; 828 c->microcode = rev; 829 830 /* Update boot_cpu_data's revision too, if we're on the BSP: */ 831 if (c->cpu_index == boot_cpu_data.cpu_index) 832 boot_cpu_data.microcode = rev; 833 834 return ret; 835 } 836 837 void load_ucode_amd_ap(unsigned int cpuid_1_eax) 838 { 839 unsigned int cpu = smp_processor_id(); 840 841 ucode_cpu_info[cpu].cpu_sig.sig = cpuid_1_eax; 842 apply_microcode_amd(cpu); 843 } 844 845 static size_t install_equiv_cpu_table(const u8 *buf, size_t buf_size) 846 { 847 u32 equiv_tbl_len; 848 const u32 *hdr; 849 850 if (!verify_equivalence_table(buf, buf_size)) 851 return 0; 852 853 hdr = (const u32 *)buf; 854 equiv_tbl_len = hdr[2]; 855 856 /* Zen and newer do not need an equivalence table. */ 857 if (x86_family(bsp_cpuid_1_eax) >= 0x17) 858 goto out; 859 860 equiv_table.entry = vmalloc(equiv_tbl_len); 861 if (!equiv_table.entry) { 862 pr_err("failed to allocate equivalent CPU table\n"); 863 return 0; 864 } 865 866 memcpy(equiv_table.entry, buf + CONTAINER_HDR_SZ, equiv_tbl_len); 867 equiv_table.num_entries = equiv_tbl_len / sizeof(struct equiv_cpu_entry); 868 869 out: 870 /* add header length */ 871 return equiv_tbl_len + CONTAINER_HDR_SZ; 872 } 873 874 static void free_equiv_cpu_table(void) 875 { 876 if (x86_family(bsp_cpuid_1_eax) >= 0x17) 877 return; 878 879 vfree(equiv_table.entry); 880 memset(&equiv_table, 0, sizeof(equiv_table)); 881 } 882 883 static void cleanup(void) 884 { 885 free_equiv_cpu_table(); 886 free_cache(); 887 } 888 889 /* 890 * Return a non-negative value even if some of the checks failed so that 891 * we can skip over the next patch. If we return a negative value, we 892 * signal a grave error like a memory allocation has failed and the 893 * driver cannot continue functioning normally. In such cases, we tear 894 * down everything we've used up so far and exit. 895 */ 896 static int verify_and_add_patch(u8 family, u8 *fw, unsigned int leftover, 897 unsigned int *patch_size) 898 { 899 struct microcode_header_amd *mc_hdr; 900 struct ucode_patch *patch; 901 u16 proc_id; 902 int ret; 903 904 ret = verify_patch(fw, leftover, patch_size); 905 if (ret) 906 return ret; 907 908 patch = kzalloc(sizeof(*patch), GFP_KERNEL); 909 if (!patch) { 910 pr_err("Patch allocation failure.\n"); 911 return -EINVAL; 912 } 913 914 patch->data = kmemdup(fw + SECTION_HDR_SIZE, *patch_size, GFP_KERNEL); 915 if (!patch->data) { 916 pr_err("Patch data allocation failure.\n"); 917 kfree(patch); 918 return -EINVAL; 919 } 920 patch->size = *patch_size; 921 922 mc_hdr = (struct microcode_header_amd *)(fw + SECTION_HDR_SIZE); 923 proc_id = mc_hdr->processor_rev_id; 924 925 INIT_LIST_HEAD(&patch->plist); 926 patch->patch_id = mc_hdr->patch_id; 927 patch->equiv_cpu = proc_id; 928 929 pr_debug("%s: Adding patch_id: 0x%08x, proc_id: 0x%04x\n", 930 __func__, patch->patch_id, proc_id); 931 932 /* ... and add to cache. */ 933 update_cache(patch); 934 935 return 0; 936 } 937 938 /* Scan the blob in @data and add microcode patches to the cache. */ 939 static enum ucode_state __load_microcode_amd(u8 family, const u8 *data, 940 size_t size) 941 { 942 u8 *fw = (u8 *)data; 943 size_t offset; 944 945 offset = install_equiv_cpu_table(data, size); 946 if (!offset) 947 return UCODE_ERROR; 948 949 fw += offset; 950 size -= offset; 951 952 if (*(u32 *)fw != UCODE_UCODE_TYPE) { 953 pr_err("invalid type field in container file section header\n"); 954 free_equiv_cpu_table(); 955 return UCODE_ERROR; 956 } 957 958 while (size > 0) { 959 unsigned int crnt_size = 0; 960 int ret; 961 962 ret = verify_and_add_patch(family, fw, size, &crnt_size); 963 if (ret < 0) 964 return UCODE_ERROR; 965 966 fw += crnt_size + SECTION_HDR_SIZE; 967 size -= (crnt_size + SECTION_HDR_SIZE); 968 } 969 970 return UCODE_OK; 971 } 972 973 static enum ucode_state _load_microcode_amd(u8 family, const u8 *data, size_t size) 974 { 975 enum ucode_state ret; 976 977 /* free old equiv table */ 978 free_equiv_cpu_table(); 979 980 ret = __load_microcode_amd(family, data, size); 981 if (ret != UCODE_OK) 982 cleanup(); 983 984 return ret; 985 } 986 987 static enum ucode_state load_microcode_amd(u8 family, const u8 *data, size_t size) 988 { 989 struct cpuinfo_x86 *c; 990 unsigned int nid, cpu; 991 struct ucode_patch *p; 992 enum ucode_state ret; 993 994 ret = _load_microcode_amd(family, data, size); 995 if (ret != UCODE_OK) 996 return ret; 997 998 for_each_node(nid) { 999 cpu = cpumask_first(cpumask_of_node(nid)); 1000 c = &cpu_data(cpu); 1001 1002 p = find_patch(cpu); 1003 if (!p) 1004 continue; 1005 1006 if (c->microcode >= p->patch_id) 1007 continue; 1008 1009 ret = UCODE_NEW; 1010 } 1011 1012 return ret; 1013 } 1014 1015 /* 1016 * AMD microcode firmware naming convention, up to family 15h they are in 1017 * the legacy file: 1018 * 1019 * amd-ucode/microcode_amd.bin 1020 * 1021 * This legacy file is always smaller than 2K in size. 1022 * 1023 * Beginning with family 15h, they are in family-specific firmware files: 1024 * 1025 * amd-ucode/microcode_amd_fam15h.bin 1026 * amd-ucode/microcode_amd_fam16h.bin 1027 * ... 1028 * 1029 * These might be larger than 2K. 1030 */ 1031 static enum ucode_state request_microcode_amd(int cpu, struct device *device) 1032 { 1033 char fw_name[36] = "amd-ucode/microcode_amd.bin"; 1034 struct cpuinfo_x86 *c = &cpu_data(cpu); 1035 enum ucode_state ret = UCODE_NFOUND; 1036 const struct firmware *fw; 1037 1038 if (force_minrev) 1039 return UCODE_NFOUND; 1040 1041 if (c->x86 >= 0x15) 1042 snprintf(fw_name, sizeof(fw_name), "amd-ucode/microcode_amd_fam%.2xh.bin", c->x86); 1043 1044 if (request_firmware_direct(&fw, (const char *)fw_name, device)) { 1045 pr_debug("failed to load file %s\n", fw_name); 1046 goto out; 1047 } 1048 1049 ret = UCODE_ERROR; 1050 if (!verify_container(fw->data, fw->size)) 1051 goto fw_release; 1052 1053 ret = load_microcode_amd(c->x86, fw->data, fw->size); 1054 1055 fw_release: 1056 release_firmware(fw); 1057 1058 out: 1059 return ret; 1060 } 1061 1062 static void microcode_fini_cpu_amd(int cpu) 1063 { 1064 struct ucode_cpu_info *uci = ucode_cpu_info + cpu; 1065 1066 uci->mc = NULL; 1067 } 1068 1069 static struct microcode_ops microcode_amd_ops = { 1070 .request_microcode_fw = request_microcode_amd, 1071 .collect_cpu_info = collect_cpu_info_amd, 1072 .apply_microcode = apply_microcode_amd, 1073 .microcode_fini_cpu = microcode_fini_cpu_amd, 1074 .nmi_safe = true, 1075 }; 1076 1077 struct microcode_ops * __init init_amd_microcode(void) 1078 { 1079 struct cpuinfo_x86 *c = &boot_cpu_data; 1080 1081 if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) { 1082 pr_warn("AMD CPU family 0x%x not supported\n", c->x86); 1083 return NULL; 1084 } 1085 return µcode_amd_ops; 1086 } 1087 1088 void __exit exit_amd_microcode(void) 1089 { 1090 cleanup(); 1091 } 1092