xref: /linux-6.15/arch/x86/kernel/cpu/microcode/core.c (revision 1582c0f4)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * CPU Microcode Update Driver for Linux
4  *
5  * Copyright (C) 2000-2006 Tigran Aivazian <[email protected]>
6  *	      2006	Shaohua Li <[email protected]>
7  *	      2013-2016	Borislav Petkov <[email protected]>
8  *
9  * X86 CPU microcode early update for Linux:
10  *
11  *	Copyright (C) 2012 Fenghua Yu <[email protected]>
12  *			   H Peter Anvin" <[email protected]>
13  *		  (C) 2015 Borislav Petkov <[email protected]>
14  *
15  * This driver allows to upgrade microcode on x86 processors.
16  */
17 
18 #define pr_fmt(fmt) "microcode: " fmt
19 
20 #include <linux/platform_device.h>
21 #include <linux/stop_machine.h>
22 #include <linux/syscore_ops.h>
23 #include <linux/miscdevice.h>
24 #include <linux/capability.h>
25 #include <linux/firmware.h>
26 #include <linux/cpumask.h>
27 #include <linux/kernel.h>
28 #include <linux/delay.h>
29 #include <linux/mutex.h>
30 #include <linux/cpu.h>
31 #include <linux/nmi.h>
32 #include <linux/fs.h>
33 #include <linux/mm.h>
34 
35 #include <asm/apic.h>
36 #include <asm/cpu_device_id.h>
37 #include <asm/perf_event.h>
38 #include <asm/processor.h>
39 #include <asm/cmdline.h>
40 #include <asm/setup.h>
41 
42 #include "internal.h"
43 
44 #define DRIVER_VERSION	"2.2"
45 
46 static struct microcode_ops	*microcode_ops;
47 bool dis_ucode_ldr = true;
48 
49 /*
50  * Synchronization.
51  *
52  * All non cpu-hotplug-callback call sites use:
53  *
54  * - cpus_read_lock/unlock() 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 struct ucode_cpu_info		ucode_cpu_info[NR_CPUS];
61 
62 struct cpu_info_ctx {
63 	struct cpu_signature	*cpu_sig;
64 	int			err;
65 };
66 
67 /*
68  * Those patch levels cannot be updated to newer ones and thus should be final.
69  */
70 static u32 final_levels[] = {
71 	0x01000098,
72 	0x0100009f,
73 	0x010000af,
74 	0, /* T-101 terminator */
75 };
76 
77 /*
78  * Check the current patch level on this CPU.
79  *
80  * Returns:
81  *  - true: if update should stop
82  *  - false: otherwise
83  */
84 static bool amd_check_current_patch_level(void)
85 {
86 	u32 lvl, dummy, i;
87 	u32 *levels;
88 
89 	native_rdmsr(MSR_AMD64_PATCH_LEVEL, lvl, dummy);
90 
91 	levels = final_levels;
92 
93 	for (i = 0; levels[i]; i++) {
94 		if (lvl == levels[i])
95 			return true;
96 	}
97 	return false;
98 }
99 
100 static bool __init check_loader_disabled_bsp(void)
101 {
102 	static const char *__dis_opt_str = "dis_ucode_ldr";
103 	const char *cmdline = boot_command_line;
104 	const char *option  = __dis_opt_str;
105 
106 	/*
107 	 * CPUID(1).ECX[31]: reserved for hypervisor use. This is still not
108 	 * completely accurate as xen pv guests don't see that CPUID bit set but
109 	 * that's good enough as they don't land on the BSP path anyway.
110 	 */
111 	if (native_cpuid_ecx(1) & BIT(31))
112 		return true;
113 
114 	if (x86_cpuid_vendor() == X86_VENDOR_AMD) {
115 		if (amd_check_current_patch_level())
116 			return true;
117 	}
118 
119 	if (cmdline_find_option_bool(cmdline, option) <= 0)
120 		dis_ucode_ldr = false;
121 
122 	return dis_ucode_ldr;
123 }
124 
125 void __init load_ucode_bsp(void)
126 {
127 	unsigned int cpuid_1_eax;
128 	bool intel = true;
129 
130 	if (!have_cpuid_p())
131 		return;
132 
133 	cpuid_1_eax = native_cpuid_eax(1);
134 
135 	switch (x86_cpuid_vendor()) {
136 	case X86_VENDOR_INTEL:
137 		if (x86_family(cpuid_1_eax) < 6)
138 			return;
139 		break;
140 
141 	case X86_VENDOR_AMD:
142 		if (x86_family(cpuid_1_eax) < 0x10)
143 			return;
144 		intel = false;
145 		break;
146 
147 	default:
148 		return;
149 	}
150 
151 	if (check_loader_disabled_bsp())
152 		return;
153 
154 	if (intel)
155 		load_ucode_intel_bsp();
156 	else
157 		load_ucode_amd_bsp(cpuid_1_eax);
158 }
159 
160 void load_ucode_ap(void)
161 {
162 	unsigned int cpuid_1_eax;
163 
164 	if (dis_ucode_ldr)
165 		return;
166 
167 	cpuid_1_eax = native_cpuid_eax(1);
168 
169 	switch (x86_cpuid_vendor()) {
170 	case X86_VENDOR_INTEL:
171 		if (x86_family(cpuid_1_eax) >= 6)
172 			load_ucode_intel_ap();
173 		break;
174 	case X86_VENDOR_AMD:
175 		if (x86_family(cpuid_1_eax) >= 0x10)
176 			load_ucode_amd_ap(cpuid_1_eax);
177 		break;
178 	default:
179 		break;
180 	}
181 }
182 
183 struct cpio_data __init find_microcode_in_initrd(const char *path)
184 {
185 #ifdef CONFIG_BLK_DEV_INITRD
186 	unsigned long start = 0;
187 	size_t size;
188 
189 #ifdef CONFIG_X86_32
190 	size = boot_params.hdr.ramdisk_size;
191 	/* Early load on BSP has a temporary mapping. */
192 	if (size)
193 		start = initrd_start_early;
194 
195 #else /* CONFIG_X86_64 */
196 	size  = (unsigned long)boot_params.ext_ramdisk_size << 32;
197 	size |= boot_params.hdr.ramdisk_size;
198 
199 	if (size) {
200 		start  = (unsigned long)boot_params.ext_ramdisk_image << 32;
201 		start |= boot_params.hdr.ramdisk_image;
202 		start += PAGE_OFFSET;
203 	}
204 #endif
205 
206 	/*
207 	 * Fixup the start address: after reserve_initrd() runs, initrd_start
208 	 * has the virtual address of the beginning of the initrd. It also
209 	 * possibly relocates the ramdisk. In either case, initrd_start contains
210 	 * the updated address so use that instead.
211 	 */
212 	if (initrd_start)
213 		start = initrd_start;
214 
215 	return find_cpio_data(path, (void *)start, size, NULL);
216 #else /* !CONFIG_BLK_DEV_INITRD */
217 	return (struct cpio_data){ NULL, 0, "" };
218 #endif
219 }
220 
221 static void reload_early_microcode(unsigned int cpu)
222 {
223 	int vendor, family;
224 
225 	vendor = x86_cpuid_vendor();
226 	family = x86_cpuid_family();
227 
228 	switch (vendor) {
229 	case X86_VENDOR_INTEL:
230 		if (family >= 6)
231 			reload_ucode_intel();
232 		break;
233 	case X86_VENDOR_AMD:
234 		if (family >= 0x10)
235 			reload_ucode_amd(cpu);
236 		break;
237 	default:
238 		break;
239 	}
240 }
241 
242 /* fake device for request_firmware */
243 static struct platform_device	*microcode_pdev;
244 
245 #ifdef CONFIG_MICROCODE_LATE_LOADING
246 /*
247  * Late loading dance. Why the heavy-handed stomp_machine effort?
248  *
249  * - HT siblings must be idle and not execute other code while the other sibling
250  *   is loading microcode in order to avoid any negative interactions caused by
251  *   the loading.
252  *
253  * - In addition, microcode update on the cores must be serialized until this
254  *   requirement can be relaxed in the future. Right now, this is conservative
255  *   and good.
256  */
257 enum sibling_ctrl {
258 	/* Spinwait with timeout */
259 	SCTRL_WAIT,
260 	/* Invoke the microcode_apply() callback */
261 	SCTRL_APPLY,
262 	/* Proceed without invoking the microcode_apply() callback */
263 	SCTRL_DONE,
264 };
265 
266 struct microcode_ctrl {
267 	enum sibling_ctrl	ctrl;
268 	enum ucode_state	result;
269 	unsigned int		ctrl_cpu;
270 	bool			nmi_enabled;
271 };
272 
273 DEFINE_STATIC_KEY_FALSE(microcode_nmi_handler_enable);
274 static DEFINE_PER_CPU(struct microcode_ctrl, ucode_ctrl);
275 static unsigned int loops_per_usec;
276 static atomic_t late_cpus_in;
277 
278 static noinstr bool wait_for_cpus(atomic_t *cnt)
279 {
280 	unsigned int timeout, loops;
281 
282 	WARN_ON_ONCE(raw_atomic_dec_return(cnt) < 0);
283 
284 	for (timeout = 0; timeout < USEC_PER_SEC; timeout++) {
285 		if (!raw_atomic_read(cnt))
286 			return true;
287 
288 		for (loops = 0; loops < loops_per_usec; loops++)
289 			cpu_relax();
290 
291 		/* If invoked directly, tickle the NMI watchdog */
292 		if (!microcode_ops->use_nmi && !(timeout % USEC_PER_MSEC)) {
293 			instrumentation_begin();
294 			touch_nmi_watchdog();
295 			instrumentation_end();
296 		}
297 	}
298 	/* Prevent the late comers from making progress and let them time out */
299 	raw_atomic_inc(cnt);
300 	return false;
301 }
302 
303 static noinstr bool wait_for_ctrl(void)
304 {
305 	unsigned int timeout, loops;
306 
307 	for (timeout = 0; timeout < USEC_PER_SEC; timeout++) {
308 		if (raw_cpu_read(ucode_ctrl.ctrl) != SCTRL_WAIT)
309 			return true;
310 
311 		for (loops = 0; loops < loops_per_usec; loops++)
312 			cpu_relax();
313 
314 		/* If invoked directly, tickle the NMI watchdog */
315 		if (!microcode_ops->use_nmi && !(timeout % USEC_PER_MSEC)) {
316 			instrumentation_begin();
317 			touch_nmi_watchdog();
318 			instrumentation_end();
319 		}
320 	}
321 	return false;
322 }
323 
324 /*
325  * Protected against instrumentation up to the point where the primary
326  * thread completed the update. See microcode_nmi_handler() for details.
327  */
328 static noinstr bool load_secondary_wait(unsigned int ctrl_cpu)
329 {
330 	/* Initial rendezvous to ensure that all CPUs have arrived */
331 	if (!wait_for_cpus(&late_cpus_in)) {
332 		raw_cpu_write(ucode_ctrl.result, UCODE_TIMEOUT);
333 		return false;
334 	}
335 
336 	/*
337 	 * Wait for primary threads to complete. If one of them hangs due
338 	 * to the update, there is no way out. This is non-recoverable
339 	 * because the CPU might hold locks or resources and confuse the
340 	 * scheduler, watchdogs etc. There is no way to safely evacuate the
341 	 * machine.
342 	 */
343 	if (wait_for_ctrl())
344 		return true;
345 
346 	instrumentation_begin();
347 	panic("Microcode load: Primary CPU %d timed out\n", ctrl_cpu);
348 	instrumentation_end();
349 }
350 
351 /*
352  * Protected against instrumentation up to the point where the primary
353  * thread completed the update. See microcode_nmi_handler() for details.
354  */
355 static noinstr void load_secondary(unsigned int cpu)
356 {
357 	unsigned int ctrl_cpu = raw_cpu_read(ucode_ctrl.ctrl_cpu);
358 	enum ucode_state ret;
359 
360 	if (!load_secondary_wait(ctrl_cpu)) {
361 		instrumentation_begin();
362 		pr_err_once("load: %d CPUs timed out\n",
363 			    atomic_read(&late_cpus_in) - 1);
364 		instrumentation_end();
365 		return;
366 	}
367 
368 	/* Primary thread completed. Allow to invoke instrumentable code */
369 	instrumentation_begin();
370 	/*
371 	 * If the primary succeeded then invoke the apply() callback,
372 	 * otherwise copy the state from the primary thread.
373 	 */
374 	if (this_cpu_read(ucode_ctrl.ctrl) == SCTRL_APPLY)
375 		ret = microcode_ops->apply_microcode(cpu);
376 	else
377 		ret = per_cpu(ucode_ctrl.result, ctrl_cpu);
378 
379 	this_cpu_write(ucode_ctrl.result, ret);
380 	this_cpu_write(ucode_ctrl.ctrl, SCTRL_DONE);
381 	instrumentation_end();
382 }
383 
384 static void load_primary(unsigned int cpu)
385 {
386 	struct cpumask *secondaries = topology_sibling_cpumask(cpu);
387 	enum sibling_ctrl ctrl;
388 	enum ucode_state ret;
389 	unsigned int sibling;
390 
391 	/* Initial rendezvous to ensure that all CPUs have arrived */
392 	if (!wait_for_cpus(&late_cpus_in)) {
393 		this_cpu_write(ucode_ctrl.result, UCODE_TIMEOUT);
394 		pr_err_once("load: %d CPUs timed out\n", atomic_read(&late_cpus_in) - 1);
395 		return;
396 	}
397 
398 	ret = microcode_ops->apply_microcode(cpu);
399 	this_cpu_write(ucode_ctrl.result, ret);
400 	this_cpu_write(ucode_ctrl.ctrl, SCTRL_DONE);
401 
402 	/*
403 	 * If the update was successful, let the siblings run the apply()
404 	 * callback. If not, tell them it's done. This also covers the
405 	 * case where the CPU has uniform loading at package or system
406 	 * scope implemented but does not advertise it.
407 	 */
408 	if (ret == UCODE_UPDATED || ret == UCODE_OK)
409 		ctrl = SCTRL_APPLY;
410 	else
411 		ctrl = SCTRL_DONE;
412 
413 	for_each_cpu(sibling, secondaries) {
414 		if (sibling != cpu)
415 			per_cpu(ucode_ctrl.ctrl, sibling) = ctrl;
416 	}
417 }
418 
419 static noinstr bool microcode_update_handler(void)
420 {
421 	unsigned int cpu = raw_smp_processor_id();
422 
423 	if (raw_cpu_read(ucode_ctrl.ctrl_cpu) == cpu) {
424 		instrumentation_begin();
425 		load_primary(cpu);
426 		instrumentation_end();
427 	} else {
428 		load_secondary(cpu);
429 	}
430 
431 	instrumentation_begin();
432 	touch_nmi_watchdog();
433 	instrumentation_end();
434 
435 	return true;
436 }
437 
438 /*
439  * Protection against instrumentation is required for CPUs which are not
440  * safe against an NMI which is delivered to the secondary SMT sibling
441  * while the primary thread updates the microcode. Instrumentation can end
442  * up in #INT3, #DB and #PF. The IRET from those exceptions reenables NMI
443  * which is the opposite of what the NMI rendezvous is trying to achieve.
444  *
445  * The primary thread is safe versus instrumentation as the actual
446  * microcode update handles this correctly. It's only the sibling code
447  * path which must be NMI safe until the primary thread completed the
448  * update.
449  */
450 bool noinstr microcode_nmi_handler(void)
451 {
452 	if (!raw_cpu_read(ucode_ctrl.nmi_enabled))
453 		return false;
454 
455 	raw_cpu_write(ucode_ctrl.nmi_enabled, false);
456 	return microcode_update_handler();
457 }
458 
459 static int load_cpus_stopped(void *unused)
460 {
461 	if (microcode_ops->use_nmi) {
462 		/* Enable the NMI handler and raise NMI */
463 		this_cpu_write(ucode_ctrl.nmi_enabled, true);
464 		apic->send_IPI(smp_processor_id(), NMI_VECTOR);
465 	} else {
466 		/* Just invoke the handler directly */
467 		microcode_update_handler();
468 	}
469 	return 0;
470 }
471 
472 static int load_late_stop_cpus(void)
473 {
474 	unsigned int cpu, updated = 0, failed = 0, timedout = 0, siblings = 0;
475 	int old_rev = boot_cpu_data.microcode;
476 	struct cpuinfo_x86 prev_info;
477 
478 	pr_err("Attempting late microcode loading - it is dangerous and taints the kernel.\n");
479 	pr_err("You should switch to early loading, if possible.\n");
480 
481 	atomic_set(&late_cpus_in, num_online_cpus());
482 	loops_per_usec = loops_per_jiffy / (TICK_NSEC / 1000);
483 
484 	/*
485 	 * Take a snapshot before the microcode update in order to compare and
486 	 * check whether any bits changed after an update.
487 	 */
488 	store_cpu_caps(&prev_info);
489 
490 	if (microcode_ops->use_nmi)
491 		static_branch_enable_cpuslocked(&microcode_nmi_handler_enable);
492 
493 	stop_machine_cpuslocked(load_cpus_stopped, NULL, cpu_online_mask);
494 
495 	if (microcode_ops->use_nmi)
496 		static_branch_disable_cpuslocked(&microcode_nmi_handler_enable);
497 
498 	/* Analyze the results */
499 	for_each_cpu_and(cpu, cpu_present_mask, &cpus_booted_once_mask) {
500 		switch (per_cpu(ucode_ctrl.result, cpu)) {
501 		case UCODE_UPDATED:	updated++; break;
502 		case UCODE_TIMEOUT:	timedout++; break;
503 		case UCODE_OK:		siblings++; break;
504 		default:		failed++; break;
505 		}
506 	}
507 
508 	if (microcode_ops->finalize_late_load)
509 		microcode_ops->finalize_late_load(!updated);
510 
511 	if (!updated) {
512 		/* Nothing changed. */
513 		if (!failed && !timedout)
514 			return 0;
515 		pr_err("update failed: %u CPUs failed %u CPUs timed out\n",
516 		       failed, timedout);
517 		return -EIO;
518 	}
519 
520 	add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
521 	pr_info("load: updated on %u primary CPUs with %u siblings\n", updated, siblings);
522 	if (failed || timedout) {
523 		pr_err("load incomplete. %u CPUs timed out or failed\n",
524 		       num_online_cpus() - (updated + siblings));
525 	}
526 	pr_info("revision: 0x%x -> 0x%x\n", old_rev, boot_cpu_data.microcode);
527 	microcode_check(&prev_info);
528 
529 	return updated + siblings == num_online_cpus() ? 0 : -EIO;
530 }
531 
532 /*
533  * This function does two things:
534  *
535  * 1) Ensure that all required CPUs which are present and have been booted
536  *    once are online.
537  *
538  *    To pass this check, all primary threads must be online.
539  *
540  *    If the microcode load is not safe against NMI then all SMT threads
541  *    must be online as well because they still react to NMIs when they are
542  *    soft-offlined and parked in one of the play_dead() variants. So if a
543  *    NMI hits while the primary thread updates the microcode the resulting
544  *    behaviour is undefined. The default play_dead() implementation on
545  *    modern CPUs uses MWAIT, which is also not guaranteed to be safe
546  *    against a microcode update which affects MWAIT.
547  *
548  * 2) Initialize the per CPU control structure
549  */
550 static bool setup_cpus(void)
551 {
552 	struct microcode_ctrl ctrl = { .ctrl = SCTRL_WAIT, .result = -1, };
553 	unsigned int cpu;
554 
555 	for_each_cpu_and(cpu, cpu_present_mask, &cpus_booted_once_mask) {
556 		if (!cpu_online(cpu)) {
557 			if (topology_is_primary_thread(cpu) || !microcode_ops->nmi_safe) {
558 				pr_err("CPU %u not online\n", cpu);
559 				return false;
560 			}
561 		}
562 
563 		/*
564 		 * Initialize the per CPU state. This is core scope for now,
565 		 * but prepared to take package or system scope into account.
566 		 */
567 		ctrl.ctrl_cpu = cpumask_first(topology_sibling_cpumask(cpu));
568 		per_cpu(ucode_ctrl, cpu) = ctrl;
569 	}
570 	return true;
571 }
572 
573 static int load_late_locked(void)
574 {
575 	if (!setup_cpus())
576 		return -EBUSY;
577 
578 	switch (microcode_ops->request_microcode_fw(0, &microcode_pdev->dev)) {
579 	case UCODE_NEW:
580 		return load_late_stop_cpus();
581 	case UCODE_NFOUND:
582 		return -ENOENT;
583 	default:
584 		return -EBADFD;
585 	}
586 }
587 
588 static ssize_t reload_store(struct device *dev,
589 			    struct device_attribute *attr,
590 			    const char *buf, size_t size)
591 {
592 	unsigned long val;
593 	ssize_t ret;
594 
595 	ret = kstrtoul(buf, 0, &val);
596 	if (ret || val != 1)
597 		return -EINVAL;
598 
599 	cpus_read_lock();
600 	ret = load_late_locked();
601 	cpus_read_unlock();
602 
603 	return ret ? : size;
604 }
605 
606 static DEVICE_ATTR_WO(reload);
607 #endif
608 
609 static ssize_t version_show(struct device *dev,
610 			struct device_attribute *attr, char *buf)
611 {
612 	struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
613 
614 	return sprintf(buf, "0x%x\n", uci->cpu_sig.rev);
615 }
616 
617 static ssize_t processor_flags_show(struct device *dev,
618 			struct device_attribute *attr, char *buf)
619 {
620 	struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
621 
622 	return sprintf(buf, "0x%x\n", uci->cpu_sig.pf);
623 }
624 
625 static DEVICE_ATTR_RO(version);
626 static DEVICE_ATTR_RO(processor_flags);
627 
628 static struct attribute *mc_default_attrs[] = {
629 	&dev_attr_version.attr,
630 	&dev_attr_processor_flags.attr,
631 	NULL
632 };
633 
634 static const struct attribute_group mc_attr_group = {
635 	.attrs			= mc_default_attrs,
636 	.name			= "microcode",
637 };
638 
639 static void microcode_fini_cpu(int cpu)
640 {
641 	if (microcode_ops->microcode_fini_cpu)
642 		microcode_ops->microcode_fini_cpu(cpu);
643 }
644 
645 /**
646  * microcode_bsp_resume - Update boot CPU microcode during resume.
647  */
648 void microcode_bsp_resume(void)
649 {
650 	int cpu = smp_processor_id();
651 	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
652 
653 	if (uci->mc)
654 		microcode_ops->apply_microcode(cpu);
655 	else
656 		reload_early_microcode(cpu);
657 }
658 
659 static struct syscore_ops mc_syscore_ops = {
660 	.resume	= microcode_bsp_resume,
661 };
662 
663 static int mc_cpu_online(unsigned int cpu)
664 {
665 	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
666 	struct device *dev = get_cpu_device(cpu);
667 
668 	memset(uci, 0, sizeof(*uci));
669 
670 	microcode_ops->collect_cpu_info(cpu, &uci->cpu_sig);
671 	cpu_data(cpu).microcode = uci->cpu_sig.rev;
672 	if (!cpu)
673 		boot_cpu_data.microcode = uci->cpu_sig.rev;
674 
675 	if (sysfs_create_group(&dev->kobj, &mc_attr_group))
676 		pr_err("Failed to create group for CPU%d\n", cpu);
677 	return 0;
678 }
679 
680 static int mc_cpu_down_prep(unsigned int cpu)
681 {
682 	struct device *dev = get_cpu_device(cpu);
683 
684 	microcode_fini_cpu(cpu);
685 	sysfs_remove_group(&dev->kobj, &mc_attr_group);
686 	return 0;
687 }
688 
689 static struct attribute *cpu_root_microcode_attrs[] = {
690 #ifdef CONFIG_MICROCODE_LATE_LOADING
691 	&dev_attr_reload.attr,
692 #endif
693 	NULL
694 };
695 
696 static const struct attribute_group cpu_root_microcode_group = {
697 	.name  = "microcode",
698 	.attrs = cpu_root_microcode_attrs,
699 };
700 
701 static int __init microcode_init(void)
702 {
703 	struct device *dev_root;
704 	struct cpuinfo_x86 *c = &boot_cpu_data;
705 	int error;
706 
707 	if (dis_ucode_ldr)
708 		return -EINVAL;
709 
710 	if (c->x86_vendor == X86_VENDOR_INTEL)
711 		microcode_ops = init_intel_microcode();
712 	else if (c->x86_vendor == X86_VENDOR_AMD)
713 		microcode_ops = init_amd_microcode();
714 	else
715 		pr_err("no support for this CPU vendor\n");
716 
717 	if (!microcode_ops)
718 		return -ENODEV;
719 
720 	microcode_pdev = platform_device_register_simple("microcode", -1, NULL, 0);
721 	if (IS_ERR(microcode_pdev))
722 		return PTR_ERR(microcode_pdev);
723 
724 	dev_root = bus_get_dev_root(&cpu_subsys);
725 	if (dev_root) {
726 		error = sysfs_create_group(&dev_root->kobj, &cpu_root_microcode_group);
727 		put_device(dev_root);
728 		if (error) {
729 			pr_err("Error creating microcode group!\n");
730 			goto out_pdev;
731 		}
732 	}
733 
734 	register_syscore_ops(&mc_syscore_ops);
735 	cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/microcode:online",
736 			  mc_cpu_online, mc_cpu_down_prep);
737 
738 	pr_info("Microcode Update Driver: v%s.", DRIVER_VERSION);
739 
740 	return 0;
741 
742  out_pdev:
743 	platform_device_unregister(microcode_pdev);
744 	return error;
745 
746 }
747 late_initcall(microcode_init);
748