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