.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
---|
1 | 2 | /* |
---|
2 | 3 | * CPU Microcode Update Driver for Linux |
---|
3 | 4 | * |
---|
.. | .. |
---|
12 | 13 | * (C) 2015 Borislav Petkov <bp@alien8.de> |
---|
13 | 14 | * |
---|
14 | 15 | * 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 | 16 | */ |
---|
21 | 17 | |
---|
22 | 18 | #define pr_fmt(fmt) "microcode: " fmt |
---|
.. | .. |
---|
59 | 55 | * All non cpu-hotplug-callback call sites use: |
---|
60 | 56 | * |
---|
61 | 57 | * - microcode_mutex to synchronize with each other; |
---|
62 | | - * - get/put_online_cpus() to synchronize with |
---|
| 58 | + * - cpus_read_lock/unlock() to synchronize with |
---|
63 | 59 | * the cpu-hotplug-callback call sites. |
---|
64 | 60 | * |
---|
65 | 61 | * We guarantee that only a single cpu is being |
---|
66 | 62 | * updated at any particular moment of time. |
---|
67 | 63 | */ |
---|
68 | 64 | static DEFINE_MUTEX(microcode_mutex); |
---|
69 | | - |
---|
70 | | -/* |
---|
71 | | - * Serialize late loading so that CPUs get updated one-by-one. |
---|
72 | | - */ |
---|
73 | | -static DEFINE_RAW_SPINLOCK(update_lock); |
---|
74 | 65 | |
---|
75 | 66 | struct ucode_cpu_info ucode_cpu_info[NR_CPUS]; |
---|
76 | 67 | |
---|
.. | .. |
---|
154 | 145 | |
---|
155 | 146 | bool get_builtin_firmware(struct cpio_data *cd, const char *name) |
---|
156 | 147 | { |
---|
157 | | -#ifdef CONFIG_FW_LOADER |
---|
158 | 148 | struct builtin_fw *b_fw; |
---|
159 | 149 | |
---|
160 | 150 | for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) { |
---|
.. | .. |
---|
164 | 154 | return true; |
---|
165 | 155 | } |
---|
166 | 156 | } |
---|
167 | | -#endif |
---|
168 | 157 | return false; |
---|
169 | 158 | } |
---|
170 | 159 | |
---|
.. | .. |
---|
326 | 315 | #endif |
---|
327 | 316 | } |
---|
328 | 317 | |
---|
329 | | -void reload_early_microcode(void) |
---|
| 318 | +void reload_early_microcode(unsigned int cpu) |
---|
330 | 319 | { |
---|
331 | 320 | int vendor, family; |
---|
332 | 321 | |
---|
.. | .. |
---|
340 | 329 | break; |
---|
341 | 330 | case X86_VENDOR_AMD: |
---|
342 | 331 | if (family >= 0x10) |
---|
343 | | - reload_ucode_amd(); |
---|
| 332 | + reload_ucode_amd(cpu); |
---|
344 | 333 | break; |
---|
345 | 334 | default: |
---|
346 | 335 | break; |
---|
.. | .. |
---|
401 | 390 | return ret; |
---|
402 | 391 | } |
---|
403 | 392 | |
---|
404 | | -#ifdef CONFIG_MICROCODE_OLD_INTERFACE |
---|
405 | | -static int do_microcode_update(const void __user *buf, size_t size) |
---|
406 | | -{ |
---|
407 | | - int error = 0; |
---|
408 | | - int cpu; |
---|
409 | | - |
---|
410 | | - for_each_online_cpu(cpu) { |
---|
411 | | - struct ucode_cpu_info *uci = ucode_cpu_info + cpu; |
---|
412 | | - enum ucode_state ustate; |
---|
413 | | - |
---|
414 | | - if (!uci->valid) |
---|
415 | | - continue; |
---|
416 | | - |
---|
417 | | - ustate = microcode_ops->request_microcode_user(cpu, buf, size); |
---|
418 | | - if (ustate == UCODE_ERROR) { |
---|
419 | | - error = -1; |
---|
420 | | - break; |
---|
421 | | - } else if (ustate == UCODE_NEW) { |
---|
422 | | - apply_microcode_on_target(cpu); |
---|
423 | | - } |
---|
424 | | - } |
---|
425 | | - |
---|
426 | | - return error; |
---|
427 | | -} |
---|
428 | | - |
---|
429 | | -static int microcode_open(struct inode *inode, struct file *file) |
---|
430 | | -{ |
---|
431 | | - return capable(CAP_SYS_RAWIO) ? nonseekable_open(inode, file) : -EPERM; |
---|
432 | | -} |
---|
433 | | - |
---|
434 | | -static ssize_t microcode_write(struct file *file, const char __user *buf, |
---|
435 | | - size_t len, loff_t *ppos) |
---|
436 | | -{ |
---|
437 | | - ssize_t ret = -EINVAL; |
---|
438 | | - |
---|
439 | | - if ((len >> PAGE_SHIFT) > totalram_pages) { |
---|
440 | | - pr_err("too much data (max %ld pages)\n", totalram_pages); |
---|
441 | | - return ret; |
---|
442 | | - } |
---|
443 | | - |
---|
444 | | - get_online_cpus(); |
---|
445 | | - mutex_lock(µcode_mutex); |
---|
446 | | - |
---|
447 | | - if (do_microcode_update(buf, len) == 0) |
---|
448 | | - ret = (ssize_t)len; |
---|
449 | | - |
---|
450 | | - if (ret > 0) |
---|
451 | | - perf_check_microcode(); |
---|
452 | | - |
---|
453 | | - mutex_unlock(µcode_mutex); |
---|
454 | | - put_online_cpus(); |
---|
455 | | - |
---|
456 | | - return ret; |
---|
457 | | -} |
---|
458 | | - |
---|
459 | | -static const struct file_operations microcode_fops = { |
---|
460 | | - .owner = THIS_MODULE, |
---|
461 | | - .write = microcode_write, |
---|
462 | | - .open = microcode_open, |
---|
463 | | - .llseek = no_llseek, |
---|
464 | | -}; |
---|
465 | | - |
---|
466 | | -static struct miscdevice microcode_dev = { |
---|
467 | | - .minor = MICROCODE_MINOR, |
---|
468 | | - .name = "microcode", |
---|
469 | | - .nodename = "cpu/microcode", |
---|
470 | | - .fops = µcode_fops, |
---|
471 | | -}; |
---|
472 | | - |
---|
473 | | -static int __init microcode_dev_init(void) |
---|
474 | | -{ |
---|
475 | | - int error; |
---|
476 | | - |
---|
477 | | - error = misc_register(µcode_dev); |
---|
478 | | - if (error) { |
---|
479 | | - pr_err("can't misc_register on minor=%d\n", MICROCODE_MINOR); |
---|
480 | | - return error; |
---|
481 | | - } |
---|
482 | | - |
---|
483 | | - return 0; |
---|
484 | | -} |
---|
485 | | - |
---|
486 | | -static void __exit microcode_dev_exit(void) |
---|
487 | | -{ |
---|
488 | | - misc_deregister(µcode_dev); |
---|
489 | | -} |
---|
490 | | -#else |
---|
491 | | -#define microcode_dev_init() 0 |
---|
492 | | -#define microcode_dev_exit() do { } while (0) |
---|
493 | | -#endif |
---|
494 | | - |
---|
495 | 393 | /* fake device for request_firmware */ |
---|
496 | 394 | static struct platform_device *microcode_pdev; |
---|
497 | 395 | |
---|
| 396 | +#ifdef CONFIG_MICROCODE_LATE_LOADING |
---|
498 | 397 | /* |
---|
499 | 398 | * Late loading dance. Why the heavy-handed stomp_machine effort? |
---|
500 | 399 | * |
---|
.. | .. |
---|
553 | 452 | /* |
---|
554 | 453 | * Returns: |
---|
555 | 454 | * < 0 - on error |
---|
556 | | - * 0 - no update done |
---|
557 | | - * 1 - microcode was updated |
---|
| 455 | + * 0 - success (no update done or microcode was updated) |
---|
558 | 456 | */ |
---|
559 | 457 | static int __reload_late(void *info) |
---|
560 | 458 | { |
---|
.. | .. |
---|
569 | 467 | if (__wait_for_cpus(&late_cpus_in, NSEC_PER_SEC)) |
---|
570 | 468 | return -1; |
---|
571 | 469 | |
---|
572 | | - raw_spin_lock(&update_lock); |
---|
573 | | - apply_microcode_local(&err); |
---|
574 | | - raw_spin_unlock(&update_lock); |
---|
| 470 | + /* |
---|
| 471 | + * On an SMT system, it suffices to load the microcode on one sibling of |
---|
| 472 | + * the core because the microcode engine is shared between the threads. |
---|
| 473 | + * Synchronization still needs to take place so that no concurrent |
---|
| 474 | + * loading attempts happen on multiple threads of an SMT core. See |
---|
| 475 | + * below. |
---|
| 476 | + */ |
---|
| 477 | + if (cpumask_first(topology_sibling_cpumask(cpu)) == cpu) |
---|
| 478 | + apply_microcode_local(&err); |
---|
| 479 | + else |
---|
| 480 | + goto wait_for_siblings; |
---|
575 | 481 | |
---|
576 | | - /* siblings return UCODE_OK because their engine got updated already */ |
---|
577 | | - if (err > UCODE_NFOUND) { |
---|
578 | | - pr_warn("Error reloading microcode on CPU %d\n", cpu); |
---|
| 482 | + if (err >= UCODE_NFOUND) { |
---|
| 483 | + if (err == UCODE_ERROR) |
---|
| 484 | + pr_warn("Error reloading microcode on CPU %d\n", cpu); |
---|
| 485 | + |
---|
579 | 486 | ret = -1; |
---|
580 | | - } else if (err == UCODE_UPDATED || err == UCODE_OK) { |
---|
581 | | - ret = 1; |
---|
582 | 487 | } |
---|
583 | 488 | |
---|
584 | | - /* |
---|
585 | | - * Increase the wait timeout to a safe value here since we're |
---|
586 | | - * serializing the microcode update and that could take a while on a |
---|
587 | | - * large number of CPUs. And that is fine as the *actual* timeout will |
---|
588 | | - * be determined by the last CPU finished updating and thus cut short. |
---|
589 | | - */ |
---|
590 | | - if (__wait_for_cpus(&late_cpus_out, NSEC_PER_SEC * num_online_cpus())) |
---|
| 489 | +wait_for_siblings: |
---|
| 490 | + if (__wait_for_cpus(&late_cpus_out, NSEC_PER_SEC)) |
---|
591 | 491 | panic("Timeout during microcode update!\n"); |
---|
| 492 | + |
---|
| 493 | + /* |
---|
| 494 | + * At least one thread has completed update on each core. |
---|
| 495 | + * For others, simply call the update to make sure the |
---|
| 496 | + * per-cpu cpuinfo can be updated with right microcode |
---|
| 497 | + * revision. |
---|
| 498 | + */ |
---|
| 499 | + if (cpumask_first(topology_sibling_cpumask(cpu)) != cpu) |
---|
| 500 | + apply_microcode_local(&err); |
---|
592 | 501 | |
---|
593 | 502 | return ret; |
---|
594 | 503 | } |
---|
.. | .. |
---|
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 | |
---|
| 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); |
---|
| 522 | + |
---|
607 | 523 | ret = stop_machine_cpuslocked(__reload_late, NULL, cpu_online_mask); |
---|
608 | | - if (ret > 0) |
---|
609 | | - microcode_check(); |
---|
| 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 | + } |
---|
610 | 532 | |
---|
611 | 533 | return ret; |
---|
612 | 534 | } |
---|
.. | .. |
---|
627 | 549 | if (val != 1) |
---|
628 | 550 | return size; |
---|
629 | 551 | |
---|
630 | | - get_online_cpus(); |
---|
| 552 | + cpus_read_lock(); |
---|
631 | 553 | |
---|
632 | 554 | ret = check_online_cpus(); |
---|
633 | 555 | if (ret) |
---|
.. | .. |
---|
642 | 564 | mutex_unlock(µcode_mutex); |
---|
643 | 565 | |
---|
644 | 566 | put: |
---|
645 | | - put_online_cpus(); |
---|
| 567 | + cpus_read_unlock(); |
---|
646 | 568 | |
---|
647 | | - if (ret >= 0) |
---|
| 569 | + if (ret == 0) |
---|
648 | 570 | ret = size; |
---|
649 | 571 | |
---|
650 | 572 | return ret; |
---|
651 | 573 | } |
---|
| 574 | + |
---|
| 575 | +static DEVICE_ATTR_WO(reload); |
---|
| 576 | +#endif |
---|
652 | 577 | |
---|
653 | 578 | static ssize_t version_show(struct device *dev, |
---|
654 | 579 | struct device_attribute *attr, char *buf) |
---|
.. | .. |
---|
666 | 591 | return sprintf(buf, "0x%x\n", uci->cpu_sig.pf); |
---|
667 | 592 | } |
---|
668 | 593 | |
---|
669 | | -static DEVICE_ATTR_WO(reload); |
---|
670 | | -static DEVICE_ATTR(version, 0400, version_show, NULL); |
---|
671 | | -static DEVICE_ATTR(processor_flags, 0400, pf_show, NULL); |
---|
| 594 | +static DEVICE_ATTR(version, 0444, version_show, NULL); |
---|
| 595 | +static DEVICE_ATTR(processor_flags, 0444, pf_show, NULL); |
---|
672 | 596 | |
---|
673 | 597 | static struct attribute *mc_default_attrs[] = { |
---|
674 | 598 | &dev_attr_version.attr, |
---|
.. | .. |
---|
773 | 697 | }; |
---|
774 | 698 | |
---|
775 | 699 | /** |
---|
776 | | - * mc_bp_resume - Update boot CPU microcode during resume. |
---|
| 700 | + * microcode_bsp_resume - Update boot CPU microcode during resume. |
---|
777 | 701 | */ |
---|
778 | | -static void mc_bp_resume(void) |
---|
| 702 | +void microcode_bsp_resume(void) |
---|
779 | 703 | { |
---|
780 | 704 | int cpu = smp_processor_id(); |
---|
781 | 705 | struct ucode_cpu_info *uci = ucode_cpu_info + cpu; |
---|
.. | .. |
---|
783 | 707 | if (uci->valid && uci->mc) |
---|
784 | 708 | microcode_ops->apply_microcode(cpu); |
---|
785 | 709 | else if (!uci->mc) |
---|
786 | | - reload_early_microcode(); |
---|
| 710 | + reload_early_microcode(cpu); |
---|
787 | 711 | } |
---|
788 | 712 | |
---|
789 | 713 | static struct syscore_ops mc_syscore_ops = { |
---|
790 | | - .resume = mc_bp_resume, |
---|
| 714 | + .resume = microcode_bsp_resume, |
---|
791 | 715 | }; |
---|
792 | 716 | |
---|
793 | 717 | static int mc_cpu_starting(unsigned int cpu) |
---|
.. | .. |
---|
819 | 743 | } |
---|
820 | 744 | |
---|
821 | 745 | static struct attribute *cpu_root_microcode_attrs[] = { |
---|
| 746 | +#ifdef CONFIG_MICROCODE_LATE_LOADING |
---|
822 | 747 | &dev_attr_reload.attr, |
---|
| 748 | +#endif |
---|
823 | 749 | NULL |
---|
824 | 750 | }; |
---|
825 | 751 | |
---|
.. | .. |
---|
851 | 777 | if (IS_ERR(microcode_pdev)) |
---|
852 | 778 | return PTR_ERR(microcode_pdev); |
---|
853 | 779 | |
---|
854 | | - get_online_cpus(); |
---|
| 780 | + cpus_read_lock(); |
---|
855 | 781 | mutex_lock(µcode_mutex); |
---|
856 | 782 | |
---|
857 | 783 | error = subsys_interface_register(&mc_cpu_interface); |
---|
858 | 784 | if (!error) |
---|
859 | 785 | perf_check_microcode(); |
---|
860 | 786 | mutex_unlock(µcode_mutex); |
---|
861 | | - put_online_cpus(); |
---|
| 787 | + cpus_read_unlock(); |
---|
862 | 788 | |
---|
863 | 789 | if (error) |
---|
864 | 790 | goto out_pdev; |
---|
.. | .. |
---|
871 | 797 | goto out_driver; |
---|
872 | 798 | } |
---|
873 | 799 | |
---|
874 | | - error = microcode_dev_init(); |
---|
875 | | - if (error) |
---|
876 | | - goto out_ucode_group; |
---|
877 | | - |
---|
878 | 800 | register_syscore_ops(&mc_syscore_ops); |
---|
879 | 801 | cpuhp_setup_state_nocalls(CPUHP_AP_MICROCODE_LOADER, "x86/microcode:starting", |
---|
880 | 802 | mc_cpu_starting, NULL); |
---|
.. | .. |
---|
885 | 807 | |
---|
886 | 808 | return 0; |
---|
887 | 809 | |
---|
888 | | - out_ucode_group: |
---|
889 | | - sysfs_remove_group(&cpu_subsys.dev_root->kobj, |
---|
890 | | - &cpu_root_microcode_group); |
---|
891 | | - |
---|
892 | 810 | out_driver: |
---|
893 | | - get_online_cpus(); |
---|
| 811 | + cpus_read_lock(); |
---|
894 | 812 | mutex_lock(µcode_mutex); |
---|
895 | 813 | |
---|
896 | 814 | subsys_interface_unregister(&mc_cpu_interface); |
---|
897 | 815 | |
---|
898 | 816 | mutex_unlock(µcode_mutex); |
---|
899 | | - put_online_cpus(); |
---|
| 817 | + cpus_read_unlock(); |
---|
900 | 818 | |
---|
901 | 819 | out_pdev: |
---|
902 | 820 | platform_device_unregister(microcode_pdev); |
---|