2 * CPU Microcode Update Driver for Linux
4 * Copyright (C) 2000-2006 Tigran Aivazian <tigran@aivazian.fsnet.co.uk>
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;
49 LIST_HEAD(microcode_cache
);
54 * All non cpu-hotplug-callback call sites use:
56 * - microcode_mutex to synchronize with each other;
57 * - get/put_online_cpus() to synchronize with
58 * the cpu-hotplug-callback call sites.
60 * We guarantee that only a single cpu is being
61 * updated at any particular moment of time.
63 static DEFINE_MUTEX(microcode_mutex
);
65 struct ucode_cpu_info ucode_cpu_info
[NR_CPUS
];
68 * Operations that are run on a target cpu:
72 struct cpu_signature
*cpu_sig
;
76 static bool __init
check_loader_disabled_bsp(void)
78 static const char *__dis_opt_str
= "dis_ucode_ldr";
82 const char *cmdline
= (const char *)__pa_nodebug(boot_command_line
);
83 const char *option
= (const char *)__pa_nodebug(__dis_opt_str
);
84 bool *res
= (bool *)__pa_nodebug(&dis_ucode_ldr
);
86 #else /* CONFIG_X86_64 */
87 const char *cmdline
= boot_command_line
;
88 const char *option
= __dis_opt_str
;
89 bool *res
= &dis_ucode_ldr
;
97 native_cpuid(&a
, &b
, &c
, &d
);
100 * CPUID(1).ECX[31]: reserved for hypervisor use. This is still not
101 * completely accurate as xen pv guests don't see that CPUID bit set but
102 * that's good enough as they don't land on the BSP path anyway.
107 if (cmdline_find_option_bool(cmdline
, option
) <= 0)
113 extern struct builtin_fw __start_builtin_fw
[];
114 extern struct builtin_fw __end_builtin_fw
[];
116 bool get_builtin_firmware(struct cpio_data
*cd
, const char *name
)
118 #ifdef CONFIG_FW_LOADER
119 struct builtin_fw
*b_fw
;
121 for (b_fw
= __start_builtin_fw
; b_fw
!= __end_builtin_fw
; b_fw
++) {
122 if (!strcmp(name
, b_fw
->name
)) {
123 cd
->size
= b_fw
->size
;
124 cd
->data
= b_fw
->data
;
132 void __init
load_ucode_bsp(void)
137 if (check_loader_disabled_bsp())
140 vendor
= x86_cpuid_vendor();
141 family
= x86_cpuid_family();
144 case X86_VENDOR_INTEL
:
146 load_ucode_intel_bsp();
150 load_ucode_amd_bsp(family
);
157 static bool check_loader_disabled_ap(void)
160 return *((bool *)__pa_nodebug(&dis_ucode_ldr
));
162 return dis_ucode_ldr
;
166 void load_ucode_ap(void)
170 if (check_loader_disabled_ap())
173 vendor
= x86_cpuid_vendor();
174 family
= x86_cpuid_family();
177 case X86_VENDOR_INTEL
:
179 load_ucode_intel_ap();
183 load_ucode_amd_ap(family
);
190 static int __init
save_microcode_in_initrd(void)
192 struct cpuinfo_x86
*c
= &boot_cpu_data
;
194 switch (c
->x86_vendor
) {
195 case X86_VENDOR_INTEL
:
197 return save_microcode_in_initrd_intel();
201 return save_microcode_in_initrd_amd(c
->x86
);
210 struct cpio_data
find_microcode_in_initrd(const char *path
, bool use_pa
)
212 #ifdef CONFIG_BLK_DEV_INITRD
213 unsigned long start
= 0;
217 struct boot_params
*params
;
220 params
= (struct boot_params
*)__pa_nodebug(&boot_params
);
222 params
= &boot_params
;
224 size
= params
->hdr
.ramdisk_size
;
227 * Set start only if we have an initrd image. We cannot use initrd_start
228 * because it is not set that early yet.
231 start
= params
->hdr
.ramdisk_image
;
233 # else /* CONFIG_X86_64 */
234 size
= (unsigned long)boot_params
.ext_ramdisk_size
<< 32;
235 size
|= boot_params
.hdr
.ramdisk_size
;
238 start
= (unsigned long)boot_params
.ext_ramdisk_image
<< 32;
239 start
|= boot_params
.hdr
.ramdisk_image
;
241 start
+= PAGE_OFFSET
;
246 * Did we relocate the ramdisk?
248 * So we possibly relocate the ramdisk *after* applying microcode on the
249 * BSP so we rely on use_pa (use physical addresses) - even if it is not
250 * absolutely correct - to determine whether we've done the ramdisk
251 * relocation already.
253 if (!use_pa
&& relocated_ramdisk
)
254 start
= initrd_start
;
256 return find_cpio_data(path
, (void *)start
, size
, NULL
);
257 #else /* !CONFIG_BLK_DEV_INITRD */
258 return (struct cpio_data
){ NULL
, 0, "" };
262 void reload_early_microcode(void)
266 vendor
= x86_cpuid_vendor();
267 family
= x86_cpuid_family();
270 case X86_VENDOR_INTEL
:
272 reload_ucode_intel();
283 static void collect_cpu_info_local(void *arg
)
285 struct cpu_info_ctx
*ctx
= arg
;
287 ctx
->err
= microcode_ops
->collect_cpu_info(smp_processor_id(),
291 static int collect_cpu_info_on_target(int cpu
, struct cpu_signature
*cpu_sig
)
293 struct cpu_info_ctx ctx
= { .cpu_sig
= cpu_sig
, .err
= 0 };
296 ret
= smp_call_function_single(cpu
, collect_cpu_info_local
, &ctx
, 1);
303 static int collect_cpu_info(int cpu
)
305 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ cpu
;
308 memset(uci
, 0, sizeof(*uci
));
310 ret
= collect_cpu_info_on_target(cpu
, &uci
->cpu_sig
);
317 struct apply_microcode_ctx
{
321 static void apply_microcode_local(void *arg
)
323 struct apply_microcode_ctx
*ctx
= arg
;
325 ctx
->err
= microcode_ops
->apply_microcode(smp_processor_id());
328 static int apply_microcode_on_target(int cpu
)
330 struct apply_microcode_ctx ctx
= { .err
= 0 };
333 ret
= smp_call_function_single(cpu
, apply_microcode_local
, &ctx
, 1);
340 #ifdef CONFIG_MICROCODE_OLD_INTERFACE
341 static int do_microcode_update(const void __user
*buf
, size_t size
)
346 for_each_online_cpu(cpu
) {
347 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ cpu
;
348 enum ucode_state ustate
;
353 ustate
= microcode_ops
->request_microcode_user(cpu
, buf
, size
);
354 if (ustate
== UCODE_ERROR
) {
357 } else if (ustate
== UCODE_OK
)
358 apply_microcode_on_target(cpu
);
364 static int microcode_open(struct inode
*inode
, struct file
*file
)
366 return capable(CAP_SYS_RAWIO
) ? nonseekable_open(inode
, file
) : -EPERM
;
369 static ssize_t
microcode_write(struct file
*file
, const char __user
*buf
,
370 size_t len
, loff_t
*ppos
)
372 ssize_t ret
= -EINVAL
;
374 if ((len
>> PAGE_SHIFT
) > totalram_pages
) {
375 pr_err("too much data (max %ld pages)\n", totalram_pages
);
380 mutex_lock(µcode_mutex
);
382 if (do_microcode_update(buf
, len
) == 0)
386 perf_check_microcode();
388 mutex_unlock(µcode_mutex
);
394 static const struct file_operations microcode_fops
= {
395 .owner
= THIS_MODULE
,
396 .write
= microcode_write
,
397 .open
= microcode_open
,
401 static struct miscdevice microcode_dev
= {
402 .minor
= MICROCODE_MINOR
,
404 .nodename
= "cpu/microcode",
405 .fops
= µcode_fops
,
408 static int __init
microcode_dev_init(void)
412 error
= misc_register(µcode_dev
);
414 pr_err("can't misc_register on minor=%d\n", MICROCODE_MINOR
);
421 static void __exit
microcode_dev_exit(void)
423 misc_deregister(µcode_dev
);
426 #define microcode_dev_init() 0
427 #define microcode_dev_exit() do { } while (0)
430 /* fake device for request_firmware */
431 static struct platform_device
*microcode_pdev
;
433 static int reload_for_cpu(int cpu
)
435 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ cpu
;
436 enum ucode_state ustate
;
442 ustate
= microcode_ops
->request_microcode_fw(cpu
, µcode_pdev
->dev
, true);
443 if (ustate
== UCODE_OK
)
444 apply_microcode_on_target(cpu
);
446 if (ustate
== UCODE_ERROR
)
451 static ssize_t
reload_store(struct device
*dev
,
452 struct device_attribute
*attr
,
453 const char *buf
, size_t size
)
457 ssize_t ret
= 0, tmp_ret
;
459 ret
= kstrtoul(buf
, 0, &val
);
467 mutex_lock(µcode_mutex
);
468 for_each_online_cpu(cpu
) {
469 tmp_ret
= reload_for_cpu(cpu
);
471 pr_warn("Error reloading microcode on CPU %d\n", cpu
);
473 /* save retval of the first encountered reload error */
478 perf_check_microcode();
479 mutex_unlock(µcode_mutex
);
488 static ssize_t
version_show(struct device
*dev
,
489 struct device_attribute
*attr
, char *buf
)
491 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ dev
->id
;
493 return sprintf(buf
, "0x%x\n", uci
->cpu_sig
.rev
);
496 static ssize_t
pf_show(struct device
*dev
,
497 struct device_attribute
*attr
, char *buf
)
499 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ dev
->id
;
501 return sprintf(buf
, "0x%x\n", uci
->cpu_sig
.pf
);
504 static DEVICE_ATTR(reload
, 0200, NULL
, reload_store
);
505 static DEVICE_ATTR(version
, 0400, version_show
, NULL
);
506 static DEVICE_ATTR(processor_flags
, 0400, pf_show
, NULL
);
508 static struct attribute
*mc_default_attrs
[] = {
509 &dev_attr_version
.attr
,
510 &dev_attr_processor_flags
.attr
,
514 static struct attribute_group mc_attr_group
= {
515 .attrs
= mc_default_attrs
,
519 static void microcode_fini_cpu(int cpu
)
521 if (microcode_ops
->microcode_fini_cpu
)
522 microcode_ops
->microcode_fini_cpu(cpu
);
525 static enum ucode_state
microcode_resume_cpu(int cpu
)
527 if (apply_microcode_on_target(cpu
))
530 pr_debug("CPU%d updated upon resume\n", cpu
);
535 static enum ucode_state
microcode_init_cpu(int cpu
, bool refresh_fw
)
537 enum ucode_state ustate
;
538 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ cpu
;
543 if (collect_cpu_info(cpu
))
546 /* --dimm. Trigger a delayed update? */
547 if (system_state
!= SYSTEM_RUNNING
)
550 ustate
= microcode_ops
->request_microcode_fw(cpu
, µcode_pdev
->dev
,
553 if (ustate
== UCODE_OK
) {
554 pr_debug("CPU%d updated upon init\n", cpu
);
555 apply_microcode_on_target(cpu
);
561 static enum ucode_state
microcode_update_cpu(int cpu
)
563 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ cpu
;
565 /* Refresh CPU microcode revision after resume. */
566 collect_cpu_info(cpu
);
569 return microcode_resume_cpu(cpu
);
571 return microcode_init_cpu(cpu
, false);
574 static int mc_device_add(struct device
*dev
, struct subsys_interface
*sif
)
576 int err
, cpu
= dev
->id
;
578 if (!cpu_online(cpu
))
581 pr_debug("CPU%d added\n", cpu
);
583 err
= sysfs_create_group(&dev
->kobj
, &mc_attr_group
);
587 if (microcode_init_cpu(cpu
, true) == UCODE_ERROR
)
593 static void mc_device_remove(struct device
*dev
, struct subsys_interface
*sif
)
597 if (!cpu_online(cpu
))
600 pr_debug("CPU%d removed\n", cpu
);
601 microcode_fini_cpu(cpu
);
602 sysfs_remove_group(&dev
->kobj
, &mc_attr_group
);
605 static struct subsys_interface mc_cpu_interface
= {
607 .subsys
= &cpu_subsys
,
608 .add_dev
= mc_device_add
,
609 .remove_dev
= mc_device_remove
,
613 * mc_bp_resume - Update boot CPU microcode during resume.
615 static void mc_bp_resume(void)
617 int cpu
= smp_processor_id();
618 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ cpu
;
620 if (uci
->valid
&& uci
->mc
)
621 microcode_ops
->apply_microcode(cpu
);
623 reload_early_microcode();
626 static struct syscore_ops mc_syscore_ops
= {
627 .resume
= mc_bp_resume
,
630 static int mc_cpu_online(unsigned int cpu
)
634 dev
= get_cpu_device(cpu
);
635 microcode_update_cpu(cpu
);
636 pr_debug("CPU%d added\n", cpu
);
638 if (sysfs_create_group(&dev
->kobj
, &mc_attr_group
))
639 pr_err("Failed to create group for CPU%d\n", cpu
);
643 static int mc_cpu_down_prep(unsigned int cpu
)
647 dev
= get_cpu_device(cpu
);
648 /* Suspend is in progress, only remove the interface */
649 sysfs_remove_group(&dev
->kobj
, &mc_attr_group
);
650 pr_debug("CPU%d removed\n", cpu
);
655 static struct attribute
*cpu_root_microcode_attrs
[] = {
656 &dev_attr_reload
.attr
,
660 static struct attribute_group cpu_root_microcode_group
= {
662 .attrs
= cpu_root_microcode_attrs
,
665 int __init
microcode_init(void)
667 struct cpuinfo_x86
*c
= &boot_cpu_data
;
673 if (c
->x86_vendor
== X86_VENDOR_INTEL
)
674 microcode_ops
= init_intel_microcode();
675 else if (c
->x86_vendor
== X86_VENDOR_AMD
)
676 microcode_ops
= init_amd_microcode();
678 pr_err("no support for this CPU vendor\n");
683 microcode_pdev
= platform_device_register_simple("microcode", -1,
685 if (IS_ERR(microcode_pdev
))
686 return PTR_ERR(microcode_pdev
);
689 mutex_lock(µcode_mutex
);
691 error
= subsys_interface_register(&mc_cpu_interface
);
693 perf_check_microcode();
694 mutex_unlock(µcode_mutex
);
700 error
= sysfs_create_group(&cpu_subsys
.dev_root
->kobj
,
701 &cpu_root_microcode_group
);
704 pr_err("Error creating microcode group!\n");
708 error
= microcode_dev_init();
710 goto out_ucode_group
;
712 register_syscore_ops(&mc_syscore_ops
);
713 cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN
, "x86/microcode:online",
714 mc_cpu_online
, mc_cpu_down_prep
);
716 pr_info("Microcode Update Driver: v%s.", DRIVER_VERSION
);
721 sysfs_remove_group(&cpu_subsys
.dev_root
->kobj
,
722 &cpu_root_microcode_group
);
726 mutex_lock(µcode_mutex
);
728 subsys_interface_unregister(&mc_cpu_interface
);
730 mutex_unlock(µcode_mutex
);
734 platform_device_unregister(microcode_pdev
);
738 fs_initcall(save_microcode_in_initrd
);
739 late_initcall(microcode_init
);