1 /* 2 * CPU Microcode Update Driver for Linux 3 * 4 * Copyright (C) 2000-2006 Tigran Aivazian <aivazian.tigran@gmail.com> 5 * 2006 Shaohua Li <shaohua.li@intel.com> 6 * 2013-2016 Borislav Petkov <bp@alien8.de> 7 * 8 * X86 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 * (C) 2015 Borislav Petkov <bp@alien8.de> 13 * 14 * This driver allows to upgrade microcode on x86 processors. 15 * 16 * This program is free software; you can redistribute it and/or 17 * modify it under the terms of the GNU General Public License 18 * as published by the Free Software Foundation; either version 19 * 2 of the License, or (at your option) any later version. 20 */ 21 22 #define pr_fmt(fmt) "microcode: " fmt 23 24 #include <linux/platform_device.h> 25 #include <linux/syscore_ops.h> 26 #include <linux/miscdevice.h> 27 #include <linux/capability.h> 28 #include <linux/firmware.h> 29 #include <linux/kernel.h> 30 #include <linux/mutex.h> 31 #include <linux/cpu.h> 32 #include <linux/fs.h> 33 #include <linux/mm.h> 34 35 #include <asm/microcode_intel.h> 36 #include <asm/cpu_device_id.h> 37 #include <asm/microcode_amd.h> 38 #include <asm/perf_event.h> 39 #include <asm/microcode.h> 40 #include <asm/processor.h> 41 #include <asm/cmdline.h> 42 #include <asm/setup.h> 43 44 #define DRIVER_VERSION "2.2" 45 46 static struct microcode_ops *microcode_ops; 47 static bool dis_ucode_ldr = true; 48 49 bool initrd_gone; 50 51 LIST_HEAD(microcode_cache); 52 53 /* 54 * Synchronization. 55 * 56 * All non cpu-hotplug-callback call sites use: 57 * 58 * - microcode_mutex to synchronize with each other; 59 * - get/put_online_cpus() to synchronize with 60 * the cpu-hotplug-callback call sites. 61 * 62 * We guarantee that only a single cpu is being 63 * updated at any particular moment of time. 64 */ 65 static DEFINE_MUTEX(microcode_mutex); 66 67 struct ucode_cpu_info ucode_cpu_info[NR_CPUS]; 68 69 struct cpu_info_ctx { 70 struct cpu_signature *cpu_sig; 71 int err; 72 }; 73 74 /* 75 * Those patch levels cannot be updated to newer ones and thus should be final. 76 */ 77 static u32 final_levels[] = { 78 0x01000098, 79 0x0100009f, 80 0x010000af, 81 0, /* T-101 terminator */ 82 }; 83 84 /* 85 * Check the current patch level on this CPU. 86 * 87 * Returns: 88 * - true: if update should stop 89 * - false: otherwise 90 */ 91 static bool amd_check_current_patch_level(void) 92 { 93 u32 lvl, dummy, i; 94 u32 *levels; 95 96 native_rdmsr(MSR_AMD64_PATCH_LEVEL, lvl, dummy); 97 98 if (IS_ENABLED(CONFIG_X86_32)) 99 levels = (u32 *)__pa_nodebug(&final_levels); 100 else 101 levels = final_levels; 102 103 for (i = 0; levels[i]; i++) { 104 if (lvl == levels[i]) 105 return true; 106 } 107 return false; 108 } 109 110 static bool __init check_loader_disabled_bsp(void) 111 { 112 static const char *__dis_opt_str = "dis_ucode_ldr"; 113 114 #ifdef CONFIG_X86_32 115 const char *cmdline = (const char *)__pa_nodebug(boot_command_line); 116 const char *option = (const char *)__pa_nodebug(__dis_opt_str); 117 bool *res = (bool *)__pa_nodebug(&dis_ucode_ldr); 118 119 #else /* CONFIG_X86_64 */ 120 const char *cmdline = boot_command_line; 121 const char *option = __dis_opt_str; 122 bool *res = &dis_ucode_ldr; 123 #endif 124 125 /* 126 * CPUID(1).ECX[31]: reserved for hypervisor use. This is still not 127 * completely accurate as xen pv guests don't see that CPUID bit set but 128 * that's good enough as they don't land on the BSP path anyway. 129 */ 130 if (native_cpuid_ecx(1) & BIT(31)) 131 return *res; 132 133 if (x86_cpuid_vendor() == X86_VENDOR_AMD) { 134 if (amd_check_current_patch_level()) 135 return *res; 136 } 137 138 if (cmdline_find_option_bool(cmdline, option) <= 0) 139 *res = false; 140 141 return *res; 142 } 143 144 extern struct builtin_fw __start_builtin_fw[]; 145 extern struct builtin_fw __end_builtin_fw[]; 146 147 bool get_builtin_firmware(struct cpio_data *cd, const char *name) 148 { 149 #ifdef CONFIG_FW_LOADER 150 struct builtin_fw *b_fw; 151 152 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) { 153 if (!strcmp(name, b_fw->name)) { 154 cd->size = b_fw->size; 155 cd->data = b_fw->data; 156 return true; 157 } 158 } 159 #endif 160 return false; 161 } 162 163 void __init load_ucode_bsp(void) 164 { 165 unsigned int cpuid_1_eax; 166 bool intel = true; 167 168 if (!have_cpuid_p()) 169 return; 170 171 cpuid_1_eax = native_cpuid_eax(1); 172 173 switch (x86_cpuid_vendor()) { 174 case X86_VENDOR_INTEL: 175 if (x86_family(cpuid_1_eax) < 6) 176 return; 177 break; 178 179 case X86_VENDOR_AMD: 180 if (x86_family(cpuid_1_eax) < 0x10) 181 return; 182 intel = false; 183 break; 184 185 default: 186 return; 187 } 188 189 if (check_loader_disabled_bsp()) 190 return; 191 192 if (intel) 193 load_ucode_intel_bsp(); 194 else 195 load_ucode_amd_bsp(cpuid_1_eax); 196 } 197 198 static bool check_loader_disabled_ap(void) 199 { 200 #ifdef CONFIG_X86_32 201 return *((bool *)__pa_nodebug(&dis_ucode_ldr)); 202 #else 203 return dis_ucode_ldr; 204 #endif 205 } 206 207 void load_ucode_ap(void) 208 { 209 unsigned int cpuid_1_eax; 210 211 if (check_loader_disabled_ap()) 212 return; 213 214 cpuid_1_eax = native_cpuid_eax(1); 215 216 switch (x86_cpuid_vendor()) { 217 case X86_VENDOR_INTEL: 218 if (x86_family(cpuid_1_eax) >= 6) 219 load_ucode_intel_ap(); 220 break; 221 case X86_VENDOR_AMD: 222 if (x86_family(cpuid_1_eax) >= 0x10) 223 load_ucode_amd_ap(cpuid_1_eax); 224 break; 225 default: 226 break; 227 } 228 } 229 230 static int __init save_microcode_in_initrd(void) 231 { 232 struct cpuinfo_x86 *c = &boot_cpu_data; 233 int ret = -EINVAL; 234 235 switch (c->x86_vendor) { 236 case X86_VENDOR_INTEL: 237 if (c->x86 >= 6) 238 ret = save_microcode_in_initrd_intel(); 239 break; 240 case X86_VENDOR_AMD: 241 if (c->x86 >= 0x10) 242 ret = save_microcode_in_initrd_amd(cpuid_eax(1)); 243 break; 244 default: 245 break; 246 } 247 248 initrd_gone = true; 249 250 return ret; 251 } 252 253 struct cpio_data find_microcode_in_initrd(const char *path, bool use_pa) 254 { 255 #ifdef CONFIG_BLK_DEV_INITRD 256 unsigned long start = 0; 257 size_t size; 258 259 #ifdef CONFIG_X86_32 260 struct boot_params *params; 261 262 if (use_pa) 263 params = (struct boot_params *)__pa_nodebug(&boot_params); 264 else 265 params = &boot_params; 266 267 size = params->hdr.ramdisk_size; 268 269 /* 270 * Set start only if we have an initrd image. We cannot use initrd_start 271 * because it is not set that early yet. 272 */ 273 if (size) 274 start = params->hdr.ramdisk_image; 275 276 # else /* CONFIG_X86_64 */ 277 size = (unsigned long)boot_params.ext_ramdisk_size << 32; 278 size |= boot_params.hdr.ramdisk_size; 279 280 if (size) { 281 start = (unsigned long)boot_params.ext_ramdisk_image << 32; 282 start |= boot_params.hdr.ramdisk_image; 283 284 start += PAGE_OFFSET; 285 } 286 # endif 287 288 /* 289 * Fixup the start address: after reserve_initrd() runs, initrd_start 290 * has the virtual address of the beginning of the initrd. It also 291 * possibly relocates the ramdisk. In either case, initrd_start contains 292 * the updated address so use that instead. 293 * 294 * initrd_gone is for the hotplug case where we've thrown out initrd 295 * already. 296 */ 297 if (!use_pa) { 298 if (initrd_gone) 299 return (struct cpio_data){ NULL, 0, "" }; 300 if (initrd_start) 301 start = initrd_start; 302 } else { 303 /* 304 * The picture with physical addresses is a bit different: we 305 * need to get the *physical* address to which the ramdisk was 306 * relocated, i.e., relocated_ramdisk (not initrd_start) and 307 * since we're running from physical addresses, we need to access 308 * relocated_ramdisk through its *physical* address too. 309 */ 310 u64 *rr = (u64 *)__pa_nodebug(&relocated_ramdisk); 311 if (*rr) 312 start = *rr; 313 } 314 315 return find_cpio_data(path, (void *)start, size, NULL); 316 #else /* !CONFIG_BLK_DEV_INITRD */ 317 return (struct cpio_data){ NULL, 0, "" }; 318 #endif 319 } 320 321 void reload_early_microcode(void) 322 { 323 int vendor, family; 324 325 vendor = x86_cpuid_vendor(); 326 family = x86_cpuid_family(); 327 328 switch (vendor) { 329 case X86_VENDOR_INTEL: 330 if (family >= 6) 331 reload_ucode_intel(); 332 break; 333 case X86_VENDOR_AMD: 334 if (family >= 0x10) 335 reload_ucode_amd(); 336 break; 337 default: 338 break; 339 } 340 } 341 342 static void collect_cpu_info_local(void *arg) 343 { 344 struct cpu_info_ctx *ctx = arg; 345 346 ctx->err = microcode_ops->collect_cpu_info(smp_processor_id(), 347 ctx->cpu_sig); 348 } 349 350 static int collect_cpu_info_on_target(int cpu, struct cpu_signature *cpu_sig) 351 { 352 struct cpu_info_ctx ctx = { .cpu_sig = cpu_sig, .err = 0 }; 353 int ret; 354 355 ret = smp_call_function_single(cpu, collect_cpu_info_local, &ctx, 1); 356 if (!ret) 357 ret = ctx.err; 358 359 return ret; 360 } 361 362 static int collect_cpu_info(int cpu) 363 { 364 struct ucode_cpu_info *uci = ucode_cpu_info + cpu; 365 int ret; 366 367 memset(uci, 0, sizeof(*uci)); 368 369 ret = collect_cpu_info_on_target(cpu, &uci->cpu_sig); 370 if (!ret) 371 uci->valid = 1; 372 373 return ret; 374 } 375 376 struct apply_microcode_ctx { 377 enum ucode_state err; 378 }; 379 380 static void apply_microcode_local(void *arg) 381 { 382 struct apply_microcode_ctx *ctx = arg; 383 384 ctx->err = microcode_ops->apply_microcode(smp_processor_id()); 385 } 386 387 static int apply_microcode_on_target(int cpu) 388 { 389 struct apply_microcode_ctx ctx = { .err = 0 }; 390 int ret; 391 392 ret = smp_call_function_single(cpu, apply_microcode_local, &ctx, 1); 393 if (!ret) 394 ret = ctx.err; 395 396 return ret; 397 } 398 399 #ifdef CONFIG_MICROCODE_OLD_INTERFACE 400 static int do_microcode_update(const void __user *buf, size_t size) 401 { 402 int error = 0; 403 int cpu; 404 405 for_each_online_cpu(cpu) { 406 struct ucode_cpu_info *uci = ucode_cpu_info + cpu; 407 enum ucode_state ustate; 408 409 if (!uci->valid) 410 continue; 411 412 ustate = microcode_ops->request_microcode_user(cpu, buf, size); 413 if (ustate == UCODE_ERROR) { 414 error = -1; 415 break; 416 } else if (ustate == UCODE_OK) 417 apply_microcode_on_target(cpu); 418 } 419 420 return error; 421 } 422 423 static int microcode_open(struct inode *inode, struct file *file) 424 { 425 return capable(CAP_SYS_RAWIO) ? nonseekable_open(inode, file) : -EPERM; 426 } 427 428 static ssize_t microcode_write(struct file *file, const char __user *buf, 429 size_t len, loff_t *ppos) 430 { 431 ssize_t ret = -EINVAL; 432 433 if ((len >> PAGE_SHIFT) > totalram_pages) { 434 pr_err("too much data (max %ld pages)\n", totalram_pages); 435 return ret; 436 } 437 438 get_online_cpus(); 439 mutex_lock(µcode_mutex); 440 441 if (do_microcode_update(buf, len) == 0) 442 ret = (ssize_t)len; 443 444 if (ret > 0) 445 perf_check_microcode(); 446 447 mutex_unlock(µcode_mutex); 448 put_online_cpus(); 449 450 return ret; 451 } 452 453 static const struct file_operations microcode_fops = { 454 .owner = THIS_MODULE, 455 .write = microcode_write, 456 .open = microcode_open, 457 .llseek = no_llseek, 458 }; 459 460 static struct miscdevice microcode_dev = { 461 .minor = MICROCODE_MINOR, 462 .name = "microcode", 463 .nodename = "cpu/microcode", 464 .fops = µcode_fops, 465 }; 466 467 static int __init microcode_dev_init(void) 468 { 469 int error; 470 471 error = misc_register(µcode_dev); 472 if (error) { 473 pr_err("can't misc_register on minor=%d\n", MICROCODE_MINOR); 474 return error; 475 } 476 477 return 0; 478 } 479 480 static void __exit microcode_dev_exit(void) 481 { 482 misc_deregister(µcode_dev); 483 } 484 #else 485 #define microcode_dev_init() 0 486 #define microcode_dev_exit() do { } while (0) 487 #endif 488 489 /* fake device for request_firmware */ 490 static struct platform_device *microcode_pdev; 491 492 static enum ucode_state reload_for_cpu(int cpu) 493 { 494 struct ucode_cpu_info *uci = ucode_cpu_info + cpu; 495 enum ucode_state ustate; 496 497 if (!uci->valid) 498 return UCODE_OK; 499 500 ustate = microcode_ops->request_microcode_fw(cpu, µcode_pdev->dev, true); 501 if (ustate != UCODE_OK) 502 return ustate; 503 504 return apply_microcode_on_target(cpu); 505 } 506 507 static ssize_t reload_store(struct device *dev, 508 struct device_attribute *attr, 509 const char *buf, size_t size) 510 { 511 enum ucode_state tmp_ret = UCODE_OK; 512 bool do_callback = false; 513 unsigned long val; 514 ssize_t ret = 0; 515 int cpu; 516 517 ret = kstrtoul(buf, 0, &val); 518 if (ret) 519 return ret; 520 521 if (val != 1) 522 return size; 523 524 get_online_cpus(); 525 mutex_lock(µcode_mutex); 526 for_each_online_cpu(cpu) { 527 tmp_ret = reload_for_cpu(cpu); 528 if (tmp_ret > UCODE_NFOUND) { 529 pr_warn("Error reloading microcode on CPU %d\n", cpu); 530 531 /* set retval for the first encountered reload error */ 532 if (!ret) 533 ret = -EINVAL; 534 } 535 536 if (tmp_ret == UCODE_UPDATED) 537 do_callback = true; 538 } 539 540 if (!ret && do_callback) 541 microcode_check(); 542 543 mutex_unlock(µcode_mutex); 544 put_online_cpus(); 545 546 if (!ret) 547 ret = size; 548 549 return ret; 550 } 551 552 static ssize_t version_show(struct device *dev, 553 struct device_attribute *attr, char *buf) 554 { 555 struct ucode_cpu_info *uci = ucode_cpu_info + dev->id; 556 557 return sprintf(buf, "0x%x\n", uci->cpu_sig.rev); 558 } 559 560 static ssize_t pf_show(struct device *dev, 561 struct device_attribute *attr, char *buf) 562 { 563 struct ucode_cpu_info *uci = ucode_cpu_info + dev->id; 564 565 return sprintf(buf, "0x%x\n", uci->cpu_sig.pf); 566 } 567 568 static DEVICE_ATTR_WO(reload); 569 static DEVICE_ATTR(version, 0400, version_show, NULL); 570 static DEVICE_ATTR(processor_flags, 0400, pf_show, NULL); 571 572 static struct attribute *mc_default_attrs[] = { 573 &dev_attr_version.attr, 574 &dev_attr_processor_flags.attr, 575 NULL 576 }; 577 578 static const struct attribute_group mc_attr_group = { 579 .attrs = mc_default_attrs, 580 .name = "microcode", 581 }; 582 583 static void microcode_fini_cpu(int cpu) 584 { 585 if (microcode_ops->microcode_fini_cpu) 586 microcode_ops->microcode_fini_cpu(cpu); 587 } 588 589 static enum ucode_state microcode_resume_cpu(int cpu) 590 { 591 if (apply_microcode_on_target(cpu)) 592 return UCODE_ERROR; 593 594 pr_debug("CPU%d updated upon resume\n", cpu); 595 596 return UCODE_OK; 597 } 598 599 static enum ucode_state microcode_init_cpu(int cpu, bool refresh_fw) 600 { 601 enum ucode_state ustate; 602 struct ucode_cpu_info *uci = ucode_cpu_info + cpu; 603 604 if (uci->valid) 605 return UCODE_OK; 606 607 if (collect_cpu_info(cpu)) 608 return UCODE_ERROR; 609 610 /* --dimm. Trigger a delayed update? */ 611 if (system_state != SYSTEM_RUNNING) 612 return UCODE_NFOUND; 613 614 ustate = microcode_ops->request_microcode_fw(cpu, µcode_pdev->dev, 615 refresh_fw); 616 617 if (ustate == UCODE_OK) { 618 pr_debug("CPU%d updated upon init\n", cpu); 619 apply_microcode_on_target(cpu); 620 } 621 622 return ustate; 623 } 624 625 static enum ucode_state microcode_update_cpu(int cpu) 626 { 627 struct ucode_cpu_info *uci = ucode_cpu_info + cpu; 628 629 /* Refresh CPU microcode revision after resume. */ 630 collect_cpu_info(cpu); 631 632 if (uci->valid) 633 return microcode_resume_cpu(cpu); 634 635 return microcode_init_cpu(cpu, false); 636 } 637 638 static int mc_device_add(struct device *dev, struct subsys_interface *sif) 639 { 640 int err, cpu = dev->id; 641 642 if (!cpu_online(cpu)) 643 return 0; 644 645 pr_debug("CPU%d added\n", cpu); 646 647 err = sysfs_create_group(&dev->kobj, &mc_attr_group); 648 if (err) 649 return err; 650 651 if (microcode_init_cpu(cpu, true) == UCODE_ERROR) 652 return -EINVAL; 653 654 return err; 655 } 656 657 static void mc_device_remove(struct device *dev, struct subsys_interface *sif) 658 { 659 int cpu = dev->id; 660 661 if (!cpu_online(cpu)) 662 return; 663 664 pr_debug("CPU%d removed\n", cpu); 665 microcode_fini_cpu(cpu); 666 sysfs_remove_group(&dev->kobj, &mc_attr_group); 667 } 668 669 static struct subsys_interface mc_cpu_interface = { 670 .name = "microcode", 671 .subsys = &cpu_subsys, 672 .add_dev = mc_device_add, 673 .remove_dev = mc_device_remove, 674 }; 675 676 /** 677 * mc_bp_resume - Update boot CPU microcode during resume. 678 */ 679 static void mc_bp_resume(void) 680 { 681 int cpu = smp_processor_id(); 682 struct ucode_cpu_info *uci = ucode_cpu_info + cpu; 683 684 if (uci->valid && uci->mc) 685 microcode_ops->apply_microcode(cpu); 686 else if (!uci->mc) 687 reload_early_microcode(); 688 } 689 690 static struct syscore_ops mc_syscore_ops = { 691 .resume = mc_bp_resume, 692 }; 693 694 static int mc_cpu_online(unsigned int cpu) 695 { 696 struct device *dev; 697 698 dev = get_cpu_device(cpu); 699 microcode_update_cpu(cpu); 700 pr_debug("CPU%d added\n", cpu); 701 702 if (sysfs_create_group(&dev->kobj, &mc_attr_group)) 703 pr_err("Failed to create group for CPU%d\n", cpu); 704 return 0; 705 } 706 707 static int mc_cpu_down_prep(unsigned int cpu) 708 { 709 struct device *dev; 710 711 dev = get_cpu_device(cpu); 712 /* Suspend is in progress, only remove the interface */ 713 sysfs_remove_group(&dev->kobj, &mc_attr_group); 714 pr_debug("CPU%d removed\n", cpu); 715 716 return 0; 717 } 718 719 static struct attribute *cpu_root_microcode_attrs[] = { 720 &dev_attr_reload.attr, 721 NULL 722 }; 723 724 static const struct attribute_group cpu_root_microcode_group = { 725 .name = "microcode", 726 .attrs = cpu_root_microcode_attrs, 727 }; 728 729 int __init microcode_init(void) 730 { 731 struct cpuinfo_x86 *c = &boot_cpu_data; 732 int error; 733 734 if (dis_ucode_ldr) 735 return -EINVAL; 736 737 if (c->x86_vendor == X86_VENDOR_INTEL) 738 microcode_ops = init_intel_microcode(); 739 else if (c->x86_vendor == X86_VENDOR_AMD) 740 microcode_ops = init_amd_microcode(); 741 else 742 pr_err("no support for this CPU vendor\n"); 743 744 if (!microcode_ops) 745 return -ENODEV; 746 747 microcode_pdev = platform_device_register_simple("microcode", -1, 748 NULL, 0); 749 if (IS_ERR(microcode_pdev)) 750 return PTR_ERR(microcode_pdev); 751 752 get_online_cpus(); 753 mutex_lock(µcode_mutex); 754 755 error = subsys_interface_register(&mc_cpu_interface); 756 if (!error) 757 perf_check_microcode(); 758 mutex_unlock(µcode_mutex); 759 put_online_cpus(); 760 761 if (error) 762 goto out_pdev; 763 764 error = sysfs_create_group(&cpu_subsys.dev_root->kobj, 765 &cpu_root_microcode_group); 766 767 if (error) { 768 pr_err("Error creating microcode group!\n"); 769 goto out_driver; 770 } 771 772 error = microcode_dev_init(); 773 if (error) 774 goto out_ucode_group; 775 776 register_syscore_ops(&mc_syscore_ops); 777 cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "x86/microcode:online", 778 mc_cpu_online, mc_cpu_down_prep); 779 780 pr_info("Microcode Update Driver: v%s.", DRIVER_VERSION); 781 782 return 0; 783 784 out_ucode_group: 785 sysfs_remove_group(&cpu_subsys.dev_root->kobj, 786 &cpu_root_microcode_group); 787 788 out_driver: 789 get_online_cpus(); 790 mutex_lock(µcode_mutex); 791 792 subsys_interface_unregister(&mc_cpu_interface); 793 794 mutex_unlock(µcode_mutex); 795 put_online_cpus(); 796 797 out_pdev: 798 platform_device_unregister(microcode_pdev); 799 return error; 800 801 } 802 fs_initcall(save_microcode_in_initrd); 803 late_initcall(microcode_init); 804