2 * CPU Microcode Update Driver for Linux
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>
8 * X86 CPU microcode early update for Linux:
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>
14 * This driver allows to upgrade microcode on x86 processors.
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.
22 #define pr_fmt(fmt) "microcode: " fmt
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>
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>
44 #define DRIVER_VERSION "2.2"
46 static struct microcode_ops *microcode_ops;
47 static bool dis_ucode_ldr = true;
51 LIST_HEAD(microcode_cache);
56 * All non cpu-hotplug-callback call sites use:
58 * - microcode_mutex to synchronize with each other;
59 * - get/put_online_cpus() to synchronize with
60 * the cpu-hotplug-callback call sites.
62 * We guarantee that only a single cpu is being
63 * updated at any particular moment of time.
65 static DEFINE_MUTEX(microcode_mutex);
67 struct ucode_cpu_info ucode_cpu_info[NR_CPUS];
70 struct cpu_signature *cpu_sig;
75 * Those patch levels cannot be updated to newer ones and thus should be final.
77 static u32 final_levels[] = {
81 0, /* T-101 terminator */
85 * Check the current patch level on this CPU.
88 * - true: if update should stop
91 static bool amd_check_current_patch_level(void)
96 native_rdmsr(MSR_AMD64_PATCH_LEVEL, lvl, dummy);
98 if (IS_ENABLED(CONFIG_X86_32))
99 levels = (u32 *)__pa_nodebug(&final_levels);
101 levels = final_levels;
103 for (i = 0; levels[i]; i++) {
104 if (lvl == levels[i])
110 static bool __init check_loader_disabled_bsp(void)
112 static const char *__dis_opt_str = "dis_ucode_ldr";
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);
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;
129 * CPUID(1).ECX[31]: reserved for hypervisor use. This is still not
130 * completely accurate as xen pv guests don't see that CPUID bit set but
131 * that's good enough as they don't land on the BSP path anyway.
133 if (native_cpuid_ecx(1) & BIT(31))
136 if (x86_cpuid_vendor() == X86_VENDOR_AMD) {
137 if (amd_check_current_patch_level())
141 if (cmdline_find_option_bool(cmdline, option) <= 0)
147 extern struct builtin_fw __start_builtin_fw[];
148 extern struct builtin_fw __end_builtin_fw[];
150 bool get_builtin_firmware(struct cpio_data *cd, const char *name)
152 #ifdef CONFIG_FW_LOADER
153 struct builtin_fw *b_fw;
155 for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
156 if (!strcmp(name, b_fw->name)) {
157 cd->size = b_fw->size;
158 cd->data = b_fw->data;
166 void __init load_ucode_bsp(void)
168 unsigned int cpuid_1_eax;
170 if (check_loader_disabled_bsp())
173 cpuid_1_eax = native_cpuid_eax(1);
175 switch (x86_cpuid_vendor()) {
176 case X86_VENDOR_INTEL:
177 if (x86_family(cpuid_1_eax) >= 6)
178 load_ucode_intel_bsp();
181 if (x86_family(cpuid_1_eax) >= 0x10)
182 load_ucode_amd_bsp(cpuid_1_eax);
189 static bool check_loader_disabled_ap(void)
192 return *((bool *)__pa_nodebug(&dis_ucode_ldr));
194 return dis_ucode_ldr;
198 void load_ucode_ap(void)
200 unsigned int cpuid_1_eax;
202 if (check_loader_disabled_ap())
205 cpuid_1_eax = native_cpuid_eax(1);
207 switch (x86_cpuid_vendor()) {
208 case X86_VENDOR_INTEL:
209 if (x86_family(cpuid_1_eax) >= 6)
210 load_ucode_intel_ap();
213 if (x86_family(cpuid_1_eax) >= 0x10)
214 load_ucode_amd_ap(cpuid_1_eax);
221 static int __init save_microcode_in_initrd(void)
223 struct cpuinfo_x86 *c = &boot_cpu_data;
226 switch (c->x86_vendor) {
227 case X86_VENDOR_INTEL:
229 ret = save_microcode_in_initrd_intel();
233 return save_microcode_in_initrd_amd(cpuid_eax(1));
244 struct cpio_data find_microcode_in_initrd(const char *path, bool use_pa)
246 #ifdef CONFIG_BLK_DEV_INITRD
247 unsigned long start = 0;
251 struct boot_params *params;
254 params = (struct boot_params *)__pa_nodebug(&boot_params);
256 params = &boot_params;
258 size = params->hdr.ramdisk_size;
261 * Set start only if we have an initrd image. We cannot use initrd_start
262 * because it is not set that early yet.
265 start = params->hdr.ramdisk_image;
267 # else /* CONFIG_X86_64 */
268 size = (unsigned long)boot_params.ext_ramdisk_size << 32;
269 size |= boot_params.hdr.ramdisk_size;
272 start = (unsigned long)boot_params.ext_ramdisk_image << 32;
273 start |= boot_params.hdr.ramdisk_image;
275 start += PAGE_OFFSET;
280 * Fixup the start address: after reserve_initrd() runs, initrd_start
281 * has the virtual address of the beginning of the initrd. It also
282 * possibly relocates the ramdisk. In either case, initrd_start contains
283 * the updated address so use that instead.
285 * initrd_gone is for the hotplug case where we've thrown out initrd
290 return (struct cpio_data){ NULL, 0, "" };
292 start = initrd_start;
295 return find_cpio_data(path, (void *)start, size, NULL);
296 #else /* !CONFIG_BLK_DEV_INITRD */
297 return (struct cpio_data){ NULL, 0, "" };
301 void reload_early_microcode(void)
305 vendor = x86_cpuid_vendor();
306 family = x86_cpuid_family();
309 case X86_VENDOR_INTEL:
311 reload_ucode_intel();
322 static void collect_cpu_info_local(void *arg)
324 struct cpu_info_ctx *ctx = arg;
326 ctx->err = microcode_ops->collect_cpu_info(smp_processor_id(),
330 static int collect_cpu_info_on_target(int cpu, struct cpu_signature *cpu_sig)
332 struct cpu_info_ctx ctx = { .cpu_sig = cpu_sig, .err = 0 };
335 ret = smp_call_function_single(cpu, collect_cpu_info_local, &ctx, 1);
342 static int collect_cpu_info(int cpu)
344 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
347 memset(uci, 0, sizeof(*uci));
349 ret = collect_cpu_info_on_target(cpu, &uci->cpu_sig);
356 struct apply_microcode_ctx {
360 static void apply_microcode_local(void *arg)
362 struct apply_microcode_ctx *ctx = arg;
364 ctx->err = microcode_ops->apply_microcode(smp_processor_id());
367 static int apply_microcode_on_target(int cpu)
369 struct apply_microcode_ctx ctx = { .err = 0 };
372 ret = smp_call_function_single(cpu, apply_microcode_local, &ctx, 1);
379 #ifdef CONFIG_MICROCODE_OLD_INTERFACE
380 static int do_microcode_update(const void __user *buf, size_t size)
385 for_each_online_cpu(cpu) {
386 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
387 enum ucode_state ustate;
392 ustate = microcode_ops->request_microcode_user(cpu, buf, size);
393 if (ustate == UCODE_ERROR) {
396 } else if (ustate == UCODE_OK)
397 apply_microcode_on_target(cpu);
403 static int microcode_open(struct inode *inode, struct file *file)
405 return capable(CAP_SYS_RAWIO) ? nonseekable_open(inode, file) : -EPERM;
408 static ssize_t microcode_write(struct file *file, const char __user *buf,
409 size_t len, loff_t *ppos)
411 ssize_t ret = -EINVAL;
413 if ((len >> PAGE_SHIFT) > totalram_pages) {
414 pr_err("too much data (max %ld pages)\n", totalram_pages);
419 mutex_lock(µcode_mutex);
421 if (do_microcode_update(buf, len) == 0)
425 perf_check_microcode();
427 mutex_unlock(µcode_mutex);
433 static const struct file_operations microcode_fops = {
434 .owner = THIS_MODULE,
435 .write = microcode_write,
436 .open = microcode_open,
440 static struct miscdevice microcode_dev = {
441 .minor = MICROCODE_MINOR,
443 .nodename = "cpu/microcode",
444 .fops = µcode_fops,
447 static int __init microcode_dev_init(void)
451 error = misc_register(µcode_dev);
453 pr_err("can't misc_register on minor=%d\n", MICROCODE_MINOR);
460 static void __exit microcode_dev_exit(void)
462 misc_deregister(µcode_dev);
465 #define microcode_dev_init() 0
466 #define microcode_dev_exit() do { } while (0)
469 /* fake device for request_firmware */
470 static struct platform_device *microcode_pdev;
472 static int reload_for_cpu(int cpu)
474 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
475 enum ucode_state ustate;
481 ustate = microcode_ops->request_microcode_fw(cpu, µcode_pdev->dev, true);
482 if (ustate == UCODE_OK)
483 apply_microcode_on_target(cpu);
485 if (ustate == UCODE_ERROR)
490 static ssize_t reload_store(struct device *dev,
491 struct device_attribute *attr,
492 const char *buf, size_t size)
496 ssize_t ret = 0, tmp_ret;
498 ret = kstrtoul(buf, 0, &val);
506 mutex_lock(µcode_mutex);
507 for_each_online_cpu(cpu) {
508 tmp_ret = reload_for_cpu(cpu);
510 pr_warn("Error reloading microcode on CPU %d\n", cpu);
512 /* save retval of the first encountered reload error */
517 perf_check_microcode();
518 mutex_unlock(µcode_mutex);
527 static ssize_t version_show(struct device *dev,
528 struct device_attribute *attr, char *buf)
530 struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
532 return sprintf(buf, "0x%x\n", uci->cpu_sig.rev);
535 static ssize_t pf_show(struct device *dev,
536 struct device_attribute *attr, char *buf)
538 struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
540 return sprintf(buf, "0x%x\n", uci->cpu_sig.pf);
543 static DEVICE_ATTR(reload, 0200, NULL, reload_store);
544 static DEVICE_ATTR(version, 0400, version_show, NULL);
545 static DEVICE_ATTR(processor_flags, 0400, pf_show, NULL);
547 static struct attribute *mc_default_attrs[] = {
548 &dev_attr_version.attr,
549 &dev_attr_processor_flags.attr,
553 static struct attribute_group mc_attr_group = {
554 .attrs = mc_default_attrs,
558 static void microcode_fini_cpu(int cpu)
560 if (microcode_ops->microcode_fini_cpu)
561 microcode_ops->microcode_fini_cpu(cpu);
564 static enum ucode_state microcode_resume_cpu(int cpu)
566 if (apply_microcode_on_target(cpu))
569 pr_debug("CPU%d updated upon resume\n", cpu);
574 static enum ucode_state microcode_init_cpu(int cpu, bool refresh_fw)
576 enum ucode_state ustate;
577 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
582 if (collect_cpu_info(cpu))
585 /* --dimm. Trigger a delayed update? */
586 if (system_state != SYSTEM_RUNNING)
589 ustate = microcode_ops->request_microcode_fw(cpu, µcode_pdev->dev,
592 if (ustate == UCODE_OK) {
593 pr_debug("CPU%d updated upon init\n", cpu);
594 apply_microcode_on_target(cpu);
600 static enum ucode_state microcode_update_cpu(int cpu)
602 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
604 /* Refresh CPU microcode revision after resume. */
605 collect_cpu_info(cpu);
608 return microcode_resume_cpu(cpu);
610 return microcode_init_cpu(cpu, false);
613 static int mc_device_add(struct device *dev, struct subsys_interface *sif)
615 int err, cpu = dev->id;
617 if (!cpu_online(cpu))
620 pr_debug("CPU%d added\n", cpu);
622 err = sysfs_create_group(&dev->kobj, &mc_attr_group);
626 if (microcode_init_cpu(cpu, true) == UCODE_ERROR)
632 static void mc_device_remove(struct device *dev, struct subsys_interface *sif)
636 if (!cpu_online(cpu))
639 pr_debug("CPU%d removed\n", cpu);
640 microcode_fini_cpu(cpu);
641 sysfs_remove_group(&dev->kobj, &mc_attr_group);
644 static struct subsys_interface mc_cpu_interface = {
646 .subsys = &cpu_subsys,
647 .add_dev = mc_device_add,
648 .remove_dev = mc_device_remove,
652 * mc_bp_resume - Update boot CPU microcode during resume.
654 static void mc_bp_resume(void)
656 int cpu = smp_processor_id();
657 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
659 if (uci->valid && uci->mc)
660 microcode_ops->apply_microcode(cpu);
662 reload_early_microcode();
665 static struct syscore_ops mc_syscore_ops = {
666 .resume = mc_bp_resume,
669 static int mc_cpu_online(unsigned int cpu)
673 dev = get_cpu_device(cpu);
674 microcode_update_cpu(cpu);
675 pr_debug("CPU%d added\n", cpu);
677 if (sysfs_create_group(&dev->kobj, &mc_attr_group))
678 pr_err("Failed to create group for CPU%d\n", cpu);
682 static int mc_cpu_down_prep(unsigned int cpu)
686 dev = get_cpu_device(cpu);
687 /* Suspend is in progress, only remove the interface */
688 sysfs_remove_group(&dev->kobj, &mc_attr_group);
689 pr_debug("CPU%d removed\n", cpu);
694 static struct attribute *cpu_root_microcode_attrs[] = {
695 &dev_attr_reload.attr,
699 static struct attribute_group cpu_root_microcode_group = {
701 .attrs = cpu_root_microcode_attrs,
704 int __init microcode_init(void)
706 struct cpuinfo_x86 *c = &boot_cpu_data;
712 if (c->x86_vendor == X86_VENDOR_INTEL)
713 microcode_ops = init_intel_microcode();
714 else if (c->x86_vendor == X86_VENDOR_AMD)
715 microcode_ops = init_amd_microcode();
717 pr_err("no support for this CPU vendor\n");
722 microcode_pdev = platform_device_register_simple("microcode", -1,
724 if (IS_ERR(microcode_pdev))
725 return PTR_ERR(microcode_pdev);
728 mutex_lock(µcode_mutex);
730 error = subsys_interface_register(&mc_cpu_interface);
732 perf_check_microcode();
733 mutex_unlock(µcode_mutex);
739 error = sysfs_create_group(&cpu_subsys.dev_root->kobj,
740 &cpu_root_microcode_group);
743 pr_err("Error creating microcode group!\n");
747 error = microcode_dev_init();
749 goto out_ucode_group;
751 register_syscore_ops(&mc_syscore_ops);
752 cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "x86/microcode:online",
753 mc_cpu_online, mc_cpu_down_prep);
755 pr_info("Microcode Update Driver: v%s.", DRIVER_VERSION);
760 sysfs_remove_group(&cpu_subsys.dev_root->kobj,
761 &cpu_root_microcode_group);
765 mutex_lock(µcode_mutex);
767 subsys_interface_unregister(&mc_cpu_interface);
769 mutex_unlock(µcode_mutex);
773 platform_device_unregister(microcode_pdev);
777 fs_initcall(save_microcode_in_initrd);
778 late_initcall(microcode_init);