]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - arch/x86/kernel/cpu/microcode/core.c
Merge branch 'for-linus-4.10-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mirror_ubuntu-artful-kernel.git] / arch / x86 / kernel / cpu / microcode / core.c
1 /*
2 * CPU Microcode Update Driver for Linux
3 *
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>
7 *
8 * X86 CPU microcode early update for Linux:
9 *
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>
13 *
14 * This driver allows to upgrade microcode on x86 processors.
15 *
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.
20 */
21
22 #define pr_fmt(fmt) "microcode: " fmt
23
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>
34
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>
43
44 #define DRIVER_VERSION "2.2"
45
46 static struct microcode_ops *microcode_ops;
47 static bool dis_ucode_ldr;
48
49 LIST_HEAD(microcode_cache);
50
51 /*
52 * Synchronization.
53 *
54 * All non cpu-hotplug-callback call sites use:
55 *
56 * - microcode_mutex to synchronize with each other;
57 * - get/put_online_cpus() to synchronize with
58 * the cpu-hotplug-callback call sites.
59 *
60 * We guarantee that only a single cpu is being
61 * updated at any particular moment of time.
62 */
63 static DEFINE_MUTEX(microcode_mutex);
64
65 struct ucode_cpu_info ucode_cpu_info[NR_CPUS];
66
67 /*
68 * Operations that are run on a target cpu:
69 */
70
71 struct cpu_info_ctx {
72 struct cpu_signature *cpu_sig;
73 int err;
74 };
75
76 static bool __init check_loader_disabled_bsp(void)
77 {
78 static const char *__dis_opt_str = "dis_ucode_ldr";
79
80 #ifdef CONFIG_X86_32
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);
84
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;
89 #endif
90
91 if (cmdline_find_option_bool(cmdline, option))
92 *res = true;
93
94 return *res;
95 }
96
97 extern struct builtin_fw __start_builtin_fw[];
98 extern struct builtin_fw __end_builtin_fw[];
99
100 bool get_builtin_firmware(struct cpio_data *cd, const char *name)
101 {
102 #ifdef CONFIG_FW_LOADER
103 struct builtin_fw *b_fw;
104
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;
109 return true;
110 }
111 }
112 #endif
113 return false;
114 }
115
116 void __init load_ucode_bsp(void)
117 {
118 int vendor;
119 unsigned int family;
120
121 if (check_loader_disabled_bsp())
122 return;
123
124 if (!have_cpuid_p())
125 return;
126
127 vendor = x86_cpuid_vendor();
128 family = x86_cpuid_family();
129
130 switch (vendor) {
131 case X86_VENDOR_INTEL:
132 if (family >= 6)
133 load_ucode_intel_bsp();
134 break;
135 case X86_VENDOR_AMD:
136 if (family >= 0x10)
137 load_ucode_amd_bsp(family);
138 break;
139 default:
140 break;
141 }
142 }
143
144 static bool check_loader_disabled_ap(void)
145 {
146 #ifdef CONFIG_X86_32
147 return *((bool *)__pa_nodebug(&dis_ucode_ldr));
148 #else
149 return dis_ucode_ldr;
150 #endif
151 }
152
153 void load_ucode_ap(void)
154 {
155 int vendor, family;
156
157 if (check_loader_disabled_ap())
158 return;
159
160 if (!have_cpuid_p())
161 return;
162
163 vendor = x86_cpuid_vendor();
164 family = x86_cpuid_family();
165
166 switch (vendor) {
167 case X86_VENDOR_INTEL:
168 if (family >= 6)
169 load_ucode_intel_ap();
170 break;
171 case X86_VENDOR_AMD:
172 if (family >= 0x10)
173 load_ucode_amd_ap(family);
174 break;
175 default:
176 break;
177 }
178 }
179
180 static int __init save_microcode_in_initrd(void)
181 {
182 struct cpuinfo_x86 *c = &boot_cpu_data;
183
184 switch (c->x86_vendor) {
185 case X86_VENDOR_INTEL:
186 if (c->x86 >= 6)
187 return save_microcode_in_initrd_intel();
188 break;
189 case X86_VENDOR_AMD:
190 if (c->x86 >= 0x10)
191 return save_microcode_in_initrd_amd(c->x86);
192 break;
193 default:
194 break;
195 }
196
197 return -EINVAL;
198 }
199
200 struct cpio_data find_microcode_in_initrd(const char *path, bool use_pa)
201 {
202 #ifdef CONFIG_BLK_DEV_INITRD
203 unsigned long start = 0;
204 size_t size;
205
206 #ifdef CONFIG_X86_32
207 struct boot_params *params;
208
209 if (use_pa)
210 params = (struct boot_params *)__pa_nodebug(&boot_params);
211 else
212 params = &boot_params;
213
214 size = params->hdr.ramdisk_size;
215
216 /*
217 * Set start only if we have an initrd image. We cannot use initrd_start
218 * because it is not set that early yet.
219 */
220 if (size)
221 start = params->hdr.ramdisk_image;
222
223 # else /* CONFIG_X86_64 */
224 size = (unsigned long)boot_params.ext_ramdisk_size << 32;
225 size |= boot_params.hdr.ramdisk_size;
226
227 if (size) {
228 start = (unsigned long)boot_params.ext_ramdisk_image << 32;
229 start |= boot_params.hdr.ramdisk_image;
230
231 start += PAGE_OFFSET;
232 }
233 # endif
234
235 /*
236 * Did we relocate the ramdisk?
237 *
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.
242 */
243 if (!use_pa && relocated_ramdisk)
244 start = initrd_start;
245
246 return find_cpio_data(path, (void *)start, size, NULL);
247 #else /* !CONFIG_BLK_DEV_INITRD */
248 return (struct cpio_data){ NULL, 0, "" };
249 #endif
250 }
251
252 void reload_early_microcode(void)
253 {
254 int vendor, family;
255
256 vendor = x86_cpuid_vendor();
257 family = x86_cpuid_family();
258
259 switch (vendor) {
260 case X86_VENDOR_INTEL:
261 if (family >= 6)
262 reload_ucode_intel();
263 break;
264 case X86_VENDOR_AMD:
265 if (family >= 0x10)
266 reload_ucode_amd();
267 break;
268 default:
269 break;
270 }
271 }
272
273 static void collect_cpu_info_local(void *arg)
274 {
275 struct cpu_info_ctx *ctx = arg;
276
277 ctx->err = microcode_ops->collect_cpu_info(smp_processor_id(),
278 ctx->cpu_sig);
279 }
280
281 static int collect_cpu_info_on_target(int cpu, struct cpu_signature *cpu_sig)
282 {
283 struct cpu_info_ctx ctx = { .cpu_sig = cpu_sig, .err = 0 };
284 int ret;
285
286 ret = smp_call_function_single(cpu, collect_cpu_info_local, &ctx, 1);
287 if (!ret)
288 ret = ctx.err;
289
290 return ret;
291 }
292
293 static int collect_cpu_info(int cpu)
294 {
295 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
296 int ret;
297
298 memset(uci, 0, sizeof(*uci));
299
300 ret = collect_cpu_info_on_target(cpu, &uci->cpu_sig);
301 if (!ret)
302 uci->valid = 1;
303
304 return ret;
305 }
306
307 struct apply_microcode_ctx {
308 int err;
309 };
310
311 static void apply_microcode_local(void *arg)
312 {
313 struct apply_microcode_ctx *ctx = arg;
314
315 ctx->err = microcode_ops->apply_microcode(smp_processor_id());
316 }
317
318 static int apply_microcode_on_target(int cpu)
319 {
320 struct apply_microcode_ctx ctx = { .err = 0 };
321 int ret;
322
323 ret = smp_call_function_single(cpu, apply_microcode_local, &ctx, 1);
324 if (!ret)
325 ret = ctx.err;
326
327 return ret;
328 }
329
330 #ifdef CONFIG_MICROCODE_OLD_INTERFACE
331 static int do_microcode_update(const void __user *buf, size_t size)
332 {
333 int error = 0;
334 int cpu;
335
336 for_each_online_cpu(cpu) {
337 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
338 enum ucode_state ustate;
339
340 if (!uci->valid)
341 continue;
342
343 ustate = microcode_ops->request_microcode_user(cpu, buf, size);
344 if (ustate == UCODE_ERROR) {
345 error = -1;
346 break;
347 } else if (ustate == UCODE_OK)
348 apply_microcode_on_target(cpu);
349 }
350
351 return error;
352 }
353
354 static int microcode_open(struct inode *inode, struct file *file)
355 {
356 return capable(CAP_SYS_RAWIO) ? nonseekable_open(inode, file) : -EPERM;
357 }
358
359 static ssize_t microcode_write(struct file *file, const char __user *buf,
360 size_t len, loff_t *ppos)
361 {
362 ssize_t ret = -EINVAL;
363
364 if ((len >> PAGE_SHIFT) > totalram_pages) {
365 pr_err("too much data (max %ld pages)\n", totalram_pages);
366 return ret;
367 }
368
369 get_online_cpus();
370 mutex_lock(&microcode_mutex);
371
372 if (do_microcode_update(buf, len) == 0)
373 ret = (ssize_t)len;
374
375 if (ret > 0)
376 perf_check_microcode();
377
378 mutex_unlock(&microcode_mutex);
379 put_online_cpus();
380
381 return ret;
382 }
383
384 static const struct file_operations microcode_fops = {
385 .owner = THIS_MODULE,
386 .write = microcode_write,
387 .open = microcode_open,
388 .llseek = no_llseek,
389 };
390
391 static struct miscdevice microcode_dev = {
392 .minor = MICROCODE_MINOR,
393 .name = "microcode",
394 .nodename = "cpu/microcode",
395 .fops = &microcode_fops,
396 };
397
398 static int __init microcode_dev_init(void)
399 {
400 int error;
401
402 error = misc_register(&microcode_dev);
403 if (error) {
404 pr_err("can't misc_register on minor=%d\n", MICROCODE_MINOR);
405 return error;
406 }
407
408 return 0;
409 }
410
411 static void __exit microcode_dev_exit(void)
412 {
413 misc_deregister(&microcode_dev);
414 }
415 #else
416 #define microcode_dev_init() 0
417 #define microcode_dev_exit() do { } while (0)
418 #endif
419
420 /* fake device for request_firmware */
421 static struct platform_device *microcode_pdev;
422
423 static int reload_for_cpu(int cpu)
424 {
425 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
426 enum ucode_state ustate;
427 int err = 0;
428
429 if (!uci->valid)
430 return err;
431
432 ustate = microcode_ops->request_microcode_fw(cpu, &microcode_pdev->dev, true);
433 if (ustate == UCODE_OK)
434 apply_microcode_on_target(cpu);
435 else
436 if (ustate == UCODE_ERROR)
437 err = -EINVAL;
438 return err;
439 }
440
441 static ssize_t reload_store(struct device *dev,
442 struct device_attribute *attr,
443 const char *buf, size_t size)
444 {
445 unsigned long val;
446 int cpu;
447 ssize_t ret = 0, tmp_ret;
448
449 ret = kstrtoul(buf, 0, &val);
450 if (ret)
451 return ret;
452
453 if (val != 1)
454 return size;
455
456 get_online_cpus();
457 mutex_lock(&microcode_mutex);
458 for_each_online_cpu(cpu) {
459 tmp_ret = reload_for_cpu(cpu);
460 if (tmp_ret != 0)
461 pr_warn("Error reloading microcode on CPU %d\n", cpu);
462
463 /* save retval of the first encountered reload error */
464 if (!ret)
465 ret = tmp_ret;
466 }
467 if (!ret)
468 perf_check_microcode();
469 mutex_unlock(&microcode_mutex);
470 put_online_cpus();
471
472 if (!ret)
473 ret = size;
474
475 return ret;
476 }
477
478 static ssize_t version_show(struct device *dev,
479 struct device_attribute *attr, char *buf)
480 {
481 struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
482
483 return sprintf(buf, "0x%x\n", uci->cpu_sig.rev);
484 }
485
486 static ssize_t pf_show(struct device *dev,
487 struct device_attribute *attr, char *buf)
488 {
489 struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
490
491 return sprintf(buf, "0x%x\n", uci->cpu_sig.pf);
492 }
493
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);
497
498 static struct attribute *mc_default_attrs[] = {
499 &dev_attr_version.attr,
500 &dev_attr_processor_flags.attr,
501 NULL
502 };
503
504 static struct attribute_group mc_attr_group = {
505 .attrs = mc_default_attrs,
506 .name = "microcode",
507 };
508
509 static void microcode_fini_cpu(int cpu)
510 {
511 if (microcode_ops->microcode_fini_cpu)
512 microcode_ops->microcode_fini_cpu(cpu);
513 }
514
515 static enum ucode_state microcode_resume_cpu(int cpu)
516 {
517 if (apply_microcode_on_target(cpu))
518 return UCODE_ERROR;
519
520 pr_debug("CPU%d updated upon resume\n", cpu);
521
522 return UCODE_OK;
523 }
524
525 static enum ucode_state microcode_init_cpu(int cpu, bool refresh_fw)
526 {
527 enum ucode_state ustate;
528 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
529
530 if (uci->valid)
531 return UCODE_OK;
532
533 if (collect_cpu_info(cpu))
534 return UCODE_ERROR;
535
536 /* --dimm. Trigger a delayed update? */
537 if (system_state != SYSTEM_RUNNING)
538 return UCODE_NFOUND;
539
540 ustate = microcode_ops->request_microcode_fw(cpu, &microcode_pdev->dev,
541 refresh_fw);
542
543 if (ustate == UCODE_OK) {
544 pr_debug("CPU%d updated upon init\n", cpu);
545 apply_microcode_on_target(cpu);
546 }
547
548 return ustate;
549 }
550
551 static enum ucode_state microcode_update_cpu(int cpu)
552 {
553 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
554
555 /* Refresh CPU microcode revision after resume. */
556 collect_cpu_info(cpu);
557
558 if (uci->valid)
559 return microcode_resume_cpu(cpu);
560
561 return microcode_init_cpu(cpu, false);
562 }
563
564 static int mc_device_add(struct device *dev, struct subsys_interface *sif)
565 {
566 int err, cpu = dev->id;
567
568 if (!cpu_online(cpu))
569 return 0;
570
571 pr_debug("CPU%d added\n", cpu);
572
573 err = sysfs_create_group(&dev->kobj, &mc_attr_group);
574 if (err)
575 return err;
576
577 if (microcode_init_cpu(cpu, true) == UCODE_ERROR)
578 return -EINVAL;
579
580 return err;
581 }
582
583 static void mc_device_remove(struct device *dev, struct subsys_interface *sif)
584 {
585 int cpu = dev->id;
586
587 if (!cpu_online(cpu))
588 return;
589
590 pr_debug("CPU%d removed\n", cpu);
591 microcode_fini_cpu(cpu);
592 sysfs_remove_group(&dev->kobj, &mc_attr_group);
593 }
594
595 static struct subsys_interface mc_cpu_interface = {
596 .name = "microcode",
597 .subsys = &cpu_subsys,
598 .add_dev = mc_device_add,
599 .remove_dev = mc_device_remove,
600 };
601
602 /**
603 * mc_bp_resume - Update boot CPU microcode during resume.
604 */
605 static void mc_bp_resume(void)
606 {
607 int cpu = smp_processor_id();
608 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
609
610 if (uci->valid && uci->mc)
611 microcode_ops->apply_microcode(cpu);
612 else if (!uci->mc)
613 reload_early_microcode();
614 }
615
616 static struct syscore_ops mc_syscore_ops = {
617 .resume = mc_bp_resume,
618 };
619
620 static int mc_cpu_online(unsigned int cpu)
621 {
622 struct device *dev;
623
624 dev = get_cpu_device(cpu);
625 microcode_update_cpu(cpu);
626 pr_debug("CPU%d added\n", cpu);
627
628 if (sysfs_create_group(&dev->kobj, &mc_attr_group))
629 pr_err("Failed to create group for CPU%d\n", cpu);
630 return 0;
631 }
632
633 static int mc_cpu_down_prep(unsigned int cpu)
634 {
635 struct device *dev;
636
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);
641
642 return 0;
643 }
644
645 static struct attribute *cpu_root_microcode_attrs[] = {
646 &dev_attr_reload.attr,
647 NULL
648 };
649
650 static struct attribute_group cpu_root_microcode_group = {
651 .name = "microcode",
652 .attrs = cpu_root_microcode_attrs,
653 };
654
655 int __init microcode_init(void)
656 {
657 struct cpuinfo_x86 *c = &boot_cpu_data;
658 int error;
659
660 if (dis_ucode_ldr)
661 return -EINVAL;
662
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();
667 else
668 pr_err("no support for this CPU vendor\n");
669
670 if (!microcode_ops)
671 return -ENODEV;
672
673 microcode_pdev = platform_device_register_simple("microcode", -1,
674 NULL, 0);
675 if (IS_ERR(microcode_pdev))
676 return PTR_ERR(microcode_pdev);
677
678 get_online_cpus();
679 mutex_lock(&microcode_mutex);
680
681 error = subsys_interface_register(&mc_cpu_interface);
682 if (!error)
683 perf_check_microcode();
684 mutex_unlock(&microcode_mutex);
685 put_online_cpus();
686
687 if (error)
688 goto out_pdev;
689
690 error = sysfs_create_group(&cpu_subsys.dev_root->kobj,
691 &cpu_root_microcode_group);
692
693 if (error) {
694 pr_err("Error creating microcode group!\n");
695 goto out_driver;
696 }
697
698 error = microcode_dev_init();
699 if (error)
700 goto out_ucode_group;
701
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);
705
706 pr_info("Microcode Update Driver: v%s.", DRIVER_VERSION);
707
708 return 0;
709
710 out_ucode_group:
711 sysfs_remove_group(&cpu_subsys.dev_root->kobj,
712 &cpu_root_microcode_group);
713
714 out_driver:
715 get_online_cpus();
716 mutex_lock(&microcode_mutex);
717
718 subsys_interface_unregister(&mc_cpu_interface);
719
720 mutex_unlock(&microcode_mutex);
721 put_online_cpus();
722
723 out_pdev:
724 platform_device_unregister(microcode_pdev);
725 return error;
726
727 }
728 fs_initcall(save_microcode_in_initrd);
729 late_initcall(microcode_init);