| .. | .. |
|---|
| 55 | 55 | * All non cpu-hotplug-callback call sites use: |
|---|
| 56 | 56 | * |
|---|
| 57 | 57 | * - microcode_mutex to synchronize with each other; |
|---|
| 58 | | - * - get/put_online_cpus() to synchronize with |
|---|
| 58 | + * - cpus_read_lock/unlock() to synchronize with |
|---|
| 59 | 59 | * the cpu-hotplug-callback call sites. |
|---|
| 60 | 60 | * |
|---|
| 61 | 61 | * We guarantee that only a single cpu is being |
|---|
| .. | .. |
|---|
| 315 | 315 | #endif |
|---|
| 316 | 316 | } |
|---|
| 317 | 317 | |
|---|
| 318 | | -void reload_early_microcode(void) |
|---|
| 318 | +void reload_early_microcode(unsigned int cpu) |
|---|
| 319 | 319 | { |
|---|
| 320 | 320 | int vendor, family; |
|---|
| 321 | 321 | |
|---|
| .. | .. |
|---|
| 329 | 329 | break; |
|---|
| 330 | 330 | case X86_VENDOR_AMD: |
|---|
| 331 | 331 | if (family >= 0x10) |
|---|
| 332 | | - reload_ucode_amd(); |
|---|
| 332 | + reload_ucode_amd(cpu); |
|---|
| 333 | 333 | break; |
|---|
| 334 | 334 | default: |
|---|
| 335 | 335 | break; |
|---|
| .. | .. |
|---|
| 390 | 390 | return ret; |
|---|
| 391 | 391 | } |
|---|
| 392 | 392 | |
|---|
| 393 | | -#ifdef CONFIG_MICROCODE_OLD_INTERFACE |
|---|
| 394 | | -static int do_microcode_update(const void __user *buf, size_t size) |
|---|
| 395 | | -{ |
|---|
| 396 | | - int error = 0; |
|---|
| 397 | | - int cpu; |
|---|
| 398 | | - |
|---|
| 399 | | - for_each_online_cpu(cpu) { |
|---|
| 400 | | - struct ucode_cpu_info *uci = ucode_cpu_info + cpu; |
|---|
| 401 | | - enum ucode_state ustate; |
|---|
| 402 | | - |
|---|
| 403 | | - if (!uci->valid) |
|---|
| 404 | | - continue; |
|---|
| 405 | | - |
|---|
| 406 | | - ustate = microcode_ops->request_microcode_user(cpu, buf, size); |
|---|
| 407 | | - if (ustate == UCODE_ERROR) { |
|---|
| 408 | | - error = -1; |
|---|
| 409 | | - break; |
|---|
| 410 | | - } else if (ustate == UCODE_NEW) { |
|---|
| 411 | | - apply_microcode_on_target(cpu); |
|---|
| 412 | | - } |
|---|
| 413 | | - } |
|---|
| 414 | | - |
|---|
| 415 | | - return error; |
|---|
| 416 | | -} |
|---|
| 417 | | - |
|---|
| 418 | | -static int microcode_open(struct inode *inode, struct file *file) |
|---|
| 419 | | -{ |
|---|
| 420 | | - return capable(CAP_SYS_RAWIO) ? stream_open(inode, file) : -EPERM; |
|---|
| 421 | | -} |
|---|
| 422 | | - |
|---|
| 423 | | -static ssize_t microcode_write(struct file *file, const char __user *buf, |
|---|
| 424 | | - size_t len, loff_t *ppos) |
|---|
| 425 | | -{ |
|---|
| 426 | | - ssize_t ret = -EINVAL; |
|---|
| 427 | | - unsigned long nr_pages = totalram_pages(); |
|---|
| 428 | | - |
|---|
| 429 | | - if ((len >> PAGE_SHIFT) > nr_pages) { |
|---|
| 430 | | - pr_err("too much data (max %ld pages)\n", nr_pages); |
|---|
| 431 | | - return ret; |
|---|
| 432 | | - } |
|---|
| 433 | | - |
|---|
| 434 | | - get_online_cpus(); |
|---|
| 435 | | - mutex_lock(µcode_mutex); |
|---|
| 436 | | - |
|---|
| 437 | | - if (do_microcode_update(buf, len) == 0) |
|---|
| 438 | | - ret = (ssize_t)len; |
|---|
| 439 | | - |
|---|
| 440 | | - if (ret > 0) |
|---|
| 441 | | - perf_check_microcode(); |
|---|
| 442 | | - |
|---|
| 443 | | - mutex_unlock(µcode_mutex); |
|---|
| 444 | | - put_online_cpus(); |
|---|
| 445 | | - |
|---|
| 446 | | - return ret; |
|---|
| 447 | | -} |
|---|
| 448 | | - |
|---|
| 449 | | -static const struct file_operations microcode_fops = { |
|---|
| 450 | | - .owner = THIS_MODULE, |
|---|
| 451 | | - .write = microcode_write, |
|---|
| 452 | | - .open = microcode_open, |
|---|
| 453 | | - .llseek = no_llseek, |
|---|
| 454 | | -}; |
|---|
| 455 | | - |
|---|
| 456 | | -static struct miscdevice microcode_dev = { |
|---|
| 457 | | - .minor = MICROCODE_MINOR, |
|---|
| 458 | | - .name = "microcode", |
|---|
| 459 | | - .nodename = "cpu/microcode", |
|---|
| 460 | | - .fops = µcode_fops, |
|---|
| 461 | | -}; |
|---|
| 462 | | - |
|---|
| 463 | | -static int __init microcode_dev_init(void) |
|---|
| 464 | | -{ |
|---|
| 465 | | - int error; |
|---|
| 466 | | - |
|---|
| 467 | | - error = misc_register(µcode_dev); |
|---|
| 468 | | - if (error) { |
|---|
| 469 | | - pr_err("can't misc_register on minor=%d\n", MICROCODE_MINOR); |
|---|
| 470 | | - return error; |
|---|
| 471 | | - } |
|---|
| 472 | | - |
|---|
| 473 | | - return 0; |
|---|
| 474 | | -} |
|---|
| 475 | | - |
|---|
| 476 | | -static void __exit microcode_dev_exit(void) |
|---|
| 477 | | -{ |
|---|
| 478 | | - misc_deregister(µcode_dev); |
|---|
| 479 | | -} |
|---|
| 480 | | -#else |
|---|
| 481 | | -#define microcode_dev_init() 0 |
|---|
| 482 | | -#define microcode_dev_exit() do { } while (0) |
|---|
| 483 | | -#endif |
|---|
| 484 | | - |
|---|
| 485 | 393 | /* fake device for request_firmware */ |
|---|
| 486 | 394 | static struct platform_device *microcode_pdev; |
|---|
| 487 | 395 | |
|---|
| 396 | +#ifdef CONFIG_MICROCODE_LATE_LOADING |
|---|
| 488 | 397 | /* |
|---|
| 489 | 398 | * Late loading dance. Why the heavy-handed stomp_machine effort? |
|---|
| 490 | 399 | * |
|---|
| .. | .. |
|---|
| 599 | 508 | */ |
|---|
| 600 | 509 | static int microcode_reload_late(void) |
|---|
| 601 | 510 | { |
|---|
| 602 | | - int ret; |
|---|
| 511 | + int old = boot_cpu_data.microcode, ret; |
|---|
| 512 | + struct cpuinfo_x86 prev_info; |
|---|
| 603 | 513 | |
|---|
| 604 | 514 | atomic_set(&late_cpus_in, 0); |
|---|
| 605 | 515 | atomic_set(&late_cpus_out, 0); |
|---|
| 606 | 516 | |
|---|
| 607 | | - ret = stop_machine_cpuslocked(__reload_late, NULL, cpu_online_mask); |
|---|
| 608 | | - if (ret == 0) |
|---|
| 609 | | - microcode_check(); |
|---|
| 517 | + /* |
|---|
| 518 | + * Take a snapshot before the microcode update in order to compare and |
|---|
| 519 | + * check whether any bits changed after an update. |
|---|
| 520 | + */ |
|---|
| 521 | + store_cpu_caps(&prev_info); |
|---|
| 610 | 522 | |
|---|
| 611 | | - pr_info("Reload completed, microcode revision: 0x%x\n", boot_cpu_data.microcode); |
|---|
| 523 | + ret = stop_machine_cpuslocked(__reload_late, NULL, cpu_online_mask); |
|---|
| 524 | + if (!ret) { |
|---|
| 525 | + pr_info("Reload succeeded, microcode revision: 0x%x -> 0x%x\n", |
|---|
| 526 | + old, boot_cpu_data.microcode); |
|---|
| 527 | + microcode_check(&prev_info); |
|---|
| 528 | + } else { |
|---|
| 529 | + pr_info("Reload failed, current microcode revision: 0x%x\n", |
|---|
| 530 | + boot_cpu_data.microcode); |
|---|
| 531 | + } |
|---|
| 612 | 532 | |
|---|
| 613 | 533 | return ret; |
|---|
| 614 | 534 | } |
|---|
| .. | .. |
|---|
| 629 | 549 | if (val != 1) |
|---|
| 630 | 550 | return size; |
|---|
| 631 | 551 | |
|---|
| 632 | | - get_online_cpus(); |
|---|
| 552 | + cpus_read_lock(); |
|---|
| 633 | 553 | |
|---|
| 634 | 554 | ret = check_online_cpus(); |
|---|
| 635 | 555 | if (ret) |
|---|
| .. | .. |
|---|
| 644 | 564 | mutex_unlock(µcode_mutex); |
|---|
| 645 | 565 | |
|---|
| 646 | 566 | put: |
|---|
| 647 | | - put_online_cpus(); |
|---|
| 567 | + cpus_read_unlock(); |
|---|
| 648 | 568 | |
|---|
| 649 | 569 | if (ret == 0) |
|---|
| 650 | 570 | ret = size; |
|---|
| 651 | 571 | |
|---|
| 652 | 572 | return ret; |
|---|
| 653 | 573 | } |
|---|
| 574 | + |
|---|
| 575 | +static DEVICE_ATTR_WO(reload); |
|---|
| 576 | +#endif |
|---|
| 654 | 577 | |
|---|
| 655 | 578 | static ssize_t version_show(struct device *dev, |
|---|
| 656 | 579 | struct device_attribute *attr, char *buf) |
|---|
| .. | .. |
|---|
| 668 | 591 | return sprintf(buf, "0x%x\n", uci->cpu_sig.pf); |
|---|
| 669 | 592 | } |
|---|
| 670 | 593 | |
|---|
| 671 | | -static DEVICE_ATTR_WO(reload); |
|---|
| 672 | 594 | static DEVICE_ATTR(version, 0444, version_show, NULL); |
|---|
| 673 | 595 | static DEVICE_ATTR(processor_flags, 0444, pf_show, NULL); |
|---|
| 674 | 596 | |
|---|
| .. | .. |
|---|
| 785 | 707 | if (uci->valid && uci->mc) |
|---|
| 786 | 708 | microcode_ops->apply_microcode(cpu); |
|---|
| 787 | 709 | else if (!uci->mc) |
|---|
| 788 | | - reload_early_microcode(); |
|---|
| 710 | + reload_early_microcode(cpu); |
|---|
| 789 | 711 | } |
|---|
| 790 | 712 | |
|---|
| 791 | 713 | static struct syscore_ops mc_syscore_ops = { |
|---|
| .. | .. |
|---|
| 821 | 743 | } |
|---|
| 822 | 744 | |
|---|
| 823 | 745 | static struct attribute *cpu_root_microcode_attrs[] = { |
|---|
| 746 | +#ifdef CONFIG_MICROCODE_LATE_LOADING |
|---|
| 824 | 747 | &dev_attr_reload.attr, |
|---|
| 748 | +#endif |
|---|
| 825 | 749 | NULL |
|---|
| 826 | 750 | }; |
|---|
| 827 | 751 | |
|---|
| .. | .. |
|---|
| 853 | 777 | if (IS_ERR(microcode_pdev)) |
|---|
| 854 | 778 | return PTR_ERR(microcode_pdev); |
|---|
| 855 | 779 | |
|---|
| 856 | | - get_online_cpus(); |
|---|
| 780 | + cpus_read_lock(); |
|---|
| 857 | 781 | mutex_lock(µcode_mutex); |
|---|
| 858 | 782 | |
|---|
| 859 | 783 | error = subsys_interface_register(&mc_cpu_interface); |
|---|
| 860 | 784 | if (!error) |
|---|
| 861 | 785 | perf_check_microcode(); |
|---|
| 862 | 786 | mutex_unlock(µcode_mutex); |
|---|
| 863 | | - put_online_cpus(); |
|---|
| 787 | + cpus_read_unlock(); |
|---|
| 864 | 788 | |
|---|
| 865 | 789 | if (error) |
|---|
| 866 | 790 | goto out_pdev; |
|---|
| .. | .. |
|---|
| 873 | 797 | goto out_driver; |
|---|
| 874 | 798 | } |
|---|
| 875 | 799 | |
|---|
| 876 | | - error = microcode_dev_init(); |
|---|
| 877 | | - if (error) |
|---|
| 878 | | - goto out_ucode_group; |
|---|
| 879 | | - |
|---|
| 880 | 800 | register_syscore_ops(&mc_syscore_ops); |
|---|
| 881 | 801 | cpuhp_setup_state_nocalls(CPUHP_AP_MICROCODE_LOADER, "x86/microcode:starting", |
|---|
| 882 | 802 | mc_cpu_starting, NULL); |
|---|
| .. | .. |
|---|
| 887 | 807 | |
|---|
| 888 | 808 | return 0; |
|---|
| 889 | 809 | |
|---|
| 890 | | - out_ucode_group: |
|---|
| 891 | | - sysfs_remove_group(&cpu_subsys.dev_root->kobj, |
|---|
| 892 | | - &cpu_root_microcode_group); |
|---|
| 893 | | - |
|---|
| 894 | 810 | out_driver: |
|---|
| 895 | | - get_online_cpus(); |
|---|
| 811 | + cpus_read_lock(); |
|---|
| 896 | 812 | mutex_lock(µcode_mutex); |
|---|
| 897 | 813 | |
|---|
| 898 | 814 | subsys_interface_unregister(&mc_cpu_interface); |
|---|
| 899 | 815 | |
|---|
| 900 | 816 | mutex_unlock(µcode_mutex); |
|---|
| 901 | | - put_online_cpus(); |
|---|
| 817 | + cpus_read_unlock(); |
|---|
| 902 | 818 | |
|---|
| 903 | 819 | out_pdev: |
|---|
| 904 | 820 | platform_device_unregister(microcode_pdev); |
|---|