1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * CPU Microcode Update Driver for Linux 4 * 5 * Copyright (C) 2000-2006 Tigran Aivazian <[email protected]> 6 * 2006 Shaohua Li <[email protected]> 7 * 2013-2016 Borislav Petkov <[email protected]> 8 * 9 * X86 CPU microcode early update for Linux: 10 * 11 * Copyright (C) 2012 Fenghua Yu <[email protected]> 12 * H Peter Anvin" <[email protected]> 13 * (C) 2015 Borislav Petkov <[email protected]> 14 * 15 * This driver allows to upgrade microcode on x86 processors. 16 */ 17 18 #define pr_fmt(fmt) "microcode: " fmt 19 20 #include <linux/platform_device.h> 21 #include <linux/stop_machine.h> 22 #include <linux/syscore_ops.h> 23 #include <linux/miscdevice.h> 24 #include <linux/capability.h> 25 #include <linux/firmware.h> 26 #include <linux/kernel.h> 27 #include <linux/delay.h> 28 #include <linux/mutex.h> 29 #include <linux/cpu.h> 30 #include <linux/nmi.h> 31 #include <linux/fs.h> 32 #include <linux/mm.h> 33 34 #include <asm/cpu_device_id.h> 35 #include <asm/perf_event.h> 36 #include <asm/processor.h> 37 #include <asm/cmdline.h> 38 #include <asm/setup.h> 39 40 #include "internal.h" 41 42 #define DRIVER_VERSION "2.2" 43 44 static struct microcode_ops *microcode_ops; 45 bool dis_ucode_ldr = true; 46 47 bool initrd_gone; 48 49 /* 50 * Synchronization. 51 * 52 * All non cpu-hotplug-callback call sites use: 53 * 54 * - cpus_read_lock/unlock() to synchronize with 55 * the cpu-hotplug-callback call sites. 56 * 57 * We guarantee that only a single cpu is being 58 * updated at any particular moment of time. 59 */ 60 struct ucode_cpu_info ucode_cpu_info[NR_CPUS]; 61 62 struct cpu_info_ctx { 63 struct cpu_signature *cpu_sig; 64 int err; 65 }; 66 67 /* 68 * Those patch levels cannot be updated to newer ones and thus should be final. 69 */ 70 static u32 final_levels[] = { 71 0x01000098, 72 0x0100009f, 73 0x010000af, 74 0, /* T-101 terminator */ 75 }; 76 77 /* 78 * Check the current patch level on this CPU. 79 * 80 * Returns: 81 * - true: if update should stop 82 * - false: otherwise 83 */ 84 static bool amd_check_current_patch_level(void) 85 { 86 u32 lvl, dummy, i; 87 u32 *levels; 88 89 native_rdmsr(MSR_AMD64_PATCH_LEVEL, lvl, dummy); 90 91 levels = final_levels; 92 93 for (i = 0; levels[i]; i++) { 94 if (lvl == levels[i]) 95 return true; 96 } 97 return false; 98 } 99 100 static bool __init check_loader_disabled_bsp(void) 101 { 102 static const char *__dis_opt_str = "dis_ucode_ldr"; 103 const char *cmdline = boot_command_line; 104 const char *option = __dis_opt_str; 105 106 /* 107 * CPUID(1).ECX[31]: reserved for hypervisor use. This is still not 108 * completely accurate as xen pv guests don't see that CPUID bit set but 109 * that's good enough as they don't land on the BSP path anyway. 110 */ 111 if (native_cpuid_ecx(1) & BIT(31)) 112 return true; 113 114 if (x86_cpuid_vendor() == X86_VENDOR_AMD) { 115 if (amd_check_current_patch_level()) 116 return true; 117 } 118 119 if (cmdline_find_option_bool(cmdline, option) <= 0) 120 dis_ucode_ldr = false; 121 122 return dis_ucode_ldr; 123 } 124 125 void __init load_ucode_bsp(void) 126 { 127 unsigned int cpuid_1_eax; 128 bool intel = true; 129 130 if (!have_cpuid_p()) 131 return; 132 133 cpuid_1_eax = native_cpuid_eax(1); 134 135 switch (x86_cpuid_vendor()) { 136 case X86_VENDOR_INTEL: 137 if (x86_family(cpuid_1_eax) < 6) 138 return; 139 break; 140 141 case X86_VENDOR_AMD: 142 if (x86_family(cpuid_1_eax) < 0x10) 143 return; 144 intel = false; 145 break; 146 147 default: 148 return; 149 } 150 151 if (check_loader_disabled_bsp()) 152 return; 153 154 if (intel) 155 load_ucode_intel_bsp(); 156 else 157 load_ucode_amd_early(cpuid_1_eax); 158 } 159 160 void load_ucode_ap(void) 161 { 162 unsigned int cpuid_1_eax; 163 164 if (dis_ucode_ldr) 165 return; 166 167 cpuid_1_eax = native_cpuid_eax(1); 168 169 switch (x86_cpuid_vendor()) { 170 case X86_VENDOR_INTEL: 171 if (x86_family(cpuid_1_eax) >= 6) 172 load_ucode_intel_ap(); 173 break; 174 case X86_VENDOR_AMD: 175 if (x86_family(cpuid_1_eax) >= 0x10) 176 load_ucode_amd_early(cpuid_1_eax); 177 break; 178 default: 179 break; 180 } 181 } 182 183 static int __init save_microcode_in_initrd(void) 184 { 185 struct cpuinfo_x86 *c = &boot_cpu_data; 186 int ret = -EINVAL; 187 188 switch (c->x86_vendor) { 189 case X86_VENDOR_AMD: 190 if (c->x86 >= 0x10) 191 ret = save_microcode_in_initrd_amd(cpuid_eax(1)); 192 break; 193 default: 194 break; 195 } 196 197 initrd_gone = true; 198 199 return ret; 200 } 201 202 struct cpio_data find_microcode_in_initrd(const char *path) 203 { 204 #ifdef CONFIG_BLK_DEV_INITRD 205 unsigned long start = 0; 206 size_t size; 207 208 #ifdef CONFIG_X86_32 209 size = boot_params.hdr.ramdisk_size; 210 /* Early load on BSP has a temporary mapping. */ 211 if (size) 212 start = initrd_start_early; 213 214 #else /* CONFIG_X86_64 */ 215 size = (unsigned long)boot_params.ext_ramdisk_size << 32; 216 size |= boot_params.hdr.ramdisk_size; 217 218 if (size) { 219 start = (unsigned long)boot_params.ext_ramdisk_image << 32; 220 start |= boot_params.hdr.ramdisk_image; 221 start += PAGE_OFFSET; 222 } 223 #endif 224 225 /* 226 * Fixup the start address: after reserve_initrd() runs, initrd_start 227 * has the virtual address of the beginning of the initrd. It also 228 * possibly relocates the ramdisk. In either case, initrd_start contains 229 * the updated address so use that instead. 230 * 231 * initrd_gone is for the hotplug case where we've thrown out initrd 232 * already. 233 */ 234 if (initrd_gone) 235 return (struct cpio_data){ NULL, 0, "" }; 236 if (initrd_start) 237 start = initrd_start; 238 239 return find_cpio_data(path, (void *)start, size, NULL); 240 #else /* !CONFIG_BLK_DEV_INITRD */ 241 return (struct cpio_data){ NULL, 0, "" }; 242 #endif 243 } 244 245 static void reload_early_microcode(unsigned int cpu) 246 { 247 int vendor, family; 248 249 vendor = x86_cpuid_vendor(); 250 family = x86_cpuid_family(); 251 252 switch (vendor) { 253 case X86_VENDOR_INTEL: 254 if (family >= 6) 255 reload_ucode_intel(); 256 break; 257 case X86_VENDOR_AMD: 258 if (family >= 0x10) 259 reload_ucode_amd(cpu); 260 break; 261 default: 262 break; 263 } 264 } 265 266 /* fake device for request_firmware */ 267 static struct platform_device *microcode_pdev; 268 269 #ifdef CONFIG_MICROCODE_LATE_LOADING 270 /* 271 * Late loading dance. Why the heavy-handed stomp_machine effort? 272 * 273 * - HT siblings must be idle and not execute other code while the other sibling 274 * is loading microcode in order to avoid any negative interactions caused by 275 * the loading. 276 * 277 * - In addition, microcode update on the cores must be serialized until this 278 * requirement can be relaxed in the future. Right now, this is conservative 279 * and good. 280 */ 281 #define SPINUNIT 100 /* 100 nsec */ 282 283 static int check_online_cpus(void) 284 { 285 unsigned int cpu; 286 287 /* 288 * Make sure all CPUs are online. It's fine for SMT to be disabled if 289 * all the primary threads are still online. 290 */ 291 for_each_present_cpu(cpu) { 292 if (topology_is_primary_thread(cpu) && !cpu_online(cpu)) { 293 pr_err("Not all CPUs online, aborting microcode update.\n"); 294 return -EINVAL; 295 } 296 } 297 298 return 0; 299 } 300 301 static atomic_t late_cpus_in; 302 static atomic_t late_cpus_out; 303 304 static int __wait_for_cpus(atomic_t *t, long long timeout) 305 { 306 int all_cpus = num_online_cpus(); 307 308 atomic_inc(t); 309 310 while (atomic_read(t) < all_cpus) { 311 if (timeout < SPINUNIT) { 312 pr_err("Timeout while waiting for CPUs rendezvous, remaining: %d\n", 313 all_cpus - atomic_read(t)); 314 return 1; 315 } 316 317 ndelay(SPINUNIT); 318 timeout -= SPINUNIT; 319 320 touch_nmi_watchdog(); 321 } 322 return 0; 323 } 324 325 /* 326 * Returns: 327 * < 0 - on error 328 * 0 - success (no update done or microcode was updated) 329 */ 330 static int __reload_late(void *info) 331 { 332 int cpu = smp_processor_id(); 333 enum ucode_state err; 334 int ret = 0; 335 336 /* 337 * Wait for all CPUs to arrive. A load will not be attempted unless all 338 * CPUs show up. 339 * */ 340 if (__wait_for_cpus(&late_cpus_in, NSEC_PER_SEC)) 341 return -1; 342 343 /* 344 * On an SMT system, it suffices to load the microcode on one sibling of 345 * the core because the microcode engine is shared between the threads. 346 * Synchronization still needs to take place so that no concurrent 347 * loading attempts happen on multiple threads of an SMT core. See 348 * below. 349 */ 350 if (cpumask_first(topology_sibling_cpumask(cpu)) == cpu) 351 err = microcode_ops->apply_microcode(cpu); 352 else 353 goto wait_for_siblings; 354 355 if (err >= UCODE_NFOUND) { 356 if (err == UCODE_ERROR) { 357 pr_warn("Error reloading microcode on CPU %d\n", cpu); 358 ret = -1; 359 } 360 } 361 362 wait_for_siblings: 363 if (__wait_for_cpus(&late_cpus_out, NSEC_PER_SEC)) 364 panic("Timeout during microcode update!\n"); 365 366 /* 367 * At least one thread has completed update on each core. 368 * For others, simply call the update to make sure the 369 * per-cpu cpuinfo can be updated with right microcode 370 * revision. 371 */ 372 if (cpumask_first(topology_sibling_cpumask(cpu)) != cpu) 373 err = microcode_ops->apply_microcode(cpu); 374 375 return ret; 376 } 377 378 /* 379 * Reload microcode late on all CPUs. Wait for a sec until they 380 * all gather together. 381 */ 382 static int microcode_reload_late(void) 383 { 384 int old = boot_cpu_data.microcode, ret; 385 struct cpuinfo_x86 prev_info; 386 387 pr_err("Attempting late microcode loading - it is dangerous and taints the kernel.\n"); 388 pr_err("You should switch to early loading, if possible.\n"); 389 390 atomic_set(&late_cpus_in, 0); 391 atomic_set(&late_cpus_out, 0); 392 393 /* 394 * Take a snapshot before the microcode update in order to compare and 395 * check whether any bits changed after an update. 396 */ 397 store_cpu_caps(&prev_info); 398 399 ret = stop_machine_cpuslocked(__reload_late, NULL, cpu_online_mask); 400 if (!ret) { 401 pr_info("Reload succeeded, microcode revision: 0x%x -> 0x%x\n", 402 old, boot_cpu_data.microcode); 403 microcode_check(&prev_info); 404 } else { 405 pr_info("Reload failed, current microcode revision: 0x%x\n", 406 boot_cpu_data.microcode); 407 } 408 409 return ret; 410 } 411 412 static ssize_t reload_store(struct device *dev, 413 struct device_attribute *attr, 414 const char *buf, size_t size) 415 { 416 enum ucode_state tmp_ret = UCODE_OK; 417 int bsp = boot_cpu_data.cpu_index; 418 unsigned long val; 419 ssize_t ret = 0; 420 421 ret = kstrtoul(buf, 0, &val); 422 if (ret || val != 1) 423 return -EINVAL; 424 425 cpus_read_lock(); 426 427 ret = check_online_cpus(); 428 if (ret) 429 goto put; 430 431 tmp_ret = microcode_ops->request_microcode_fw(bsp, µcode_pdev->dev); 432 if (tmp_ret != UCODE_NEW) 433 goto put; 434 435 ret = microcode_reload_late(); 436 put: 437 cpus_read_unlock(); 438 439 if (ret == 0) 440 ret = size; 441 442 add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK); 443 444 return ret; 445 } 446 447 static DEVICE_ATTR_WO(reload); 448 #endif 449 450 static ssize_t version_show(struct device *dev, 451 struct device_attribute *attr, char *buf) 452 { 453 struct ucode_cpu_info *uci = ucode_cpu_info + dev->id; 454 455 return sprintf(buf, "0x%x\n", uci->cpu_sig.rev); 456 } 457 458 static ssize_t processor_flags_show(struct device *dev, 459 struct device_attribute *attr, char *buf) 460 { 461 struct ucode_cpu_info *uci = ucode_cpu_info + dev->id; 462 463 return sprintf(buf, "0x%x\n", uci->cpu_sig.pf); 464 } 465 466 static DEVICE_ATTR_RO(version); 467 static DEVICE_ATTR_RO(processor_flags); 468 469 static struct attribute *mc_default_attrs[] = { 470 &dev_attr_version.attr, 471 &dev_attr_processor_flags.attr, 472 NULL 473 }; 474 475 static const struct attribute_group mc_attr_group = { 476 .attrs = mc_default_attrs, 477 .name = "microcode", 478 }; 479 480 static void microcode_fini_cpu(int cpu) 481 { 482 if (microcode_ops->microcode_fini_cpu) 483 microcode_ops->microcode_fini_cpu(cpu); 484 } 485 486 static enum ucode_state microcode_init_cpu(int cpu) 487 { 488 struct ucode_cpu_info *uci = ucode_cpu_info + cpu; 489 490 memset(uci, 0, sizeof(*uci)); 491 492 microcode_ops->collect_cpu_info(cpu, &uci->cpu_sig); 493 494 return microcode_ops->apply_microcode(cpu); 495 } 496 497 /** 498 * microcode_bsp_resume - Update boot CPU microcode during resume. 499 */ 500 void microcode_bsp_resume(void) 501 { 502 int cpu = smp_processor_id(); 503 struct ucode_cpu_info *uci = ucode_cpu_info + cpu; 504 505 if (uci->mc) 506 microcode_ops->apply_microcode(cpu); 507 else 508 reload_early_microcode(cpu); 509 } 510 511 static struct syscore_ops mc_syscore_ops = { 512 .resume = microcode_bsp_resume, 513 }; 514 515 static int mc_cpu_starting(unsigned int cpu) 516 { 517 enum ucode_state err = microcode_ops->apply_microcode(cpu); 518 519 pr_debug("%s: CPU%d, err: %d\n", __func__, cpu, err); 520 521 return err == UCODE_ERROR; 522 } 523 524 static int mc_cpu_online(unsigned int cpu) 525 { 526 struct device *dev = get_cpu_device(cpu); 527 528 if (sysfs_create_group(&dev->kobj, &mc_attr_group)) 529 pr_err("Failed to create group for CPU%d\n", cpu); 530 return 0; 531 } 532 533 static int mc_cpu_down_prep(unsigned int cpu) 534 { 535 struct device *dev; 536 537 dev = get_cpu_device(cpu); 538 539 microcode_fini_cpu(cpu); 540 541 /* Suspend is in progress, only remove the interface */ 542 sysfs_remove_group(&dev->kobj, &mc_attr_group); 543 pr_debug("%s: CPU%d\n", __func__, cpu); 544 545 return 0; 546 } 547 548 static void setup_online_cpu(struct work_struct *work) 549 { 550 int cpu = smp_processor_id(); 551 enum ucode_state err; 552 553 err = microcode_init_cpu(cpu); 554 if (err == UCODE_ERROR) { 555 pr_err("Error applying microcode on CPU%d\n", cpu); 556 return; 557 } 558 559 mc_cpu_online(cpu); 560 } 561 562 static struct attribute *cpu_root_microcode_attrs[] = { 563 #ifdef CONFIG_MICROCODE_LATE_LOADING 564 &dev_attr_reload.attr, 565 #endif 566 NULL 567 }; 568 569 static const struct attribute_group cpu_root_microcode_group = { 570 .name = "microcode", 571 .attrs = cpu_root_microcode_attrs, 572 }; 573 574 static int __init microcode_init(void) 575 { 576 struct device *dev_root; 577 struct cpuinfo_x86 *c = &boot_cpu_data; 578 int error; 579 580 if (dis_ucode_ldr) 581 return -EINVAL; 582 583 if (c->x86_vendor == X86_VENDOR_INTEL) 584 microcode_ops = init_intel_microcode(); 585 else if (c->x86_vendor == X86_VENDOR_AMD) 586 microcode_ops = init_amd_microcode(); 587 else 588 pr_err("no support for this CPU vendor\n"); 589 590 if (!microcode_ops) 591 return -ENODEV; 592 593 microcode_pdev = platform_device_register_simple("microcode", -1, NULL, 0); 594 if (IS_ERR(microcode_pdev)) 595 return PTR_ERR(microcode_pdev); 596 597 dev_root = bus_get_dev_root(&cpu_subsys); 598 if (dev_root) { 599 error = sysfs_create_group(&dev_root->kobj, &cpu_root_microcode_group); 600 put_device(dev_root); 601 if (error) { 602 pr_err("Error creating microcode group!\n"); 603 goto out_pdev; 604 } 605 } 606 607 /* Do per-CPU setup */ 608 schedule_on_each_cpu(setup_online_cpu); 609 610 register_syscore_ops(&mc_syscore_ops); 611 cpuhp_setup_state_nocalls(CPUHP_AP_MICROCODE_LOADER, "x86/microcode:starting", 612 mc_cpu_starting, NULL); 613 cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "x86/microcode:online", 614 mc_cpu_online, mc_cpu_down_prep); 615 616 pr_info("Microcode Update Driver: v%s.", DRIVER_VERSION); 617 618 return 0; 619 620 out_pdev: 621 platform_device_unregister(microcode_pdev); 622 return error; 623 624 } 625 fs_initcall(save_microcode_in_initrd); 626 late_initcall(microcode_init); 627