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-2015 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>
43 #define MICROCODE_VERSION "2.01"
45 static struct microcode_ops
*microcode_ops
;
47 static bool dis_ucode_ldr
;
49 static int __init
disable_loader(char *str
)
54 __setup("dis_ucode_ldr", disable_loader
);
59 * All non cpu-hotplug-callback call sites use:
61 * - microcode_mutex to synchronize with each other;
62 * - get/put_online_cpus() to synchronize with
63 * the cpu-hotplug-callback call sites.
65 * We guarantee that only a single cpu is being
66 * updated at any particular moment of time.
68 static DEFINE_MUTEX(microcode_mutex
);
70 struct ucode_cpu_info ucode_cpu_info
[NR_CPUS
];
71 EXPORT_SYMBOL_GPL(ucode_cpu_info
);
74 * Operations that are run on a target cpu:
78 struct cpu_signature
*cpu_sig
;
82 static bool __init
check_loader_disabled_bsp(void)
85 const char *cmdline
= (const char *)__pa_nodebug(boot_command_line
);
86 const char *opt
= "dis_ucode_ldr";
87 const char *option
= (const char *)__pa_nodebug(opt
);
88 bool *res
= (bool *)__pa_nodebug(&dis_ucode_ldr
);
90 #else /* CONFIG_X86_64 */
91 const char *cmdline
= boot_command_line
;
92 const char *option
= "dis_ucode_ldr";
93 bool *res
= &dis_ucode_ldr
;
96 if (cmdline_find_option_bool(cmdline
, option
))
102 extern struct builtin_fw __start_builtin_fw
[];
103 extern struct builtin_fw __end_builtin_fw
[];
105 bool get_builtin_firmware(struct cpio_data
*cd
, const char *name
)
107 #ifdef CONFIG_FW_LOADER
108 struct builtin_fw
*b_fw
;
110 for (b_fw
= __start_builtin_fw
; b_fw
!= __end_builtin_fw
; b_fw
++) {
111 if (!strcmp(name
, b_fw
->name
)) {
112 cd
->size
= b_fw
->size
;
113 cd
->data
= b_fw
->data
;
121 void __init
load_ucode_bsp(void)
126 if (check_loader_disabled_bsp())
132 vendor
= x86_cpuid_vendor();
133 family
= x86_cpuid_family();
136 case X86_VENDOR_INTEL
:
138 load_ucode_intel_bsp();
142 load_ucode_amd_bsp(family
);
149 static bool check_loader_disabled_ap(void)
152 return *((bool *)__pa_nodebug(&dis_ucode_ldr
));
154 return dis_ucode_ldr
;
158 void load_ucode_ap(void)
162 if (check_loader_disabled_ap())
168 vendor
= x86_cpuid_vendor();
169 family
= x86_cpuid_family();
172 case X86_VENDOR_INTEL
:
174 load_ucode_intel_ap();
185 int __init
save_microcode_in_initrd(void)
187 struct cpuinfo_x86
*c
= &boot_cpu_data
;
189 switch (c
->x86_vendor
) {
190 case X86_VENDOR_INTEL
:
192 save_microcode_in_initrd_intel();
196 save_microcode_in_initrd_amd();
205 void reload_early_microcode(void)
209 vendor
= x86_cpuid_vendor();
210 family
= x86_cpuid_family();
213 case X86_VENDOR_INTEL
:
215 reload_ucode_intel();
226 static void collect_cpu_info_local(void *arg
)
228 struct cpu_info_ctx
*ctx
= arg
;
230 ctx
->err
= microcode_ops
->collect_cpu_info(smp_processor_id(),
234 static int collect_cpu_info_on_target(int cpu
, struct cpu_signature
*cpu_sig
)
236 struct cpu_info_ctx ctx
= { .cpu_sig
= cpu_sig
, .err
= 0 };
239 ret
= smp_call_function_single(cpu
, collect_cpu_info_local
, &ctx
, 1);
246 static int collect_cpu_info(int cpu
)
248 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ cpu
;
251 memset(uci
, 0, sizeof(*uci
));
253 ret
= collect_cpu_info_on_target(cpu
, &uci
->cpu_sig
);
260 struct apply_microcode_ctx
{
264 static void apply_microcode_local(void *arg
)
266 struct apply_microcode_ctx
*ctx
= arg
;
268 ctx
->err
= microcode_ops
->apply_microcode(smp_processor_id());
271 static int apply_microcode_on_target(int cpu
)
273 struct apply_microcode_ctx ctx
= { .err
= 0 };
276 ret
= smp_call_function_single(cpu
, apply_microcode_local
, &ctx
, 1);
283 #ifdef CONFIG_MICROCODE_OLD_INTERFACE
284 static int do_microcode_update(const void __user
*buf
, size_t size
)
289 for_each_online_cpu(cpu
) {
290 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ cpu
;
291 enum ucode_state ustate
;
296 ustate
= microcode_ops
->request_microcode_user(cpu
, buf
, size
);
297 if (ustate
== UCODE_ERROR
) {
300 } else if (ustate
== UCODE_OK
)
301 apply_microcode_on_target(cpu
);
307 static int microcode_open(struct inode
*inode
, struct file
*file
)
309 return capable(CAP_SYS_RAWIO
) ? nonseekable_open(inode
, file
) : -EPERM
;
312 static ssize_t
microcode_write(struct file
*file
, const char __user
*buf
,
313 size_t len
, loff_t
*ppos
)
315 ssize_t ret
= -EINVAL
;
317 if ((len
>> PAGE_SHIFT
) > totalram_pages
) {
318 pr_err("too much data (max %ld pages)\n", totalram_pages
);
323 mutex_lock(µcode_mutex
);
325 if (do_microcode_update(buf
, len
) == 0)
329 perf_check_microcode();
331 mutex_unlock(µcode_mutex
);
337 static const struct file_operations microcode_fops
= {
338 .owner
= THIS_MODULE
,
339 .write
= microcode_write
,
340 .open
= microcode_open
,
344 static struct miscdevice microcode_dev
= {
345 .minor
= MICROCODE_MINOR
,
347 .nodename
= "cpu/microcode",
348 .fops
= µcode_fops
,
351 static int __init
microcode_dev_init(void)
355 error
= misc_register(µcode_dev
);
357 pr_err("can't misc_register on minor=%d\n", MICROCODE_MINOR
);
364 static void __exit
microcode_dev_exit(void)
366 misc_deregister(µcode_dev
);
369 #define microcode_dev_init() 0
370 #define microcode_dev_exit() do { } while (0)
373 /* fake device for request_firmware */
374 static struct platform_device
*microcode_pdev
;
376 static int reload_for_cpu(int cpu
)
378 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ cpu
;
379 enum ucode_state ustate
;
385 ustate
= microcode_ops
->request_microcode_fw(cpu
, µcode_pdev
->dev
, true);
386 if (ustate
== UCODE_OK
)
387 apply_microcode_on_target(cpu
);
389 if (ustate
== UCODE_ERROR
)
394 static ssize_t
reload_store(struct device
*dev
,
395 struct device_attribute
*attr
,
396 const char *buf
, size_t size
)
400 ssize_t ret
= 0, tmp_ret
;
402 ret
= kstrtoul(buf
, 0, &val
);
410 mutex_lock(µcode_mutex
);
411 for_each_online_cpu(cpu
) {
412 tmp_ret
= reload_for_cpu(cpu
);
414 pr_warn("Error reloading microcode on CPU %d\n", cpu
);
416 /* save retval of the first encountered reload error */
421 perf_check_microcode();
422 mutex_unlock(µcode_mutex
);
431 static ssize_t
version_show(struct device
*dev
,
432 struct device_attribute
*attr
, char *buf
)
434 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ dev
->id
;
436 return sprintf(buf
, "0x%x\n", uci
->cpu_sig
.rev
);
439 static ssize_t
pf_show(struct device
*dev
,
440 struct device_attribute
*attr
, char *buf
)
442 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ dev
->id
;
444 return sprintf(buf
, "0x%x\n", uci
->cpu_sig
.pf
);
447 static DEVICE_ATTR(reload
, 0200, NULL
, reload_store
);
448 static DEVICE_ATTR(version
, 0400, version_show
, NULL
);
449 static DEVICE_ATTR(processor_flags
, 0400, pf_show
, NULL
);
451 static struct attribute
*mc_default_attrs
[] = {
452 &dev_attr_version
.attr
,
453 &dev_attr_processor_flags
.attr
,
457 static struct attribute_group mc_attr_group
= {
458 .attrs
= mc_default_attrs
,
462 static void microcode_fini_cpu(int cpu
)
464 microcode_ops
->microcode_fini_cpu(cpu
);
467 static enum ucode_state
microcode_resume_cpu(int cpu
)
469 pr_debug("CPU%d updated upon resume\n", cpu
);
471 if (apply_microcode_on_target(cpu
))
477 static enum ucode_state
microcode_init_cpu(int cpu
, bool refresh_fw
)
479 enum ucode_state ustate
;
480 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ cpu
;
482 if (uci
&& uci
->valid
)
485 if (collect_cpu_info(cpu
))
488 /* --dimm. Trigger a delayed update? */
489 if (system_state
!= SYSTEM_RUNNING
)
492 ustate
= microcode_ops
->request_microcode_fw(cpu
, µcode_pdev
->dev
,
495 if (ustate
== UCODE_OK
) {
496 pr_debug("CPU%d updated upon init\n", cpu
);
497 apply_microcode_on_target(cpu
);
503 static enum ucode_state
microcode_update_cpu(int cpu
)
505 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ cpu
;
508 return microcode_resume_cpu(cpu
);
510 return microcode_init_cpu(cpu
, false);
513 static int mc_device_add(struct device
*dev
, struct subsys_interface
*sif
)
515 int err
, cpu
= dev
->id
;
517 if (!cpu_online(cpu
))
520 pr_debug("CPU%d added\n", cpu
);
522 err
= sysfs_create_group(&dev
->kobj
, &mc_attr_group
);
526 if (microcode_init_cpu(cpu
, true) == UCODE_ERROR
)
532 static void mc_device_remove(struct device
*dev
, struct subsys_interface
*sif
)
536 if (!cpu_online(cpu
))
539 pr_debug("CPU%d removed\n", cpu
);
540 microcode_fini_cpu(cpu
);
541 sysfs_remove_group(&dev
->kobj
, &mc_attr_group
);
544 static struct subsys_interface mc_cpu_interface
= {
546 .subsys
= &cpu_subsys
,
547 .add_dev
= mc_device_add
,
548 .remove_dev
= mc_device_remove
,
552 * mc_bp_resume - Update boot CPU microcode during resume.
554 static void mc_bp_resume(void)
556 int cpu
= smp_processor_id();
557 struct ucode_cpu_info
*uci
= ucode_cpu_info
+ cpu
;
559 if (uci
->valid
&& uci
->mc
)
560 microcode_ops
->apply_microcode(cpu
);
562 reload_early_microcode();
565 static struct syscore_ops mc_syscore_ops
= {
566 .resume
= mc_bp_resume
,
570 mc_cpu_callback(struct notifier_block
*nb
, unsigned long action
, void *hcpu
)
572 unsigned int cpu
= (unsigned long)hcpu
;
575 dev
= get_cpu_device(cpu
);
577 switch (action
& ~CPU_TASKS_FROZEN
) {
579 microcode_update_cpu(cpu
);
580 pr_debug("CPU%d added\n", cpu
);
582 * "break" is missing on purpose here because we want to fall
583 * through in order to create the sysfs group.
586 case CPU_DOWN_FAILED
:
587 if (sysfs_create_group(&dev
->kobj
, &mc_attr_group
))
588 pr_err("Failed to create group for CPU%d\n", cpu
);
591 case CPU_DOWN_PREPARE
:
592 /* Suspend is in progress, only remove the interface */
593 sysfs_remove_group(&dev
->kobj
, &mc_attr_group
);
594 pr_debug("CPU%d removed\n", cpu
);
600 * When a CPU goes offline, don't free up or invalidate the copy of
601 * the microcode in kernel memory, so that we can reuse it when the
602 * CPU comes back online without unnecessarily requesting the userspace
607 /* The CPU refused to come up during a system resume */
608 if (action
== CPU_UP_CANCELED_FROZEN
)
609 microcode_fini_cpu(cpu
);
614 static struct notifier_block mc_cpu_notifier
= {
615 .notifier_call
= mc_cpu_callback
,
618 static struct attribute
*cpu_root_microcode_attrs
[] = {
619 &dev_attr_reload
.attr
,
623 static struct attribute_group cpu_root_microcode_group
= {
625 .attrs
= cpu_root_microcode_attrs
,
628 int __init
microcode_init(void)
630 struct cpuinfo_x86
*c
= &boot_cpu_data
;
633 if (paravirt_enabled() || dis_ucode_ldr
)
636 if (c
->x86_vendor
== X86_VENDOR_INTEL
)
637 microcode_ops
= init_intel_microcode();
638 else if (c
->x86_vendor
== X86_VENDOR_AMD
)
639 microcode_ops
= init_amd_microcode();
641 pr_err("no support for this CPU vendor\n");
646 microcode_pdev
= platform_device_register_simple("microcode", -1,
648 if (IS_ERR(microcode_pdev
))
649 return PTR_ERR(microcode_pdev
);
652 mutex_lock(µcode_mutex
);
654 error
= subsys_interface_register(&mc_cpu_interface
);
656 perf_check_microcode();
657 mutex_unlock(µcode_mutex
);
663 error
= sysfs_create_group(&cpu_subsys
.dev_root
->kobj
,
664 &cpu_root_microcode_group
);
667 pr_err("Error creating microcode group!\n");
671 error
= microcode_dev_init();
673 goto out_ucode_group
;
675 register_syscore_ops(&mc_syscore_ops
);
676 register_hotcpu_notifier(&mc_cpu_notifier
);
678 pr_info("Microcode Update Driver: v" MICROCODE_VERSION
679 " <tigran@aivazian.fsnet.co.uk>, Peter Oruba\n");
684 sysfs_remove_group(&cpu_subsys
.dev_root
->kobj
,
685 &cpu_root_microcode_group
);
689 mutex_lock(µcode_mutex
);
691 subsys_interface_unregister(&mc_cpu_interface
);
693 mutex_unlock(µcode_mutex
);
697 platform_device_unregister(microcode_pdev
);
701 late_initcall(microcode_init
);