of: MSI: Simplify irqdomain lookup
[linux/fpc-iii.git] / arch / x86 / kernel / cpu / microcode / core.c
blobfaec7120c508584c9f038445c0dae593cfb5faae
1 /*
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>
32 #include <linux/fs.h>
33 #include <linux/mm.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)
51 dis_ucode_ldr = true;
52 return 1;
54 __setup("dis_ucode_ldr", disable_loader);
57 * Synchronization.
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:
77 struct cpu_info_ctx {
78 struct cpu_signature *cpu_sig;
79 int err;
82 static bool __init check_loader_disabled_bsp(void)
84 #ifdef CONFIG_X86_32
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;
94 #endif
96 if (cmdline_find_option_bool(cmdline, option))
97 *res = true;
99 return *res;
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;
114 return true;
117 #endif
118 return false;
121 void __init load_ucode_bsp(void)
123 int vendor;
124 unsigned int family;
126 if (check_loader_disabled_bsp())
127 return;
129 if (!have_cpuid_p())
130 return;
132 vendor = x86_cpuid_vendor();
133 family = x86_cpuid_family();
135 switch (vendor) {
136 case X86_VENDOR_INTEL:
137 if (family >= 6)
138 load_ucode_intel_bsp();
139 break;
140 case X86_VENDOR_AMD:
141 if (family >= 0x10)
142 load_ucode_amd_bsp(family);
143 break;
144 default:
145 break;
149 static bool check_loader_disabled_ap(void)
151 #ifdef CONFIG_X86_32
152 return *((bool *)__pa_nodebug(&dis_ucode_ldr));
153 #else
154 return dis_ucode_ldr;
155 #endif
158 void load_ucode_ap(void)
160 int vendor, family;
162 if (check_loader_disabled_ap())
163 return;
165 if (!have_cpuid_p())
166 return;
168 vendor = x86_cpuid_vendor();
169 family = x86_cpuid_family();
171 switch (vendor) {
172 case X86_VENDOR_INTEL:
173 if (family >= 6)
174 load_ucode_intel_ap();
175 break;
176 case X86_VENDOR_AMD:
177 if (family >= 0x10)
178 load_ucode_amd_ap();
179 break;
180 default:
181 break;
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:
191 if (c->x86 >= 6)
192 save_microcode_in_initrd_intel();
193 break;
194 case X86_VENDOR_AMD:
195 if (c->x86 >= 0x10)
196 save_microcode_in_initrd_amd();
197 break;
198 default:
199 break;
202 return 0;
205 void reload_early_microcode(void)
207 int vendor, family;
209 vendor = x86_cpuid_vendor();
210 family = x86_cpuid_family();
212 switch (vendor) {
213 case X86_VENDOR_INTEL:
214 if (family >= 6)
215 reload_ucode_intel();
216 break;
217 case X86_VENDOR_AMD:
218 if (family >= 0x10)
219 reload_ucode_amd();
220 break;
221 default:
222 break;
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(),
231 ctx->cpu_sig);
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 };
237 int ret;
239 ret = smp_call_function_single(cpu, collect_cpu_info_local, &ctx, 1);
240 if (!ret)
241 ret = ctx.err;
243 return ret;
246 static int collect_cpu_info(int cpu)
248 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
249 int ret;
251 memset(uci, 0, sizeof(*uci));
253 ret = collect_cpu_info_on_target(cpu, &uci->cpu_sig);
254 if (!ret)
255 uci->valid = 1;
257 return ret;
260 struct apply_microcode_ctx {
261 int err;
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 };
274 int ret;
276 ret = smp_call_function_single(cpu, apply_microcode_local, &ctx, 1);
277 if (!ret)
278 ret = ctx.err;
280 return ret;
283 #ifdef CONFIG_MICROCODE_OLD_INTERFACE
284 static int do_microcode_update(const void __user *buf, size_t size)
286 int error = 0;
287 int cpu;
289 for_each_online_cpu(cpu) {
290 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
291 enum ucode_state ustate;
293 if (!uci->valid)
294 continue;
296 ustate = microcode_ops->request_microcode_user(cpu, buf, size);
297 if (ustate == UCODE_ERROR) {
298 error = -1;
299 break;
300 } else if (ustate == UCODE_OK)
301 apply_microcode_on_target(cpu);
304 return error;
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);
319 return ret;
322 get_online_cpus();
323 mutex_lock(&microcode_mutex);
325 if (do_microcode_update(buf, len) == 0)
326 ret = (ssize_t)len;
328 if (ret > 0)
329 perf_check_microcode();
331 mutex_unlock(&microcode_mutex);
332 put_online_cpus();
334 return ret;
337 static const struct file_operations microcode_fops = {
338 .owner = THIS_MODULE,
339 .write = microcode_write,
340 .open = microcode_open,
341 .llseek = no_llseek,
344 static struct miscdevice microcode_dev = {
345 .minor = MICROCODE_MINOR,
346 .name = "microcode",
347 .nodename = "cpu/microcode",
348 .fops = &microcode_fops,
351 static int __init microcode_dev_init(void)
353 int error;
355 error = misc_register(&microcode_dev);
356 if (error) {
357 pr_err("can't misc_register on minor=%d\n", MICROCODE_MINOR);
358 return error;
361 return 0;
364 static void __exit microcode_dev_exit(void)
366 misc_deregister(&microcode_dev);
368 #else
369 #define microcode_dev_init() 0
370 #define microcode_dev_exit() do { } while (0)
371 #endif
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;
380 int err = 0;
382 if (!uci->valid)
383 return err;
385 ustate = microcode_ops->request_microcode_fw(cpu, &microcode_pdev->dev, true);
386 if (ustate == UCODE_OK)
387 apply_microcode_on_target(cpu);
388 else
389 if (ustate == UCODE_ERROR)
390 err = -EINVAL;
391 return err;
394 static ssize_t reload_store(struct device *dev,
395 struct device_attribute *attr,
396 const char *buf, size_t size)
398 unsigned long val;
399 int cpu;
400 ssize_t ret = 0, tmp_ret;
402 ret = kstrtoul(buf, 0, &val);
403 if (ret)
404 return ret;
406 if (val != 1)
407 return size;
409 get_online_cpus();
410 mutex_lock(&microcode_mutex);
411 for_each_online_cpu(cpu) {
412 tmp_ret = reload_for_cpu(cpu);
413 if (tmp_ret != 0)
414 pr_warn("Error reloading microcode on CPU %d\n", cpu);
416 /* save retval of the first encountered reload error */
417 if (!ret)
418 ret = tmp_ret;
420 if (!ret)
421 perf_check_microcode();
422 mutex_unlock(&microcode_mutex);
423 put_online_cpus();
425 if (!ret)
426 ret = size;
428 return ret;
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,
454 NULL
457 static struct attribute_group mc_attr_group = {
458 .attrs = mc_default_attrs,
459 .name = "microcode",
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))
472 return UCODE_ERROR;
474 return UCODE_OK;
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)
483 return UCODE_OK;
485 if (collect_cpu_info(cpu))
486 return UCODE_ERROR;
488 /* --dimm. Trigger a delayed update? */
489 if (system_state != SYSTEM_RUNNING)
490 return UCODE_NFOUND;
492 ustate = microcode_ops->request_microcode_fw(cpu, &microcode_pdev->dev,
493 refresh_fw);
495 if (ustate == UCODE_OK) {
496 pr_debug("CPU%d updated upon init\n", cpu);
497 apply_microcode_on_target(cpu);
500 return ustate;
503 static enum ucode_state microcode_update_cpu(int cpu)
505 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
507 if (uci->valid)
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))
518 return 0;
520 pr_debug("CPU%d added\n", cpu);
522 err = sysfs_create_group(&dev->kobj, &mc_attr_group);
523 if (err)
524 return err;
526 if (microcode_init_cpu(cpu, true) == UCODE_ERROR)
527 return -EINVAL;
529 return err;
532 static void mc_device_remove(struct device *dev, struct subsys_interface *sif)
534 int cpu = dev->id;
536 if (!cpu_online(cpu))
537 return;
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 = {
545 .name = "microcode",
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);
561 else if (!uci->mc)
562 reload_early_microcode();
565 static struct syscore_ops mc_syscore_ops = {
566 .resume = mc_bp_resume,
569 static int
570 mc_cpu_callback(struct notifier_block *nb, unsigned long action, void *hcpu)
572 unsigned int cpu = (unsigned long)hcpu;
573 struct device *dev;
575 dev = get_cpu_device(cpu);
577 switch (action & ~CPU_TASKS_FROZEN) {
578 case CPU_ONLINE:
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);
589 break;
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);
595 break;
598 * case CPU_DEAD:
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
603 * for it again.
607 /* The CPU refused to come up during a system resume */
608 if (action == CPU_UP_CANCELED_FROZEN)
609 microcode_fini_cpu(cpu);
611 return NOTIFY_OK;
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,
620 NULL
623 static struct attribute_group cpu_root_microcode_group = {
624 .name = "microcode",
625 .attrs = cpu_root_microcode_attrs,
628 int __init microcode_init(void)
630 struct cpuinfo_x86 *c = &boot_cpu_data;
631 int error;
633 if (paravirt_enabled() || dis_ucode_ldr)
634 return -EINVAL;
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();
640 else
641 pr_err("no support for this CPU vendor\n");
643 if (!microcode_ops)
644 return -ENODEV;
646 microcode_pdev = platform_device_register_simple("microcode", -1,
647 NULL, 0);
648 if (IS_ERR(microcode_pdev))
649 return PTR_ERR(microcode_pdev);
651 get_online_cpus();
652 mutex_lock(&microcode_mutex);
654 error = subsys_interface_register(&mc_cpu_interface);
655 if (!error)
656 perf_check_microcode();
657 mutex_unlock(&microcode_mutex);
658 put_online_cpus();
660 if (error)
661 goto out_pdev;
663 error = sysfs_create_group(&cpu_subsys.dev_root->kobj,
664 &cpu_root_microcode_group);
666 if (error) {
667 pr_err("Error creating microcode group!\n");
668 goto out_driver;
671 error = microcode_dev_init();
672 if (error)
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");
681 return 0;
683 out_ucode_group:
684 sysfs_remove_group(&cpu_subsys.dev_root->kobj,
685 &cpu_root_microcode_group);
687 out_driver:
688 get_online_cpus();
689 mutex_lock(&microcode_mutex);
691 subsys_interface_unregister(&mc_cpu_interface);
693 mutex_unlock(&microcode_mutex);
694 put_online_cpus();
696 out_pdev:
697 platform_device_unregister(microcode_pdev);
698 return error;
701 late_initcall(microcode_init);