core.c 18.4 KB
Newer Older
P
Peter Oruba 已提交
1
/*
2
 * CPU Microcode Update Driver for Linux
P
Peter Oruba 已提交
3
 *
A
Andrew Morton 已提交
4
 * Copyright (C) 2000-2006 Tigran Aivazian <aivazian.tigran@gmail.com>
5
 *	      2006	Shaohua Li <shaohua.li@intel.com>
6
 *	      2013-2016	Borislav Petkov <bp@alien8.de>
P
Peter Oruba 已提交
7
 *
8 9 10 11 12 13
 * X86 CPU microcode early update for Linux:
 *
 *	Copyright (C) 2012 Fenghua Yu <fenghua.yu@intel.com>
 *			   H Peter Anvin" <hpa@zytor.com>
 *		  (C) 2015 Borislav Petkov <bp@alien8.de>
 *
14
 * This driver allows to upgrade microcode on x86 processors.
P
Peter Oruba 已提交
15
 *
16 17 18 19
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
P
Peter Oruba 已提交
20
 */
21

22
#define pr_fmt(fmt) "microcode: " fmt
23

I
Ingo Molnar 已提交
24
#include <linux/platform_device.h>
25
#include <linux/stop_machine.h>
26
#include <linux/syscore_ops.h>
I
Ingo Molnar 已提交
27
#include <linux/miscdevice.h>
28
#include <linux/capability.h>
29
#include <linux/firmware.h>
I
Ingo Molnar 已提交
30
#include <linux/kernel.h>
31
#include <linux/delay.h>
P
Peter Oruba 已提交
32 33
#include <linux/mutex.h>
#include <linux/cpu.h>
34
#include <linux/nmi.h>
I
Ingo Molnar 已提交
35 36
#include <linux/fs.h>
#include <linux/mm.h>
P
Peter Oruba 已提交
37

38
#include <asm/microcode_intel.h>
39
#include <asm/cpu_device_id.h>
40
#include <asm/microcode_amd.h>
41
#include <asm/perf_event.h>
42 43 44
#include <asm/microcode.h>
#include <asm/processor.h>
#include <asm/cmdline.h>
45
#include <asm/setup.h>
P
Peter Oruba 已提交
46

47
#define DRIVER_VERSION	"2.2"
P
Peter Oruba 已提交
48

I
Ingo Molnar 已提交
49
static struct microcode_ops	*microcode_ops;
50
static bool dis_ucode_ldr = true;
51

52 53
bool initrd_gone;

54 55
LIST_HEAD(microcode_cache);

56 57 58 59 60 61 62 63 64 65 66 67
/*
 * Synchronization.
 *
 * All non cpu-hotplug-callback call sites use:
 *
 * - microcode_mutex to synchronize with each other;
 * - get/put_online_cpus() to synchronize with
 *   the cpu-hotplug-callback call sites.
 *
 * We guarantee that only a single cpu is being
 * updated at any particular moment of time.
 */
68
static DEFINE_MUTEX(microcode_mutex);
P
Peter Oruba 已提交
69

70 71 72 73 74
/*
 * Serialize late loading so that CPUs get updated one-by-one.
 */
static DEFINE_SPINLOCK(update_lock);

I
Ingo Molnar 已提交
75
struct ucode_cpu_info		ucode_cpu_info[NR_CPUS];
P
Peter Oruba 已提交
76

77 78 79 80 81
struct cpu_info_ctx {
	struct cpu_signature	*cpu_sig;
	int			err;
};

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
/*
 * Those patch levels cannot be updated to newer ones and thus should be final.
 */
static u32 final_levels[] = {
	0x01000098,
	0x0100009f,
	0x010000af,
	0, /* T-101 terminator */
};

/*
 * Check the current patch level on this CPU.
 *
 * Returns:
 *  - true: if update should stop
 *  - false: otherwise
 */
static bool amd_check_current_patch_level(void)
{
	u32 lvl, dummy, i;
	u32 *levels;

	native_rdmsr(MSR_AMD64_PATCH_LEVEL, lvl, dummy);

	if (IS_ENABLED(CONFIG_X86_32))
		levels = (u32 *)__pa_nodebug(&final_levels);
	else
		levels = final_levels;

	for (i = 0; levels[i]; i++) {
		if (lvl == levels[i])
			return true;
	}
	return false;
}

118 119
static bool __init check_loader_disabled_bsp(void)
{
120 121
	static const char *__dis_opt_str = "dis_ucode_ldr";

122 123
#ifdef CONFIG_X86_32
	const char *cmdline = (const char *)__pa_nodebug(boot_command_line);
124
	const char *option  = (const char *)__pa_nodebug(__dis_opt_str);
125 126 127 128
	bool *res = (bool *)__pa_nodebug(&dis_ucode_ldr);

#else /* CONFIG_X86_64 */
	const char *cmdline = boot_command_line;
129
	const char *option  = __dis_opt_str;
130 131 132
	bool *res = &dis_ucode_ldr;
#endif

133 134 135 136 137
	/*
	 * CPUID(1).ECX[31]: reserved for hypervisor use. This is still not
	 * completely accurate as xen pv guests don't see that CPUID bit set but
	 * that's good enough as they don't land on the BSP path anyway.
	 */
138
	if (native_cpuid_ecx(1) & BIT(31))
139 140
		return *res;

141 142 143 144 145
	if (x86_cpuid_vendor() == X86_VENDOR_AMD) {
		if (amd_check_current_patch_level())
			return *res;
	}

146 147
	if (cmdline_find_option_bool(cmdline, option) <= 0)
		*res = false;
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

	return *res;
}

extern struct builtin_fw __start_builtin_fw[];
extern struct builtin_fw __end_builtin_fw[];

bool get_builtin_firmware(struct cpio_data *cd, const char *name)
{
#ifdef CONFIG_FW_LOADER
	struct builtin_fw *b_fw;

	for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
		if (!strcmp(name, b_fw->name)) {
			cd->size = b_fw->size;
			cd->data = b_fw->data;
			return true;
		}
	}
#endif
	return false;
}

void __init load_ucode_bsp(void)
{
173
	unsigned int cpuid_1_eax;
174
	bool intel = true;
175

176
	if (!have_cpuid_p())
177 178
		return;

179
	cpuid_1_eax = native_cpuid_eax(1);
180

181
	switch (x86_cpuid_vendor()) {
182
	case X86_VENDOR_INTEL:
183 184
		if (x86_family(cpuid_1_eax) < 6)
			return;
185
		break;
186

187
	case X86_VENDOR_AMD:
188 189 190
		if (x86_family(cpuid_1_eax) < 0x10)
			return;
		intel = false;
191
		break;
192

193
	default:
194
		return;
195
	}
196 197 198 199 200 201 202 203

	if (check_loader_disabled_bsp())
		return;

	if (intel)
		load_ucode_intel_bsp();
	else
		load_ucode_amd_bsp(cpuid_1_eax);
204 205 206 207 208 209 210 211 212 213 214 215 216
}

static bool check_loader_disabled_ap(void)
{
#ifdef CONFIG_X86_32
	return *((bool *)__pa_nodebug(&dis_ucode_ldr));
#else
	return dis_ucode_ldr;
#endif
}

void load_ucode_ap(void)
{
217
	unsigned int cpuid_1_eax;
218 219 220 221

	if (check_loader_disabled_ap())
		return;

222
	cpuid_1_eax = native_cpuid_eax(1);
223

224
	switch (x86_cpuid_vendor()) {
225
	case X86_VENDOR_INTEL:
226
		if (x86_family(cpuid_1_eax) >= 6)
227 228 229
			load_ucode_intel_ap();
		break;
	case X86_VENDOR_AMD:
230 231
		if (x86_family(cpuid_1_eax) >= 0x10)
			load_ucode_amd_ap(cpuid_1_eax);
232 233 234 235 236 237
		break;
	default:
		break;
	}
}

238
static int __init save_microcode_in_initrd(void)
239 240
{
	struct cpuinfo_x86 *c = &boot_cpu_data;
241
	int ret = -EINVAL;
242 243 244 245

	switch (c->x86_vendor) {
	case X86_VENDOR_INTEL:
		if (c->x86 >= 6)
246
			ret = save_microcode_in_initrd_intel();
247 248 249
		break;
	case X86_VENDOR_AMD:
		if (c->x86 >= 0x10)
250
			ret = save_microcode_in_initrd_amd(cpuid_eax(1));
251 252 253 254 255
		break;
	default:
		break;
	}

256 257 258
	initrd_gone = true;

	return ret;
259 260
}

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
struct cpio_data find_microcode_in_initrd(const char *path, bool use_pa)
{
#ifdef CONFIG_BLK_DEV_INITRD
	unsigned long start = 0;
	size_t size;

#ifdef CONFIG_X86_32
	struct boot_params *params;

	if (use_pa)
		params = (struct boot_params *)__pa_nodebug(&boot_params);
	else
		params = &boot_params;

	size = params->hdr.ramdisk_size;

	/*
	 * Set start only if we have an initrd image. We cannot use initrd_start
	 * because it is not set that early yet.
	 */
	if (size)
		start = params->hdr.ramdisk_image;

# else /* CONFIG_X86_64 */
	size  = (unsigned long)boot_params.ext_ramdisk_size << 32;
	size |= boot_params.hdr.ramdisk_size;

	if (size) {
		start  = (unsigned long)boot_params.ext_ramdisk_image << 32;
		start |= boot_params.hdr.ramdisk_image;

		start += PAGE_OFFSET;
	}
# endif

	/*
297 298 299 300
	 * Fixup the start address: after reserve_initrd() runs, initrd_start
	 * has the virtual address of the beginning of the initrd. It also
	 * possibly relocates the ramdisk. In either case, initrd_start contains
	 * the updated address so use that instead.
301 302 303
	 *
	 * initrd_gone is for the hotplug case where we've thrown out initrd
	 * already.
304
	 */
305 306 307 308 309
	if (!use_pa) {
		if (initrd_gone)
			return (struct cpio_data){ NULL, 0, "" };
		if (initrd_start)
			start = initrd_start;
310 311 312 313 314 315 316 317 318 319 320
	} else {
		/*
		 * The picture with physical addresses is a bit different: we
		 * need to get the *physical* address to which the ramdisk was
		 * relocated, i.e., relocated_ramdisk (not initrd_start) and
		 * since we're running from physical addresses, we need to access
		 * relocated_ramdisk through its *physical* address too.
		 */
		u64 *rr = (u64 *)__pa_nodebug(&relocated_ramdisk);
		if (*rr)
			start = *rr;
321
	}
322 323 324 325 326 327 328

	return find_cpio_data(path, (void *)start, size, NULL);
#else /* !CONFIG_BLK_DEV_INITRD */
	return (struct cpio_data){ NULL, 0, "" };
#endif
}

329 330 331 332
void reload_early_microcode(void)
{
	int vendor, family;

333 334
	vendor = x86_cpuid_vendor();
	family = x86_cpuid_family();
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349

	switch (vendor) {
	case X86_VENDOR_INTEL:
		if (family >= 6)
			reload_ucode_intel();
		break;
	case X86_VENDOR_AMD:
		if (family >= 0x10)
			reload_ucode_amd();
		break;
	default:
		break;
	}
}

350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
static void collect_cpu_info_local(void *arg)
{
	struct cpu_info_ctx *ctx = arg;

	ctx->err = microcode_ops->collect_cpu_info(smp_processor_id(),
						   ctx->cpu_sig);
}

static int collect_cpu_info_on_target(int cpu, struct cpu_signature *cpu_sig)
{
	struct cpu_info_ctx ctx = { .cpu_sig = cpu_sig, .err = 0 };
	int ret;

	ret = smp_call_function_single(cpu, collect_cpu_info_local, &ctx, 1);
	if (!ret)
		ret = ctx.err;

	return ret;
}

static int collect_cpu_info(int cpu)
{
	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
	int ret;

	memset(uci, 0, sizeof(*uci));

	ret = collect_cpu_info_on_target(cpu, &uci->cpu_sig);
	if (!ret)
		uci->valid = 1;

	return ret;
}

static void apply_microcode_local(void *arg)
{
386
	enum ucode_state *err = arg;
387

388
	*err = microcode_ops->apply_microcode(smp_processor_id());
389 390 391 392
}

static int apply_microcode_on_target(int cpu)
{
393
	enum ucode_state err;
394 395
	int ret;

396 397 398 399 400
	ret = smp_call_function_single(cpu, apply_microcode_local, &err, 1);
	if (!ret) {
		if (err == UCODE_ERROR)
			ret = 1;
	}
401 402 403
	return ret;
}

P
Peter Oruba 已提交
404
#ifdef CONFIG_MICROCODE_OLD_INTERFACE
D
Dmitry Adamushko 已提交
405
static int do_microcode_update(const void __user *buf, size_t size)
P
Peter Oruba 已提交
406 407 408
{
	int error = 0;
	int cpu;
409

D
Dmitry Adamushko 已提交
410 411
	for_each_online_cpu(cpu) {
		struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
412
		enum ucode_state ustate;
D
Dmitry Adamushko 已提交
413 414 415

		if (!uci->valid)
			continue;
416

417 418 419 420 421 422
		ustate = microcode_ops->request_microcode_user(cpu, buf, size);
		if (ustate == UCODE_ERROR) {
			error = -1;
			break;
		} else if (ustate == UCODE_OK)
			apply_microcode_on_target(cpu);
P
Peter Oruba 已提交
423
	}
424

P
Peter Oruba 已提交
425 426 427
	return error;
}

428
static int microcode_open(struct inode *inode, struct file *file)
P
Peter Oruba 已提交
429
{
430
	return capable(CAP_SYS_RAWIO) ? nonseekable_open(inode, file) : -EPERM;
P
Peter Oruba 已提交
431 432
}

433 434
static ssize_t microcode_write(struct file *file, const char __user *buf,
			       size_t len, loff_t *ppos)
P
Peter Oruba 已提交
435
{
436
	ssize_t ret = -EINVAL;
P
Peter Oruba 已提交
437

438
	if ((len >> PAGE_SHIFT) > totalram_pages) {
439
		pr_err("too much data (max %ld pages)\n", totalram_pages);
440
		return ret;
P
Peter Oruba 已提交
441 442 443 444 445
	}

	get_online_cpus();
	mutex_lock(&microcode_mutex);

446
	if (do_microcode_update(buf, len) == 0)
P
Peter Oruba 已提交
447 448
		ret = (ssize_t)len;

449 450 451
	if (ret > 0)
		perf_check_microcode();

P
Peter Oruba 已提交
452 453 454 455 456 457 458
	mutex_unlock(&microcode_mutex);
	put_online_cpus();

	return ret;
}

static const struct file_operations microcode_fops = {
459 460 461
	.owner			= THIS_MODULE,
	.write			= microcode_write,
	.open			= microcode_open,
462
	.llseek		= no_llseek,
P
Peter Oruba 已提交
463 464 465
};

static struct miscdevice microcode_dev = {
466 467
	.minor			= MICROCODE_MINOR,
	.name			= "microcode",
468
	.nodename		= "cpu/microcode",
469
	.fops			= &microcode_fops,
P
Peter Oruba 已提交
470 471
};

472
static int __init microcode_dev_init(void)
P
Peter Oruba 已提交
473 474 475 476 477
{
	int error;

	error = misc_register(&microcode_dev);
	if (error) {
478
		pr_err("can't misc_register on minor=%d\n", MICROCODE_MINOR);
P
Peter Oruba 已提交
479 480 481 482 483 484
		return error;
	}

	return 0;
}

485
static void __exit microcode_dev_exit(void)
P
Peter Oruba 已提交
486 487 488 489
{
	misc_deregister(&microcode_dev);
}
#else
I
Ingo Molnar 已提交
490 491
#define microcode_dev_init()	0
#define microcode_dev_exit()	do { } while (0)
P
Peter Oruba 已提交
492 493 494
#endif

/* fake device for request_firmware */
I
Ingo Molnar 已提交
495
static struct platform_device	*microcode_pdev;
P
Peter Oruba 已提交
496

497 498 499 500 501 502 503 504 505 506 507 508 509
/*
 * Late loading dance. Why the heavy-handed stomp_machine effort?
 *
 * - HT siblings must be idle and not execute other code while the other sibling
 *   is loading microcode in order to avoid any negative interactions caused by
 *   the loading.
 *
 * - In addition, microcode update on the cores must be serialized until this
 *   requirement can be relaxed in the future. Right now, this is conservative
 *   and good.
 */
#define SPINUNIT 100 /* 100 nsec */

510 511 512 513 514 515 516 517 518 519
static int check_online_cpus(void)
{
	if (num_online_cpus() == num_present_cpus())
		return 0;

	pr_err("Not all CPUs online, aborting microcode update.\n");

	return -EINVAL;
}

520 521 522 523 524 525 526 527 528
static atomic_t late_cpus;

/*
 * Returns:
 * < 0 - on error
 *   0 - no update done
 *   1 - microcode was updated
 */
static int __reload_late(void *info)
529
{
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
	unsigned int timeout = NSEC_PER_SEC;
	int all_cpus = num_online_cpus();
	int cpu = smp_processor_id();
	enum ucode_state err;
	int ret = 0;

	atomic_dec(&late_cpus);

	/*
	 * Wait for all CPUs to arrive. A load will not be attempted unless all
	 * CPUs show up.
	 * */
	while (atomic_read(&late_cpus)) {
		if (timeout < SPINUNIT) {
			pr_err("Timeout while waiting for CPUs rendezvous, remaining: %d\n",
				atomic_read(&late_cpus));
			return -1;
		}

		ndelay(SPINUNIT);
		timeout -= SPINUNIT;

		touch_nmi_watchdog();
	}

	spin_lock(&update_lock);
	apply_microcode_local(&err);
	spin_unlock(&update_lock);

	if (err > UCODE_NFOUND) {
		pr_warn("Error reloading microcode on CPU %d\n", cpu);
		ret = -1;
	} else if (err == UCODE_UPDATED) {
		ret = 1;
	}
565

566
	atomic_inc(&late_cpus);
567

568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
	while (atomic_read(&late_cpus) != all_cpus)
		cpu_relax();

	return ret;
}

/*
 * Reload microcode late on all CPUs. Wait for a sec until they
 * all gather together.
 */
static int microcode_reload_late(void)
{
	int ret;

	atomic_set(&late_cpus, num_online_cpus());

	ret = stop_machine_cpuslocked(__reload_late, NULL, cpu_online_mask);
	if (ret < 0)
		return ret;
	else if (ret > 0)
		microcode_check();

	return ret;
591 592
}

593 594
static ssize_t reload_store(struct device *dev,
			    struct device_attribute *attr,
595
			    const char *buf, size_t size)
P
Peter Oruba 已提交
596
{
597
	enum ucode_state tmp_ret = UCODE_OK;
598
	int bsp = boot_cpu_data.cpu_index;
599
	unsigned long val;
600
	ssize_t ret = 0;
601

602 603 604
	ret = kstrtoul(buf, 0, &val);
	if (ret)
		return ret;
605

606 607 608
	if (val != 1)
		return size;

609 610 611 612
	tmp_ret = microcode_ops->request_microcode_fw(bsp, &microcode_pdev->dev, true);
	if (tmp_ret != UCODE_OK)
		return size;

613
	get_online_cpus();
614 615 616 617 618

	ret = check_online_cpus();
	if (ret)
		goto put;

619
	mutex_lock(&microcode_mutex);
620
	ret = microcode_reload_late();
621
	mutex_unlock(&microcode_mutex);
622 623

put:
624
	put_online_cpus();
625

626
	if (ret >= 0)
627 628 629
		ret = size;

	return ret;
P
Peter Oruba 已提交
630 631
}

632 633
static ssize_t version_show(struct device *dev,
			struct device_attribute *attr, char *buf)
P
Peter Oruba 已提交
634 635 636
{
	struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;

637
	return sprintf(buf, "0x%x\n", uci->cpu_sig.rev);
P
Peter Oruba 已提交
638 639
}

640 641
static ssize_t pf_show(struct device *dev,
			struct device_attribute *attr, char *buf)
P
Peter Oruba 已提交
642 643 644
{
	struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;

645
	return sprintf(buf, "0x%x\n", uci->cpu_sig.pf);
P
Peter Oruba 已提交
646 647
}

J
Joe Perches 已提交
648
static DEVICE_ATTR_WO(reload);
649 650
static DEVICE_ATTR(version, 0400, version_show, NULL);
static DEVICE_ATTR(processor_flags, 0400, pf_show, NULL);
P
Peter Oruba 已提交
651 652

static struct attribute *mc_default_attrs[] = {
653 654
	&dev_attr_version.attr,
	&dev_attr_processor_flags.attr,
P
Peter Oruba 已提交
655 656 657
	NULL
};

658
static const struct attribute_group mc_attr_group = {
659 660
	.attrs			= mc_default_attrs,
	.name			= "microcode",
P
Peter Oruba 已提交
661 662
};

663
static void microcode_fini_cpu(int cpu)
664
{
665 666
	if (microcode_ops->microcode_fini_cpu)
		microcode_ops->microcode_fini_cpu(cpu);
667 668
}

669
static enum ucode_state microcode_resume_cpu(int cpu)
670
{
671 672
	if (apply_microcode_on_target(cpu))
		return UCODE_ERROR;
673

674 675
	pr_debug("CPU%d updated upon resume\n", cpu);

676
	return UCODE_OK;
677 678
}

679
static enum ucode_state microcode_init_cpu(int cpu, bool refresh_fw)
680
{
681
	enum ucode_state ustate;
682 683
	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;

684
	if (uci->valid)
685
		return UCODE_OK;
686

687 688
	if (collect_cpu_info(cpu))
		return UCODE_ERROR;
689

690 691 692
	/* --dimm. Trigger a delayed update? */
	if (system_state != SYSTEM_RUNNING)
		return UCODE_NFOUND;
693

694 695
	ustate = microcode_ops->request_microcode_fw(cpu, &microcode_pdev->dev,
						     refresh_fw);
696

697
	if (ustate == UCODE_OK) {
698
		pr_debug("CPU%d updated upon init\n", cpu);
699
		apply_microcode_on_target(cpu);
700 701
	}

702
	return ustate;
703 704
}

705
static enum ucode_state microcode_update_cpu(int cpu)
706
{
707
	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
708

709 710 711
	/* Refresh CPU microcode revision after resume. */
	collect_cpu_info(cpu);

712
	if (uci->valid)
713
		return microcode_resume_cpu(cpu);
714

715
	return microcode_init_cpu(cpu, false);
716 717
}

718
static int mc_device_add(struct device *dev, struct subsys_interface *sif)
P
Peter Oruba 已提交
719
{
720
	int err, cpu = dev->id;
P
Peter Oruba 已提交
721 722 723 724

	if (!cpu_online(cpu))
		return 0;

725
	pr_debug("CPU%d added\n", cpu);
P
Peter Oruba 已提交
726

727
	err = sysfs_create_group(&dev->kobj, &mc_attr_group);
P
Peter Oruba 已提交
728 729 730
	if (err)
		return err;

731
	if (microcode_init_cpu(cpu, true) == UCODE_ERROR)
732
		return -EINVAL;
733 734

	return err;
P
Peter Oruba 已提交
735 736
}

737
static void mc_device_remove(struct device *dev, struct subsys_interface *sif)
P
Peter Oruba 已提交
738
{
739
	int cpu = dev->id;
P
Peter Oruba 已提交
740 741

	if (!cpu_online(cpu))
742
		return;
P
Peter Oruba 已提交
743

744
	pr_debug("CPU%d removed\n", cpu);
745
	microcode_fini_cpu(cpu);
746
	sysfs_remove_group(&dev->kobj, &mc_attr_group);
P
Peter Oruba 已提交
747 748
}

749 750 751 752 753
static struct subsys_interface mc_cpu_interface = {
	.name			= "microcode",
	.subsys			= &cpu_subsys,
	.add_dev		= mc_device_add,
	.remove_dev		= mc_device_remove,
754 755 756 757 758 759
};

/**
 * mc_bp_resume - Update boot CPU microcode during resume.
 */
static void mc_bp_resume(void)
P
Peter Oruba 已提交
760
{
761
	int cpu = smp_processor_id();
762
	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
P
Peter Oruba 已提交
763

764 765
	if (uci->valid && uci->mc)
		microcode_ops->apply_microcode(cpu);
766
	else if (!uci->mc)
767
		reload_early_microcode();
P
Peter Oruba 已提交
768 769
}

770 771
static struct syscore_ops mc_syscore_ops = {
	.resume			= mc_bp_resume,
P
Peter Oruba 已提交
772 773
};

774
static int mc_cpu_online(unsigned int cpu)
P
Peter Oruba 已提交
775
{
776
	struct device *dev;
P
Peter Oruba 已提交
777

778
	dev = get_cpu_device(cpu);
779 780
	microcode_update_cpu(cpu);
	pr_debug("CPU%d added\n", cpu);
781

782 783 784 785
	if (sysfs_create_group(&dev->kobj, &mc_attr_group))
		pr_err("Failed to create group for CPU%d\n", cpu);
	return 0;
}
786

787 788 789
static int mc_cpu_down_prep(unsigned int cpu)
{
	struct device *dev;
790

791 792 793 794
	dev = get_cpu_device(cpu);
	/* Suspend is in progress, only remove the interface */
	sysfs_remove_group(&dev->kobj, &mc_attr_group);
	pr_debug("CPU%d removed\n", cpu);
795

796
	return 0;
P
Peter Oruba 已提交
797 798
}

799 800 801 802 803
static struct attribute *cpu_root_microcode_attrs[] = {
	&dev_attr_reload.attr,
	NULL
};

804
static const struct attribute_group cpu_root_microcode_group = {
805 806 807 808
	.name  = "microcode",
	.attrs = cpu_root_microcode_attrs,
};

809
int __init microcode_init(void)
P
Peter Oruba 已提交
810
{
811
	struct cpuinfo_x86 *c = &boot_cpu_data;
P
Peter Oruba 已提交
812 813
	int error;

814
	if (dis_ucode_ldr)
815
		return -EINVAL;
816

817 818
	if (c->x86_vendor == X86_VENDOR_INTEL)
		microcode_ops = init_intel_microcode();
P
Peter Oruba 已提交
819
	else if (c->x86_vendor == X86_VENDOR_AMD)
820
		microcode_ops = init_amd_microcode();
821
	else
822
		pr_err("no support for this CPU vendor\n");
823 824

	if (!microcode_ops)
825
		return -ENODEV;
P
Peter Oruba 已提交
826 827 828

	microcode_pdev = platform_device_register_simple("microcode", -1,
							 NULL, 0);
829
	if (IS_ERR(microcode_pdev))
P
Peter Oruba 已提交
830 831 832
		return PTR_ERR(microcode_pdev);

	get_online_cpus();
833 834
	mutex_lock(&microcode_mutex);

835
	error = subsys_interface_register(&mc_cpu_interface);
836 837
	if (!error)
		perf_check_microcode();
838
	mutex_unlock(&microcode_mutex);
P
Peter Oruba 已提交
839
	put_online_cpus();
840

841 842
	if (error)
		goto out_pdev;
P
Peter Oruba 已提交
843

844 845 846 847 848 849 850 851
	error = sysfs_create_group(&cpu_subsys.dev_root->kobj,
				   &cpu_root_microcode_group);

	if (error) {
		pr_err("Error creating microcode group!\n");
		goto out_driver;
	}

852 853
	error = microcode_dev_init();
	if (error)
854
		goto out_ucode_group;
855

856
	register_syscore_ops(&mc_syscore_ops);
857 858
	cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "x86/microcode:online",
				  mc_cpu_online, mc_cpu_down_prep);
P
Peter Oruba 已提交
859

860
	pr_info("Microcode Update Driver: v%s.", DRIVER_VERSION);
P
Peter Oruba 已提交
861

P
Peter Oruba 已提交
862
	return 0;
863

864 865 866 867 868
 out_ucode_group:
	sysfs_remove_group(&cpu_subsys.dev_root->kobj,
			   &cpu_root_microcode_group);

 out_driver:
869 870 871
	get_online_cpus();
	mutex_lock(&microcode_mutex);

872
	subsys_interface_unregister(&mc_cpu_interface);
873 874 875 876

	mutex_unlock(&microcode_mutex);
	put_online_cpus();

877
 out_pdev:
878 879 880
	platform_device_unregister(microcode_pdev);
	return error;

P
Peter Oruba 已提交
881
}
882
fs_initcall(save_microcode_in_initrd);
883
late_initcall(microcode_init);