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