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