core.c 13.9 KB
Newer Older
P
Peter Oruba 已提交
1
/*
2
 * CPU Microcode Update Driver for Linux
P
Peter Oruba 已提交
3
 *
4 5 6
 * Copyright (C) 2000-2006 Tigran Aivazian <tigran@aivazian.fsnet.co.uk>
 *	      2006	Shaohua Li <shaohua.li@intel.com>
 *	      2013-2015	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/syscore_ops.h>
I
Ingo Molnar 已提交
26
#include <linux/miscdevice.h>
27
#include <linux/capability.h>
28
#include <linux/firmware.h>
I
Ingo Molnar 已提交
29
#include <linux/kernel.h>
P
Peter Oruba 已提交
30 31
#include <linux/mutex.h>
#include <linux/cpu.h>
I
Ingo Molnar 已提交
32 33
#include <linux/fs.h>
#include <linux/mm.h>
P
Peter Oruba 已提交
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 40 41
#include <asm/microcode.h>
#include <asm/processor.h>
#include <asm/cmdline.h>
P
Peter Oruba 已提交
42

43
#define MICROCODE_VERSION	"2.01"
P
Peter Oruba 已提交
44

I
Ingo Molnar 已提交
45
static struct microcode_ops	*microcode_ops;
46 47
static bool dis_ucode_ldr;

48 49
LIST_HEAD(microcode_cache);

50 51 52 53 54 55 56 57 58 59 60 61
/*
 * 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.
 */
62
static DEFINE_MUTEX(microcode_mutex);
P
Peter Oruba 已提交
63

I
Ingo Molnar 已提交
64
struct ucode_cpu_info		ucode_cpu_info[NR_CPUS];
P
Peter Oruba 已提交
65

66 67 68 69 70 71 72 73 74
/*
 * Operations that are run on a target cpu:
 */

struct cpu_info_ctx {
	struct cpu_signature	*cpu_sig;
	int			err;
};

75 76
static bool __init check_loader_disabled_bsp(void)
{
77 78
	static const char *__dis_opt_str = "dis_ucode_ldr";

79 80
#ifdef CONFIG_X86_32
	const char *cmdline = (const char *)__pa_nodebug(boot_command_line);
81
	const char *option  = (const char *)__pa_nodebug(__dis_opt_str);
82 83 84 85
	bool *res = (bool *)__pa_nodebug(&dis_ucode_ldr);

#else /* CONFIG_X86_64 */
	const char *cmdline = boot_command_line;
86
	const char *option  = __dis_opt_str;
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 118 119 120 121 122 123 124 125
	bool *res = &dis_ucode_ldr;
#endif

	if (cmdline_find_option_bool(cmdline, option))
		*res = true;

	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)
{
	int vendor;
	unsigned int family;

	if (check_loader_disabled_bsp())
		return;

	if (!have_cpuid_p())
		return;

126 127
	vendor = x86_cpuid_vendor();
	family = x86_cpuid_family();
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

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

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)
{
	int vendor, family;

	if (check_loader_disabled_ap())
		return;

	if (!have_cpuid_p())
		return;

162 163
	vendor = x86_cpuid_vendor();
	family = x86_cpuid_family();
164 165 166 167 168 169 170 171

	switch (vendor) {
	case X86_VENDOR_INTEL:
		if (family >= 6)
			load_ucode_intel_ap();
		break;
	case X86_VENDOR_AMD:
		if (family >= 0x10)
172
			load_ucode_amd_ap(family);
173 174 175 176 177 178
		break;
	default:
		break;
	}
}

179
static int __init save_microcode_in_initrd(void)
180 181 182 183 184 185
{
	struct cpuinfo_x86 *c = &boot_cpu_data;

	switch (c->x86_vendor) {
	case X86_VENDOR_INTEL:
		if (c->x86 >= 6)
186
			return save_microcode_in_initrd_intel();
187 188 189
		break;
	case X86_VENDOR_AMD:
		if (c->x86 >= 0x10)
190
			return save_microcode_in_initrd_amd(c->x86);
191 192 193 194 195
		break;
	default:
		break;
	}

196
	return -EINVAL;
197 198 199 200 201 202
}

void reload_early_microcode(void)
{
	int vendor, family;

203 204
	vendor = x86_cpuid_vendor();
	family = x86_cpuid_family();
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

	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;
	}
}

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
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;
}

struct apply_microcode_ctx {
	int err;
};

static void apply_microcode_local(void *arg)
{
	struct apply_microcode_ctx *ctx = arg;

	ctx->err = microcode_ops->apply_microcode(smp_processor_id());
}

static int apply_microcode_on_target(int cpu)
{
	struct apply_microcode_ctx ctx = { .err = 0 };
	int ret;

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

	return ret;
}

P
Peter Oruba 已提交
277
#ifdef CONFIG_MICROCODE_OLD_INTERFACE
D
Dmitry Adamushko 已提交
278
static int do_microcode_update(const void __user *buf, size_t size)
P
Peter Oruba 已提交
279 280 281
{
	int error = 0;
	int cpu;
282

D
Dmitry Adamushko 已提交
283 284
	for_each_online_cpu(cpu) {
		struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
285
		enum ucode_state ustate;
D
Dmitry Adamushko 已提交
286 287 288

		if (!uci->valid)
			continue;
289

290 291 292 293 294 295
		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 已提交
296
	}
297

P
Peter Oruba 已提交
298 299 300
	return error;
}

301
static int microcode_open(struct inode *inode, struct file *file)
P
Peter Oruba 已提交
302
{
303
	return capable(CAP_SYS_RAWIO) ? nonseekable_open(inode, file) : -EPERM;
P
Peter Oruba 已提交
304 305
}

306 307
static ssize_t microcode_write(struct file *file, const char __user *buf,
			       size_t len, loff_t *ppos)
P
Peter Oruba 已提交
308
{
309
	ssize_t ret = -EINVAL;
P
Peter Oruba 已提交
310

311
	if ((len >> PAGE_SHIFT) > totalram_pages) {
312
		pr_err("too much data (max %ld pages)\n", totalram_pages);
313
		return ret;
P
Peter Oruba 已提交
314 315 316 317 318
	}

	get_online_cpus();
	mutex_lock(&microcode_mutex);

319
	if (do_microcode_update(buf, len) == 0)
P
Peter Oruba 已提交
320 321
		ret = (ssize_t)len;

322 323 324
	if (ret > 0)
		perf_check_microcode();

P
Peter Oruba 已提交
325 326 327 328 329 330 331
	mutex_unlock(&microcode_mutex);
	put_online_cpus();

	return ret;
}

static const struct file_operations microcode_fops = {
332 333 334
	.owner			= THIS_MODULE,
	.write			= microcode_write,
	.open			= microcode_open,
335
	.llseek		= no_llseek,
P
Peter Oruba 已提交
336 337 338
};

static struct miscdevice microcode_dev = {
339 340
	.minor			= MICROCODE_MINOR,
	.name			= "microcode",
341
	.nodename		= "cpu/microcode",
342
	.fops			= &microcode_fops,
P
Peter Oruba 已提交
343 344
};

345
static int __init microcode_dev_init(void)
P
Peter Oruba 已提交
346 347 348 349 350
{
	int error;

	error = misc_register(&microcode_dev);
	if (error) {
351
		pr_err("can't misc_register on minor=%d\n", MICROCODE_MINOR);
P
Peter Oruba 已提交
352 353 354 355 356 357
		return error;
	}

	return 0;
}

358
static void __exit microcode_dev_exit(void)
P
Peter Oruba 已提交
359 360 361 362
{
	misc_deregister(&microcode_dev);
}
#else
I
Ingo Molnar 已提交
363 364
#define microcode_dev_init()	0
#define microcode_dev_exit()	do { } while (0)
P
Peter Oruba 已提交
365 366 367
#endif

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

370
static int reload_for_cpu(int cpu)
371
{
372
	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
373
	enum ucode_state ustate;
374 375
	int err = 0;

376 377
	if (!uci->valid)
		return err;
378

379
	ustate = microcode_ops->request_microcode_fw(cpu, &microcode_pdev->dev, true);
380 381 382 383 384
	if (ustate == UCODE_OK)
		apply_microcode_on_target(cpu);
	else
		if (ustate == UCODE_ERROR)
			err = -EINVAL;
385 386 387
	return err;
}

388 389
static ssize_t reload_store(struct device *dev,
			    struct device_attribute *attr,
390
			    const char *buf, size_t size)
P
Peter Oruba 已提交
391
{
392
	unsigned long val;
393 394 395
	int cpu;
	ssize_t ret = 0, tmp_ret;

396 397 398
	ret = kstrtoul(buf, 0, &val);
	if (ret)
		return ret;
399

400 401 402 403
	if (val != 1)
		return size;

	get_online_cpus();
404
	mutex_lock(&microcode_mutex);
405 406 407 408 409 410 411 412
	for_each_online_cpu(cpu) {
		tmp_ret = reload_for_cpu(cpu);
		if (tmp_ret != 0)
			pr_warn("Error reloading microcode on CPU %d\n", cpu);

		/* save retval of the first encountered reload error */
		if (!ret)
			ret = tmp_ret;
P
Peter Oruba 已提交
413
	}
414 415 416
	if (!ret)
		perf_check_microcode();
	mutex_unlock(&microcode_mutex);
417
	put_online_cpus();
418 419 420 421 422

	if (!ret)
		ret = size;

	return ret;
P
Peter Oruba 已提交
423 424
}

425 426
static ssize_t version_show(struct device *dev,
			struct device_attribute *attr, char *buf)
P
Peter Oruba 已提交
427 428 429
{
	struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;

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

433 434
static ssize_t pf_show(struct device *dev,
			struct device_attribute *attr, char *buf)
P
Peter Oruba 已提交
435 436 437
{
	struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;

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

441 442 443
static DEVICE_ATTR(reload, 0200, NULL, reload_store);
static DEVICE_ATTR(version, 0400, version_show, NULL);
static DEVICE_ATTR(processor_flags, 0400, pf_show, NULL);
P
Peter Oruba 已提交
444 445

static struct attribute *mc_default_attrs[] = {
446 447
	&dev_attr_version.attr,
	&dev_attr_processor_flags.attr,
P
Peter Oruba 已提交
448 449 450 451
	NULL
};

static struct attribute_group mc_attr_group = {
452 453
	.attrs			= mc_default_attrs,
	.name			= "microcode",
P
Peter Oruba 已提交
454 455
};

456
static void microcode_fini_cpu(int cpu)
457 458
{
	microcode_ops->microcode_fini_cpu(cpu);
459 460
}

461
static enum ucode_state microcode_resume_cpu(int cpu)
462
{
463 464
	if (apply_microcode_on_target(cpu))
		return UCODE_ERROR;
465

466 467
	pr_debug("CPU%d updated upon resume\n", cpu);

468
	return UCODE_OK;
469 470
}

471
static enum ucode_state microcode_init_cpu(int cpu, bool refresh_fw)
472
{
473
	enum ucode_state ustate;
474 475
	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;

476
	if (uci->valid)
477
		return UCODE_OK;
478

479 480
	if (collect_cpu_info(cpu))
		return UCODE_ERROR;
481

482 483 484
	/* --dimm. Trigger a delayed update? */
	if (system_state != SYSTEM_RUNNING)
		return UCODE_NFOUND;
485

486 487
	ustate = microcode_ops->request_microcode_fw(cpu, &microcode_pdev->dev,
						     refresh_fw);
488

489
	if (ustate == UCODE_OK) {
490
		pr_debug("CPU%d updated upon init\n", cpu);
491
		apply_microcode_on_target(cpu);
492 493
	}

494
	return ustate;
495 496
}

497
static enum ucode_state microcode_update_cpu(int cpu)
498
{
499
	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
500

501 502 503
	/* Refresh CPU microcode revision after resume. */
	collect_cpu_info(cpu);

504
	if (uci->valid)
505
		return microcode_resume_cpu(cpu);
506

507
	return microcode_init_cpu(cpu, false);
508 509
}

510
static int mc_device_add(struct device *dev, struct subsys_interface *sif)
P
Peter Oruba 已提交
511
{
512
	int err, cpu = dev->id;
P
Peter Oruba 已提交
513 514 515 516

	if (!cpu_online(cpu))
		return 0;

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

519
	err = sysfs_create_group(&dev->kobj, &mc_attr_group);
P
Peter Oruba 已提交
520 521 522
	if (err)
		return err;

523
	if (microcode_init_cpu(cpu, true) == UCODE_ERROR)
524
		return -EINVAL;
525 526

	return err;
P
Peter Oruba 已提交
527 528
}

529
static void mc_device_remove(struct device *dev, struct subsys_interface *sif)
P
Peter Oruba 已提交
530
{
531
	int cpu = dev->id;
P
Peter Oruba 已提交
532 533

	if (!cpu_online(cpu))
534
		return;
P
Peter Oruba 已提交
535

536
	pr_debug("CPU%d removed\n", cpu);
537
	microcode_fini_cpu(cpu);
538
	sysfs_remove_group(&dev->kobj, &mc_attr_group);
P
Peter Oruba 已提交
539 540
}

541 542 543 544 545
static struct subsys_interface mc_cpu_interface = {
	.name			= "microcode",
	.subsys			= &cpu_subsys,
	.add_dev		= mc_device_add,
	.remove_dev		= mc_device_remove,
546 547 548 549 550 551
};

/**
 * mc_bp_resume - Update boot CPU microcode during resume.
 */
static void mc_bp_resume(void)
P
Peter Oruba 已提交
552
{
553
	int cpu = smp_processor_id();
554
	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
P
Peter Oruba 已提交
555

556 557
	if (uci->valid && uci->mc)
		microcode_ops->apply_microcode(cpu);
558
	else if (!uci->mc)
559
		reload_early_microcode();
P
Peter Oruba 已提交
560 561
}

562 563
static struct syscore_ops mc_syscore_ops = {
	.resume			= mc_bp_resume,
P
Peter Oruba 已提交
564 565
};

566
static int mc_cpu_online(unsigned int cpu)
P
Peter Oruba 已提交
567
{
568
	struct device *dev;
P
Peter Oruba 已提交
569

570
	dev = get_cpu_device(cpu);
571 572
	microcode_update_cpu(cpu);
	pr_debug("CPU%d added\n", cpu);
573

574 575 576 577
	if (sysfs_create_group(&dev->kobj, &mc_attr_group))
		pr_err("Failed to create group for CPU%d\n", cpu);
	return 0;
}
578

579 580 581
static int mc_cpu_down_prep(unsigned int cpu)
{
	struct device *dev;
582

583 584 585 586
	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);
587 588 589 590 591 592
	/*
	 * When a CPU goes offline, don't free up or invalidate the copy of
	 * the microcode in kernel memory, so that we can reuse it when the
	 * CPU comes back online without unnecessarily requesting the userspace
	 * for it again.
	 */
593
	return 0;
P
Peter Oruba 已提交
594 595
}

596 597 598 599 600 601 602 603 604 605
static struct attribute *cpu_root_microcode_attrs[] = {
	&dev_attr_reload.attr,
	NULL
};

static struct attribute_group cpu_root_microcode_group = {
	.name  = "microcode",
	.attrs = cpu_root_microcode_attrs,
};

606
int __init microcode_init(void)
P
Peter Oruba 已提交
607
{
608
	struct cpuinfo_x86 *c = &boot_cpu_data;
P
Peter Oruba 已提交
609 610
	int error;

611
	if (dis_ucode_ldr)
612
		return -EINVAL;
613

614 615
	if (c->x86_vendor == X86_VENDOR_INTEL)
		microcode_ops = init_intel_microcode();
P
Peter Oruba 已提交
616
	else if (c->x86_vendor == X86_VENDOR_AMD)
617
		microcode_ops = init_amd_microcode();
618
	else
619
		pr_err("no support for this CPU vendor\n");
620 621

	if (!microcode_ops)
622
		return -ENODEV;
P
Peter Oruba 已提交
623 624 625

	microcode_pdev = platform_device_register_simple("microcode", -1,
							 NULL, 0);
626
	if (IS_ERR(microcode_pdev))
P
Peter Oruba 已提交
627 628 629
		return PTR_ERR(microcode_pdev);

	get_online_cpus();
630 631
	mutex_lock(&microcode_mutex);

632
	error = subsys_interface_register(&mc_cpu_interface);
633 634
	if (!error)
		perf_check_microcode();
635
	mutex_unlock(&microcode_mutex);
P
Peter Oruba 已提交
636
	put_online_cpus();
637

638 639
	if (error)
		goto out_pdev;
P
Peter Oruba 已提交
640

641 642 643 644 645 646 647 648
	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;
	}

649 650
	error = microcode_dev_init();
	if (error)
651
		goto out_ucode_group;
652

653
	register_syscore_ops(&mc_syscore_ops);
654 655
	cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "x86/microcode:online",
				  mc_cpu_online, mc_cpu_down_prep);
P
Peter Oruba 已提交
656

657
	pr_info("Microcode Update Driver: v%s.", MICROCODE_VERSION);
P
Peter Oruba 已提交
658

P
Peter Oruba 已提交
659
	return 0;
660

661 662 663 664 665
 out_ucode_group:
	sysfs_remove_group(&cpu_subsys.dev_root->kobj,
			   &cpu_root_microcode_group);

 out_driver:
666 667 668
	get_online_cpus();
	mutex_lock(&microcode_mutex);

669
	subsys_interface_unregister(&mc_cpu_interface);
670 671 672 673

	mutex_unlock(&microcode_mutex);
	put_online_cpus();

674
 out_pdev:
675 676 677
	platform_device_unregister(microcode_pdev);
	return error;

P
Peter Oruba 已提交
678
}
679
fs_initcall(save_microcode_in_initrd);
680
late_initcall(microcode_init);