1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * CPU Microcode Update Driver for Linux
5 * Copyright (C) 2000-2006 Tigran Aivazian <aivazian.tigran@gmail.com>
6 * 2006 Shaohua Li <shaohua.li@intel.com>
7 * 2013-2016 Borislav Petkov <bp@alien8.de>
9 * X86 CPU microcode early update for Linux:
11 * Copyright (C) 2012 Fenghua Yu <fenghua.yu@intel.com>
12 * H Peter Anvin" <hpa@zytor.com>
13 * (C) 2015 Borislav Petkov <bp@alien8.de>
15 * This driver allows to upgrade microcode on x86 processors.
18 #define pr_fmt(fmt) "microcode: " fmt
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>
34 #include <asm/microcode_intel.h>
35 #include <asm/cpu_device_id.h>
36 #include <asm/microcode_amd.h>
37 #include <asm/perf_event.h>
38 #include <asm/microcode.h>
39 #include <asm/processor.h>
40 #include <asm/cmdline.h>
41 #include <asm/setup.h>
43 #define DRIVER_VERSION "2.2"
45 static struct microcode_ops
*microcode_ops
;
46 static bool dis_ucode_ldr
= true;
50 LIST_HEAD(microcode_cache
);
55 * All non cpu-hotplug-callback call sites use:
57 * - microcode_mutex to synchronize with each other;
58 * - get/put_online_cpus() to synchronize with
59 * the cpu-hotplug-callback call sites.
61 * We guarantee that only a single cpu is being
62 * updated at any particular moment of time.
64 static DEFINE_MUTEX(microcode_mutex
);
67 * Serialize late loading so that CPUs get updated one-by-one.
69 static DEFINE_RAW_SPINLOCK(update_lock
);
71 struct ucode_cpu_info ucode_cpu_info
[NR_CPUS
];
74 struct cpu_signature
*cpu_sig
;
79 * Those patch levels cannot be updated to newer ones and thus should be final.
81 static u32 final_levels
[] = {
85 0, /* T-101 terminator */
89 * Check the current patch level on this CPU.
92 * - true: if update should stop
95 static bool amd_check_current_patch_level(void)
100 native_rdmsr(MSR_AMD64_PATCH_LEVEL
, lvl
, dummy
);
102 if (IS_ENABLED(CONFIG_X86_32
))
103 levels
= (u32
*)__pa_nodebug(&final_levels
);
105 levels
= final_levels
;
107 for (i
= 0; levels
[i
]; i
++) {
108 if (lvl
== levels
[i
])
114 static bool __init
check_loader_disabled_bsp(void)
116 static const char *__dis_opt_str
= "dis_ucode_ldr";
119 const char *cmdline
= (const char *)__pa_nodebug(boot_command_line
);
120 const char *option
= (const char *)__pa_nodebug(__dis_opt_str
);
121 bool *res
= (bool *)__pa_nodebug(&dis_ucode_ldr
);
123 #else /* CONFIG_X86_64 */
124 const char *cmdline
= boot_command_line
;
125 const char *option
= __dis_opt_str
;
126 bool *res
= &dis_ucode_ldr
;
130 * CPUID(1).ECX[31]: reserved for hypervisor use. This is still not
131 * completely accurate as xen pv guests don't see that CPUID bit set but
132 * that's good enough as they don't land on the BSP path anyway.
134 if (native_cpuid_ecx(1) & BIT(31))
137 if (x86_cpuid_vendor() == X86_VENDOR_AMD
) {
138 if (amd_check_current_patch_level())
142 if (cmdline_find_option_bool(cmdline
, option
) <= 0)
148 extern struct builtin_fw __start_builtin_fw
[];
149 extern struct builtin_fw __end_builtin_fw
[];
151 bool get_builtin_firmware(struct cpio_data
*cd
, const char *name
)
153 #ifdef CONFIG_FW_LOADER
154 struct builtin_fw
*b_fw
;
156 for (b_fw
= __start_builtin_fw
; b_fw
!= __end_builtin_fw
; b_fw
++) {
157 if (!strcmp(name
, b_fw
->name
)) {
158 cd
->size
= b_fw
->size
;
159 cd
->data
= b_fw
->data
;
167 void __init
load_ucode_bsp(void)
169 unsigned int cpuid_1_eax
;
175 cpuid_1_eax
= native_cpuid_eax(1);
177 switch (x86_cpuid_vendor()) {
178 case X86_VENDOR_INTEL
:
179 if (x86_family(cpuid_1_eax
) < 6)
184 if (x86_family(cpuid_1_eax
) < 0x10)
193 if (check_loader_disabled_bsp())
197 load_ucode_intel_bsp();
199 load_ucode_amd_bsp(cpuid_1_eax
);
202 static bool check_loader_disabled_ap(void)
205 return *((bool *)__pa_nodebug(&dis_ucode_ldr
));
207 return dis_ucode_ldr
;
211 void load_ucode_ap(void)
213 unsigned int cpuid_1_eax
;
215 if (check_loader_disabled_ap())
218 cpuid_1_eax
= native_cpuid_eax(1);
220 switch (x86_cpuid_vendor()) {
221 case X86_VENDOR_INTEL
:
222 if (x86_family(cpuid_1_eax
) >= 6)
223 load_ucode_intel_ap();
226 if (x86_family(cpuid_1_eax
) >= 0x10)
227 load_ucode_amd_ap(cpuid_1_eax
);
234 static int __init
save_microcode_in_initrd(void)
236 struct cpuinfo_x86
*c
= &boot_cpu_data
;
239 switch (c
->x86_vendor
) {
240 case X86_VENDOR_INTEL
:
242 ret
= save_microcode_in_initrd_intel();
246 ret
= save_microcode_in_initrd_amd(cpuid_eax(1));
257 struct cpio_data
find_microcode_in_initrd(const char *path
, bool use_pa
)
259 #ifdef CONFIG_BLK_DEV_INITRD
260 unsigned long start
= 0;
264 struct boot_params
*params
;
267 params
= (struct boot_params
*)__pa_nodebug(&boot_params
);
269 params
= &boot_params
;
271 size
= params
->hdr
.ramdisk_size
;
274 * Set start only if we have an initrd image. We cannot use initrd_start
275 * because it is not set that early yet.
278 start
= params
->hdr
.ramdisk_image
;
280 # else /* CONFIG_X86_64 */
281 size
= (unsigned long)boot_params
.ext_ramdisk_size
<< 32;
282 size
|= boot_params
.hdr
.ramdisk_size
;
285 start
= (unsigned long)boot_params
.ext_ramdisk_image
<< 32;
286 start
|= boot_params
.hdr
.ramdisk_image
;
288 start
+= PAGE_OFFSET
;
293 * Fixup the start address: after reserve_initrd() runs, initrd_start
294 * has the virtual address of the beginning of the initrd. It also
295 * possibly relocates the ramdisk. In either case, initrd_start contains
296 * the updated address so use that instead.
298 * initrd_gone is for the hotplug case where we've thrown out initrd
303 return (struct cpio_data
){ NULL
, 0, "" };
305 start
= initrd_start
;
308 * The picture with physical addresses is a bit different: we
309 * need to get the *physical* address to which the ramdisk was
310 * relocated, i.e., relocated_ramdisk (not initrd_start) and
311 * since we're running from physical addresses, we need to access
312 * relocated_ramdisk through its *physical* address too.
314 u64
*rr
= (u64
*)__pa_nodebug(&relocated_ramdisk
);
319 return find_cpio_data(path
, (void *)start
, size
, NULL
);
320 #else /* !CONFIG_BLK_DEV_INITRD */
321 return (struct cpio_data
){ NULL
, 0, "" };
325 void reload_early_microcode(void)
329 vendor
= x86_cpuid_vendor();
330 family
= x86_cpuid_family();
333 case X86_VENDOR_INTEL
:
335 reload_ucode_intel();
346 static void collect_cpu_info_local(void *arg
)
348 struct cpu_info_ctx
*ctx
= arg
;
350 ctx
->err
= microcode_ops
->collect_cpu_info(smp_processor_id(),
354 static int collect_cpu_info_on_target(int cpu
, struct cpu_signature
*cpu_sig
)
356 struct cpu_info_ctx ctx
= { .cpu_sig
= cpu_sig
, .err
= 0 };
359 ret
= smp_call_function_single(cpu
, collect_cpu_info_local
, &ctx
, 1);
366 static int collect_cpu_info(int cpu
)
368 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ cpu
;
371 memset(uci
, 0, sizeof(*uci
));
373 ret
= collect_cpu_info_on_target(cpu
, &uci
->cpu_sig
);
380 static void apply_microcode_local(void *arg
)
382 enum ucode_state
*err
= arg
;
384 *err
= microcode_ops
->apply_microcode(smp_processor_id());
387 static int apply_microcode_on_target(int cpu
)
389 enum ucode_state err
;
392 ret
= smp_call_function_single(cpu
, apply_microcode_local
, &err
, 1);
394 if (err
== UCODE_ERROR
)
400 #ifdef CONFIG_MICROCODE_OLD_INTERFACE
401 static int do_microcode_update(const void __user
*buf
, size_t size
)
406 for_each_online_cpu(cpu
) {
407 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ cpu
;
408 enum ucode_state ustate
;
413 ustate
= microcode_ops
->request_microcode_user(cpu
, buf
, size
);
414 if (ustate
== UCODE_ERROR
) {
417 } else if (ustate
== UCODE_NEW
) {
418 apply_microcode_on_target(cpu
);
425 static int microcode_open(struct inode
*inode
, struct file
*file
)
427 return capable(CAP_SYS_RAWIO
) ? stream_open(inode
, file
) : -EPERM
;
430 static ssize_t
microcode_write(struct file
*file
, const char __user
*buf
,
431 size_t len
, loff_t
*ppos
)
433 ssize_t ret
= -EINVAL
;
434 unsigned long nr_pages
= totalram_pages();
436 if ((len
>> PAGE_SHIFT
) > nr_pages
) {
437 pr_err("too much data (max %ld pages)\n", nr_pages
);
442 mutex_lock(µcode_mutex
);
444 if (do_microcode_update(buf
, len
) == 0)
448 perf_check_microcode();
450 mutex_unlock(µcode_mutex
);
456 static const struct file_operations microcode_fops
= {
457 .owner
= THIS_MODULE
,
458 .write
= microcode_write
,
459 .open
= microcode_open
,
463 static struct miscdevice microcode_dev
= {
464 .minor
= MICROCODE_MINOR
,
466 .nodename
= "cpu/microcode",
467 .fops
= µcode_fops
,
470 static int __init
microcode_dev_init(void)
474 error
= misc_register(µcode_dev
);
476 pr_err("can't misc_register on minor=%d\n", MICROCODE_MINOR
);
483 static void __exit
microcode_dev_exit(void)
485 misc_deregister(µcode_dev
);
488 #define microcode_dev_init() 0
489 #define microcode_dev_exit() do { } while (0)
492 /* fake device for request_firmware */
493 static struct platform_device
*microcode_pdev
;
496 * Late loading dance. Why the heavy-handed stomp_machine effort?
498 * - HT siblings must be idle and not execute other code while the other sibling
499 * is loading microcode in order to avoid any negative interactions caused by
502 * - In addition, microcode update on the cores must be serialized until this
503 * requirement can be relaxed in the future. Right now, this is conservative
506 #define SPINUNIT 100 /* 100 nsec */
508 static int check_online_cpus(void)
513 * Make sure all CPUs are online. It's fine for SMT to be disabled if
514 * all the primary threads are still online.
516 for_each_present_cpu(cpu
) {
517 if (topology_is_primary_thread(cpu
) && !cpu_online(cpu
)) {
518 pr_err("Not all CPUs online, aborting microcode update.\n");
526 static atomic_t late_cpus_in
;
527 static atomic_t late_cpus_out
;
529 static int __wait_for_cpus(atomic_t
*t
, long long timeout
)
531 int all_cpus
= num_online_cpus();
535 while (atomic_read(t
) < all_cpus
) {
536 if (timeout
< SPINUNIT
) {
537 pr_err("Timeout while waiting for CPUs rendezvous, remaining: %d\n",
538 all_cpus
- atomic_read(t
));
545 touch_nmi_watchdog();
554 * 1 - microcode was updated
556 static int __reload_late(void *info
)
558 int cpu
= smp_processor_id();
559 enum ucode_state err
;
563 * Wait for all CPUs to arrive. A load will not be attempted unless all
566 if (__wait_for_cpus(&late_cpus_in
, NSEC_PER_SEC
))
569 raw_spin_lock(&update_lock
);
570 apply_microcode_local(&err
);
571 raw_spin_unlock(&update_lock
);
573 /* siblings return UCODE_OK because their engine got updated already */
574 if (err
> UCODE_NFOUND
) {
575 pr_warn("Error reloading microcode on CPU %d\n", cpu
);
577 } else if (err
== UCODE_UPDATED
|| err
== UCODE_OK
) {
582 * Increase the wait timeout to a safe value here since we're
583 * serializing the microcode update and that could take a while on a
584 * large number of CPUs. And that is fine as the *actual* timeout will
585 * be determined by the last CPU finished updating and thus cut short.
587 if (__wait_for_cpus(&late_cpus_out
, NSEC_PER_SEC
* num_online_cpus()))
588 panic("Timeout during microcode update!\n");
594 * Reload microcode late on all CPUs. Wait for a sec until they
595 * all gather together.
597 static int microcode_reload_late(void)
601 atomic_set(&late_cpus_in
, 0);
602 atomic_set(&late_cpus_out
, 0);
604 ret
= stop_machine_cpuslocked(__reload_late
, NULL
, cpu_online_mask
);
608 pr_info("Reload completed, microcode revision: 0x%x\n", boot_cpu_data
.microcode
);
613 static ssize_t
reload_store(struct device
*dev
,
614 struct device_attribute
*attr
,
615 const char *buf
, size_t size
)
617 enum ucode_state tmp_ret
= UCODE_OK
;
618 int bsp
= boot_cpu_data
.cpu_index
;
622 ret
= kstrtoul(buf
, 0, &val
);
629 tmp_ret
= microcode_ops
->request_microcode_fw(bsp
, µcode_pdev
->dev
, true);
630 if (tmp_ret
!= UCODE_NEW
)
635 ret
= check_online_cpus();
639 mutex_lock(µcode_mutex
);
640 ret
= microcode_reload_late();
641 mutex_unlock(µcode_mutex
);
652 static ssize_t
version_show(struct device
*dev
,
653 struct device_attribute
*attr
, char *buf
)
655 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ dev
->id
;
657 return sprintf(buf
, "0x%x\n", uci
->cpu_sig
.rev
);
660 static ssize_t
pf_show(struct device
*dev
,
661 struct device_attribute
*attr
, char *buf
)
663 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ dev
->id
;
665 return sprintf(buf
, "0x%x\n", uci
->cpu_sig
.pf
);
668 static DEVICE_ATTR_WO(reload
);
669 static DEVICE_ATTR(version
, 0444, version_show
, NULL
);
670 static DEVICE_ATTR(processor_flags
, 0444, pf_show
, NULL
);
672 static struct attribute
*mc_default_attrs
[] = {
673 &dev_attr_version
.attr
,
674 &dev_attr_processor_flags
.attr
,
678 static const struct attribute_group mc_attr_group
= {
679 .attrs
= mc_default_attrs
,
683 static void microcode_fini_cpu(int cpu
)
685 if (microcode_ops
->microcode_fini_cpu
)
686 microcode_ops
->microcode_fini_cpu(cpu
);
689 static enum ucode_state
microcode_resume_cpu(int cpu
)
691 if (apply_microcode_on_target(cpu
))
694 pr_debug("CPU%d updated upon resume\n", cpu
);
699 static enum ucode_state
microcode_init_cpu(int cpu
, bool refresh_fw
)
701 enum ucode_state ustate
;
702 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ cpu
;
707 if (collect_cpu_info(cpu
))
710 /* --dimm. Trigger a delayed update? */
711 if (system_state
!= SYSTEM_RUNNING
)
714 ustate
= microcode_ops
->request_microcode_fw(cpu
, µcode_pdev
->dev
, refresh_fw
);
715 if (ustate
== UCODE_NEW
) {
716 pr_debug("CPU%d updated upon init\n", cpu
);
717 apply_microcode_on_target(cpu
);
723 static enum ucode_state
microcode_update_cpu(int cpu
)
725 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ cpu
;
727 /* Refresh CPU microcode revision after resume. */
728 collect_cpu_info(cpu
);
731 return microcode_resume_cpu(cpu
);
733 return microcode_init_cpu(cpu
, false);
736 static int mc_device_add(struct device
*dev
, struct subsys_interface
*sif
)
738 int err
, cpu
= dev
->id
;
740 if (!cpu_online(cpu
))
743 pr_debug("CPU%d added\n", cpu
);
745 err
= sysfs_create_group(&dev
->kobj
, &mc_attr_group
);
749 if (microcode_init_cpu(cpu
, true) == UCODE_ERROR
)
755 static void mc_device_remove(struct device
*dev
, struct subsys_interface
*sif
)
759 if (!cpu_online(cpu
))
762 pr_debug("CPU%d removed\n", cpu
);
763 microcode_fini_cpu(cpu
);
764 sysfs_remove_group(&dev
->kobj
, &mc_attr_group
);
767 static struct subsys_interface mc_cpu_interface
= {
769 .subsys
= &cpu_subsys
,
770 .add_dev
= mc_device_add
,
771 .remove_dev
= mc_device_remove
,
775 * mc_bp_resume - Update boot CPU microcode during resume.
777 static void mc_bp_resume(void)
779 int cpu
= smp_processor_id();
780 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ cpu
;
782 if (uci
->valid
&& uci
->mc
)
783 microcode_ops
->apply_microcode(cpu
);
785 reload_early_microcode();
788 static struct syscore_ops mc_syscore_ops
= {
789 .resume
= mc_bp_resume
,
792 static int mc_cpu_online(unsigned int cpu
)
796 dev
= get_cpu_device(cpu
);
797 microcode_update_cpu(cpu
);
798 pr_debug("CPU%d added\n", cpu
);
800 if (sysfs_create_group(&dev
->kobj
, &mc_attr_group
))
801 pr_err("Failed to create group for CPU%d\n", cpu
);
805 static int mc_cpu_down_prep(unsigned int cpu
)
809 dev
= get_cpu_device(cpu
);
810 /* Suspend is in progress, only remove the interface */
811 sysfs_remove_group(&dev
->kobj
, &mc_attr_group
);
812 pr_debug("CPU%d removed\n", cpu
);
817 static struct attribute
*cpu_root_microcode_attrs
[] = {
818 &dev_attr_reload
.attr
,
822 static const struct attribute_group cpu_root_microcode_group
= {
824 .attrs
= cpu_root_microcode_attrs
,
827 int __init
microcode_init(void)
829 struct cpuinfo_x86
*c
= &boot_cpu_data
;
835 if (c
->x86_vendor
== X86_VENDOR_INTEL
)
836 microcode_ops
= init_intel_microcode();
837 else if (c
->x86_vendor
== X86_VENDOR_AMD
)
838 microcode_ops
= init_amd_microcode();
840 pr_err("no support for this CPU vendor\n");
845 microcode_pdev
= platform_device_register_simple("microcode", -1,
847 if (IS_ERR(microcode_pdev
))
848 return PTR_ERR(microcode_pdev
);
851 mutex_lock(µcode_mutex
);
853 error
= subsys_interface_register(&mc_cpu_interface
);
855 perf_check_microcode();
856 mutex_unlock(µcode_mutex
);
862 error
= sysfs_create_group(&cpu_subsys
.dev_root
->kobj
,
863 &cpu_root_microcode_group
);
866 pr_err("Error creating microcode group!\n");
870 error
= microcode_dev_init();
872 goto out_ucode_group
;
874 register_syscore_ops(&mc_syscore_ops
);
875 cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN
, "x86/microcode:online",
876 mc_cpu_online
, mc_cpu_down_prep
);
878 pr_info("Microcode Update Driver: v%s.", DRIVER_VERSION
);
883 sysfs_remove_group(&cpu_subsys
.dev_root
->kobj
,
884 &cpu_root_microcode_group
);
888 mutex_lock(µcode_mutex
);
890 subsys_interface_unregister(&mc_cpu_interface
);
892 mutex_unlock(µcode_mutex
);
896 platform_device_unregister(microcode_pdev
);
900 fs_initcall(save_microcode_in_initrd
);
901 late_initcall(microcode_init
);