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
;
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";
81 const char *cmdline
= (const char *)__pa_nodebug(boot_command_line
);
82 const char *option
= (const char *)__pa_nodebug(__dis_opt_str
);
83 bool *res
= (bool *)__pa_nodebug(&dis_ucode_ldr
);
85 #else /* CONFIG_X86_64 */
86 const char *cmdline
= boot_command_line
;
87 const char *option
= __dis_opt_str
;
88 bool *res
= &dis_ucode_ldr
;
91 if (cmdline_find_option_bool(cmdline
, option
))
97 extern struct builtin_fw __start_builtin_fw
[];
98 extern struct builtin_fw __end_builtin_fw
[];
100 bool get_builtin_firmware(struct cpio_data
*cd
, const char *name
)
102 #ifdef CONFIG_FW_LOADER
103 struct builtin_fw
*b_fw
;
105 for (b_fw
= __start_builtin_fw
; b_fw
!= __end_builtin_fw
; b_fw
++) {
106 if (!strcmp(name
, b_fw
->name
)) {
107 cd
->size
= b_fw
->size
;
108 cd
->data
= b_fw
->data
;
116 void __init
load_ucode_bsp(void)
121 if (check_loader_disabled_bsp())
127 vendor
= x86_cpuid_vendor();
128 family
= x86_cpuid_family();
131 case X86_VENDOR_INTEL
:
133 load_ucode_intel_bsp();
137 load_ucode_amd_bsp(family
);
144 static bool check_loader_disabled_ap(void)
147 return *((bool *)__pa_nodebug(&dis_ucode_ldr
));
149 return dis_ucode_ldr
;
153 void load_ucode_ap(void)
157 if (check_loader_disabled_ap())
163 vendor
= x86_cpuid_vendor();
164 family
= x86_cpuid_family();
167 case X86_VENDOR_INTEL
:
169 load_ucode_intel_ap();
173 load_ucode_amd_ap(family
);
180 static int __init
save_microcode_in_initrd(void)
182 struct cpuinfo_x86
*c
= &boot_cpu_data
;
184 switch (c
->x86_vendor
) {
185 case X86_VENDOR_INTEL
:
187 return save_microcode_in_initrd_intel();
191 return save_microcode_in_initrd_amd(c
->x86
);
200 struct cpio_data
find_microcode_in_initrd(const char *path
, bool use_pa
)
202 #ifdef CONFIG_BLK_DEV_INITRD
203 unsigned long start
= 0;
207 struct boot_params
*params
;
210 params
= (struct boot_params
*)__pa_nodebug(&boot_params
);
212 params
= &boot_params
;
214 size
= params
->hdr
.ramdisk_size
;
217 * Set start only if we have an initrd image. We cannot use initrd_start
218 * because it is not set that early yet.
221 start
= params
->hdr
.ramdisk_image
;
223 # else /* CONFIG_X86_64 */
224 size
= (unsigned long)boot_params
.ext_ramdisk_size
<< 32;
225 size
|= boot_params
.hdr
.ramdisk_size
;
228 start
= (unsigned long)boot_params
.ext_ramdisk_image
<< 32;
229 start
|= boot_params
.hdr
.ramdisk_image
;
231 start
+= PAGE_OFFSET
;
236 * Did we relocate the ramdisk?
238 * So we possibly relocate the ramdisk *after* applying microcode on the
239 * BSP so we rely on use_pa (use physical addresses) - even if it is not
240 * absolutely correct - to determine whether we've done the ramdisk
241 * relocation already.
243 if (!use_pa
&& relocated_ramdisk
)
244 start
= initrd_start
;
246 return find_cpio_data(path
, (void *)start
, size
, NULL
);
247 #else /* !CONFIG_BLK_DEV_INITRD */
248 return (struct cpio_data
){ NULL
, 0, "" };
252 void reload_early_microcode(void)
256 vendor
= x86_cpuid_vendor();
257 family
= x86_cpuid_family();
260 case X86_VENDOR_INTEL
:
262 reload_ucode_intel();
273 static void collect_cpu_info_local(void *arg
)
275 struct cpu_info_ctx
*ctx
= arg
;
277 ctx
->err
= microcode_ops
->collect_cpu_info(smp_processor_id(),
281 static int collect_cpu_info_on_target(int cpu
, struct cpu_signature
*cpu_sig
)
283 struct cpu_info_ctx ctx
= { .cpu_sig
= cpu_sig
, .err
= 0 };
286 ret
= smp_call_function_single(cpu
, collect_cpu_info_local
, &ctx
, 1);
293 static int collect_cpu_info(int cpu
)
295 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ cpu
;
298 memset(uci
, 0, sizeof(*uci
));
300 ret
= collect_cpu_info_on_target(cpu
, &uci
->cpu_sig
);
307 struct apply_microcode_ctx
{
311 static void apply_microcode_local(void *arg
)
313 struct apply_microcode_ctx
*ctx
= arg
;
315 ctx
->err
= microcode_ops
->apply_microcode(smp_processor_id());
318 static int apply_microcode_on_target(int cpu
)
320 struct apply_microcode_ctx ctx
= { .err
= 0 };
323 ret
= smp_call_function_single(cpu
, apply_microcode_local
, &ctx
, 1);
330 #ifdef CONFIG_MICROCODE_OLD_INTERFACE
331 static int do_microcode_update(const void __user
*buf
, size_t size
)
336 for_each_online_cpu(cpu
) {
337 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ cpu
;
338 enum ucode_state ustate
;
343 ustate
= microcode_ops
->request_microcode_user(cpu
, buf
, size
);
344 if (ustate
== UCODE_ERROR
) {
347 } else if (ustate
== UCODE_OK
)
348 apply_microcode_on_target(cpu
);
354 static int microcode_open(struct inode
*inode
, struct file
*file
)
356 return capable(CAP_SYS_RAWIO
) ? nonseekable_open(inode
, file
) : -EPERM
;
359 static ssize_t
microcode_write(struct file
*file
, const char __user
*buf
,
360 size_t len
, loff_t
*ppos
)
362 ssize_t ret
= -EINVAL
;
364 if ((len
>> PAGE_SHIFT
) > totalram_pages
) {
365 pr_err("too much data (max %ld pages)\n", totalram_pages
);
370 mutex_lock(µcode_mutex
);
372 if (do_microcode_update(buf
, len
) == 0)
376 perf_check_microcode();
378 mutex_unlock(µcode_mutex
);
384 static const struct file_operations microcode_fops
= {
385 .owner
= THIS_MODULE
,
386 .write
= microcode_write
,
387 .open
= microcode_open
,
391 static struct miscdevice microcode_dev
= {
392 .minor
= MICROCODE_MINOR
,
394 .nodename
= "cpu/microcode",
395 .fops
= µcode_fops
,
398 static int __init
microcode_dev_init(void)
402 error
= misc_register(µcode_dev
);
404 pr_err("can't misc_register on minor=%d\n", MICROCODE_MINOR
);
411 static void __exit
microcode_dev_exit(void)
413 misc_deregister(µcode_dev
);
416 #define microcode_dev_init() 0
417 #define microcode_dev_exit() do { } while (0)
420 /* fake device for request_firmware */
421 static struct platform_device
*microcode_pdev
;
423 static int reload_for_cpu(int cpu
)
425 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ cpu
;
426 enum ucode_state ustate
;
432 ustate
= microcode_ops
->request_microcode_fw(cpu
, µcode_pdev
->dev
, true);
433 if (ustate
== UCODE_OK
)
434 apply_microcode_on_target(cpu
);
436 if (ustate
== UCODE_ERROR
)
441 static ssize_t
reload_store(struct device
*dev
,
442 struct device_attribute
*attr
,
443 const char *buf
, size_t size
)
447 ssize_t ret
= 0, tmp_ret
;
449 ret
= kstrtoul(buf
, 0, &val
);
457 mutex_lock(µcode_mutex
);
458 for_each_online_cpu(cpu
) {
459 tmp_ret
= reload_for_cpu(cpu
);
461 pr_warn("Error reloading microcode on CPU %d\n", cpu
);
463 /* save retval of the first encountered reload error */
468 perf_check_microcode();
469 mutex_unlock(µcode_mutex
);
478 static ssize_t
version_show(struct device
*dev
,
479 struct device_attribute
*attr
, char *buf
)
481 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ dev
->id
;
483 return sprintf(buf
, "0x%x\n", uci
->cpu_sig
.rev
);
486 static ssize_t
pf_show(struct device
*dev
,
487 struct device_attribute
*attr
, char *buf
)
489 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ dev
->id
;
491 return sprintf(buf
, "0x%x\n", uci
->cpu_sig
.pf
);
494 static DEVICE_ATTR(reload
, 0200, NULL
, reload_store
);
495 static DEVICE_ATTR(version
, 0400, version_show
, NULL
);
496 static DEVICE_ATTR(processor_flags
, 0400, pf_show
, NULL
);
498 static struct attribute
*mc_default_attrs
[] = {
499 &dev_attr_version
.attr
,
500 &dev_attr_processor_flags
.attr
,
504 static struct attribute_group mc_attr_group
= {
505 .attrs
= mc_default_attrs
,
509 static void microcode_fini_cpu(int cpu
)
511 if (microcode_ops
->microcode_fini_cpu
)
512 microcode_ops
->microcode_fini_cpu(cpu
);
515 static enum ucode_state
microcode_resume_cpu(int cpu
)
517 if (apply_microcode_on_target(cpu
))
520 pr_debug("CPU%d updated upon resume\n", cpu
);
525 static enum ucode_state
microcode_init_cpu(int cpu
, bool refresh_fw
)
527 enum ucode_state ustate
;
528 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ cpu
;
533 if (collect_cpu_info(cpu
))
536 /* --dimm. Trigger a delayed update? */
537 if (system_state
!= SYSTEM_RUNNING
)
540 ustate
= microcode_ops
->request_microcode_fw(cpu
, µcode_pdev
->dev
,
543 if (ustate
== UCODE_OK
) {
544 pr_debug("CPU%d updated upon init\n", cpu
);
545 apply_microcode_on_target(cpu
);
551 static enum ucode_state
microcode_update_cpu(int cpu
)
553 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ cpu
;
555 /* Refresh CPU microcode revision after resume. */
556 collect_cpu_info(cpu
);
559 return microcode_resume_cpu(cpu
);
561 return microcode_init_cpu(cpu
, false);
564 static int mc_device_add(struct device
*dev
, struct subsys_interface
*sif
)
566 int err
, cpu
= dev
->id
;
568 if (!cpu_online(cpu
))
571 pr_debug("CPU%d added\n", cpu
);
573 err
= sysfs_create_group(&dev
->kobj
, &mc_attr_group
);
577 if (microcode_init_cpu(cpu
, true) == UCODE_ERROR
)
583 static void mc_device_remove(struct device
*dev
, struct subsys_interface
*sif
)
587 if (!cpu_online(cpu
))
590 pr_debug("CPU%d removed\n", cpu
);
591 microcode_fini_cpu(cpu
);
592 sysfs_remove_group(&dev
->kobj
, &mc_attr_group
);
595 static struct subsys_interface mc_cpu_interface
= {
597 .subsys
= &cpu_subsys
,
598 .add_dev
= mc_device_add
,
599 .remove_dev
= mc_device_remove
,
603 * mc_bp_resume - Update boot CPU microcode during resume.
605 static void mc_bp_resume(void)
607 int cpu
= smp_processor_id();
608 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ cpu
;
610 if (uci
->valid
&& uci
->mc
)
611 microcode_ops
->apply_microcode(cpu
);
613 reload_early_microcode();
616 static struct syscore_ops mc_syscore_ops
= {
617 .resume
= mc_bp_resume
,
620 static int mc_cpu_online(unsigned int cpu
)
624 dev
= get_cpu_device(cpu
);
625 microcode_update_cpu(cpu
);
626 pr_debug("CPU%d added\n", cpu
);
628 if (sysfs_create_group(&dev
->kobj
, &mc_attr_group
))
629 pr_err("Failed to create group for CPU%d\n", cpu
);
633 static int mc_cpu_down_prep(unsigned int cpu
)
637 dev
= get_cpu_device(cpu
);
638 /* Suspend is in progress, only remove the interface */
639 sysfs_remove_group(&dev
->kobj
, &mc_attr_group
);
640 pr_debug("CPU%d removed\n", cpu
);
645 static struct attribute
*cpu_root_microcode_attrs
[] = {
646 &dev_attr_reload
.attr
,
650 static struct attribute_group cpu_root_microcode_group
= {
652 .attrs
= cpu_root_microcode_attrs
,
655 int __init
microcode_init(void)
657 struct cpuinfo_x86
*c
= &boot_cpu_data
;
663 if (c
->x86_vendor
== X86_VENDOR_INTEL
)
664 microcode_ops
= init_intel_microcode();
665 else if (c
->x86_vendor
== X86_VENDOR_AMD
)
666 microcode_ops
= init_amd_microcode();
668 pr_err("no support for this CPU vendor\n");
673 microcode_pdev
= platform_device_register_simple("microcode", -1,
675 if (IS_ERR(microcode_pdev
))
676 return PTR_ERR(microcode_pdev
);
679 mutex_lock(µcode_mutex
);
681 error
= subsys_interface_register(&mc_cpu_interface
);
683 perf_check_microcode();
684 mutex_unlock(µcode_mutex
);
690 error
= sysfs_create_group(&cpu_subsys
.dev_root
->kobj
,
691 &cpu_root_microcode_group
);
694 pr_err("Error creating microcode group!\n");
698 error
= microcode_dev_init();
700 goto out_ucode_group
;
702 register_syscore_ops(&mc_syscore_ops
);
703 cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN
, "x86/microcode:online",
704 mc_cpu_online
, mc_cpu_down_prep
);
706 pr_info("Microcode Update Driver: v%s.", DRIVER_VERSION
);
711 sysfs_remove_group(&cpu_subsys
.dev_root
->kobj
,
712 &cpu_root_microcode_group
);
716 mutex_lock(µcode_mutex
);
718 subsys_interface_unregister(&mc_cpu_interface
);
720 mutex_unlock(µcode_mutex
);
724 platform_device_unregister(microcode_pdev
);
728 fs_initcall(save_microcode_in_initrd
);
729 late_initcall(microcode_init
);