core.c 12.7 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
 * This driver allows to upgrade microcode on x86 processors.
P
Peter Oruba 已提交
9
 *
10 11 12 13
 * 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 已提交
14
 */
15 16 17

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

I
Ingo Molnar 已提交
18 19
#include <linux/platform_device.h>
#include <linux/miscdevice.h>
20
#include <linux/capability.h>
I
Ingo Molnar 已提交
21 22
#include <linux/kernel.h>
#include <linux/module.h>
P
Peter Oruba 已提交
23 24
#include <linux/mutex.h>
#include <linux/cpu.h>
I
Ingo Molnar 已提交
25 26
#include <linux/fs.h>
#include <linux/mm.h>
27
#include <linux/syscore_ops.h>
P
Peter Oruba 已提交
28 29

#include <asm/microcode.h>
I
Ingo Molnar 已提交
30
#include <asm/processor.h>
31
#include <asm/cpu_device_id.h>
32
#include <asm/perf_event.h>
P
Peter Oruba 已提交
33 34 35 36 37

MODULE_DESCRIPTION("Microcode Update Driver");
MODULE_AUTHOR("Tigran Aivazian <tigran@aivazian.fsnet.co.uk>");
MODULE_LICENSE("GPL");

I
Ingo Molnar 已提交
38
#define MICROCODE_VERSION	"2.00"
P
Peter Oruba 已提交
39

I
Ingo Molnar 已提交
40
static struct microcode_ops	*microcode_ops;
P
Peter Oruba 已提交
41

42 43 44
bool dis_ucode_ldr;
module_param(dis_ucode_ldr, bool, 0);

45 46 47 48 49 50 51 52 53 54 55 56
/*
 * 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.
 */
57
static DEFINE_MUTEX(microcode_mutex);
P
Peter Oruba 已提交
58

I
Ingo Molnar 已提交
59
struct ucode_cpu_info		ucode_cpu_info[NR_CPUS];
P
Peter Oruba 已提交
60
EXPORT_SYMBOL_GPL(ucode_cpu_info);
P
Peter Oruba 已提交
61

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 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 118 119 120 121 122 123 124 125 126 127
/*
 * Operations that are run on a target cpu:
 */

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

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 已提交
128
#ifdef CONFIG_MICROCODE_OLD_INTERFACE
D
Dmitry Adamushko 已提交
129
static int do_microcode_update(const void __user *buf, size_t size)
P
Peter Oruba 已提交
130 131 132
{
	int error = 0;
	int cpu;
133

D
Dmitry Adamushko 已提交
134 135
	for_each_online_cpu(cpu) {
		struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
136
		enum ucode_state ustate;
D
Dmitry Adamushko 已提交
137 138 139

		if (!uci->valid)
			continue;
140

141 142 143 144 145 146
		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 已提交
147
	}
148

P
Peter Oruba 已提交
149 150 151
	return error;
}

152
static int microcode_open(struct inode *inode, struct file *file)
P
Peter Oruba 已提交
153
{
154
	return capable(CAP_SYS_RAWIO) ? nonseekable_open(inode, file) : -EPERM;
P
Peter Oruba 已提交
155 156
}

157 158
static ssize_t microcode_write(struct file *file, const char __user *buf,
			       size_t len, loff_t *ppos)
P
Peter Oruba 已提交
159
{
160
	ssize_t ret = -EINVAL;
P
Peter Oruba 已提交
161

162
	if ((len >> PAGE_SHIFT) > totalram_pages) {
163
		pr_err("too much data (max %ld pages)\n", totalram_pages);
164
		return ret;
P
Peter Oruba 已提交
165 166 167 168 169
	}

	get_online_cpus();
	mutex_lock(&microcode_mutex);

170
	if (do_microcode_update(buf, len) == 0)
P
Peter Oruba 已提交
171 172
		ret = (ssize_t)len;

173 174 175
	if (ret > 0)
		perf_check_microcode();

P
Peter Oruba 已提交
176 177 178 179 180 181 182
	mutex_unlock(&microcode_mutex);
	put_online_cpus();

	return ret;
}

static const struct file_operations microcode_fops = {
183 184 185
	.owner			= THIS_MODULE,
	.write			= microcode_write,
	.open			= microcode_open,
186
	.llseek		= no_llseek,
P
Peter Oruba 已提交
187 188 189
};

static struct miscdevice microcode_dev = {
190 191
	.minor			= MICROCODE_MINOR,
	.name			= "microcode",
192
	.nodename		= "cpu/microcode",
193
	.fops			= &microcode_fops,
P
Peter Oruba 已提交
194 195
};

196
static int __init microcode_dev_init(void)
P
Peter Oruba 已提交
197 198 199 200 201
{
	int error;

	error = misc_register(&microcode_dev);
	if (error) {
202
		pr_err("can't misc_register on minor=%d\n", MICROCODE_MINOR);
P
Peter Oruba 已提交
203 204 205 206 207 208
		return error;
	}

	return 0;
}

209
static void __exit microcode_dev_exit(void)
P
Peter Oruba 已提交
210 211 212 213 214
{
	misc_deregister(&microcode_dev);
}

MODULE_ALIAS_MISCDEV(MICROCODE_MINOR);
215
MODULE_ALIAS("devname:cpu/microcode");
P
Peter Oruba 已提交
216
#else
I
Ingo Molnar 已提交
217 218
#define microcode_dev_init()	0
#define microcode_dev_exit()	do { } while (0)
P
Peter Oruba 已提交
219 220 221
#endif

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

224
static int reload_for_cpu(int cpu)
225
{
226
	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
227
	enum ucode_state ustate;
228 229
	int err = 0;

230 231
	if (!uci->valid)
		return err;
232

233
	ustate = microcode_ops->request_microcode_fw(cpu, &microcode_pdev->dev, true);
234 235 236 237 238
	if (ustate == UCODE_OK)
		apply_microcode_on_target(cpu);
	else
		if (ustate == UCODE_ERROR)
			err = -EINVAL;
239 240 241
	return err;
}

242 243
static ssize_t reload_store(struct device *dev,
			    struct device_attribute *attr,
244
			    const char *buf, size_t size)
P
Peter Oruba 已提交
245
{
246
	unsigned long val;
247 248 249
	int cpu;
	ssize_t ret = 0, tmp_ret;

250 251 252
	ret = kstrtoul(buf, 0, &val);
	if (ret)
		return ret;
253

254 255 256 257
	if (val != 1)
		return size;

	get_online_cpus();
258
	mutex_lock(&microcode_mutex);
259 260 261 262 263 264 265 266
	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 已提交
267
	}
268 269 270
	if (!ret)
		perf_check_microcode();
	mutex_unlock(&microcode_mutex);
271
	put_online_cpus();
272 273 274 275 276

	if (!ret)
		ret = size;

	return ret;
P
Peter Oruba 已提交
277 278
}

279 280
static ssize_t version_show(struct device *dev,
			struct device_attribute *attr, char *buf)
P
Peter Oruba 已提交
281 282 283
{
	struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;

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

287 288
static ssize_t pf_show(struct device *dev,
			struct device_attribute *attr, char *buf)
P
Peter Oruba 已提交
289 290 291
{
	struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;

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

295 296 297
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 已提交
298 299

static struct attribute *mc_default_attrs[] = {
300 301
	&dev_attr_version.attr,
	&dev_attr_processor_flags.attr,
P
Peter Oruba 已提交
302 303 304 305
	NULL
};

static struct attribute_group mc_attr_group = {
306 307
	.attrs			= mc_default_attrs,
	.name			= "microcode",
P
Peter Oruba 已提交
308 309
};

310
static void microcode_fini_cpu(int cpu)
311 312
{
	microcode_ops->microcode_fini_cpu(cpu);
313 314
}

315
static enum ucode_state microcode_resume_cpu(int cpu)
316
{
317
	pr_debug("CPU%d updated upon resume\n", cpu);
318 319 320

	if (apply_microcode_on_target(cpu))
		return UCODE_ERROR;
321 322

	return UCODE_OK;
323 324
}

325
static enum ucode_state microcode_init_cpu(int cpu, bool refresh_fw)
326
{
327
	enum ucode_state ustate;
328 329 330 331
	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;

	if (uci && uci->valid)
		return UCODE_OK;
332

333 334
	if (collect_cpu_info(cpu))
		return UCODE_ERROR;
335

336 337 338
	/* --dimm. Trigger a delayed update? */
	if (system_state != SYSTEM_RUNNING)
		return UCODE_NFOUND;
339

340 341
	ustate = microcode_ops->request_microcode_fw(cpu, &microcode_pdev->dev,
						     refresh_fw);
342

343
	if (ustate == UCODE_OK) {
344
		pr_debug("CPU%d updated upon init\n", cpu);
345
		apply_microcode_on_target(cpu);
346 347
	}

348
	return ustate;
349 350
}

351
static enum ucode_state microcode_update_cpu(int cpu)
352
{
353
	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
354

355
	if (uci->valid)
356
		return microcode_resume_cpu(cpu);
357

358
	return microcode_init_cpu(cpu, false);
359 360
}

361
static int mc_device_add(struct device *dev, struct subsys_interface *sif)
P
Peter Oruba 已提交
362
{
363
	int err, cpu = dev->id;
P
Peter Oruba 已提交
364 365 366 367

	if (!cpu_online(cpu))
		return 0;

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

370
	err = sysfs_create_group(&dev->kobj, &mc_attr_group);
P
Peter Oruba 已提交
371 372 373
	if (err)
		return err;

374
	if (microcode_init_cpu(cpu, true) == UCODE_ERROR)
375
		return -EINVAL;
376 377

	return err;
P
Peter Oruba 已提交
378 379
}

380
static int mc_device_remove(struct device *dev, struct subsys_interface *sif)
P
Peter Oruba 已提交
381
{
382
	int cpu = dev->id;
P
Peter Oruba 已提交
383 384 385 386

	if (!cpu_online(cpu))
		return 0;

387
	pr_debug("CPU%d removed\n", cpu);
388
	microcode_fini_cpu(cpu);
389
	sysfs_remove_group(&dev->kobj, &mc_attr_group);
P
Peter Oruba 已提交
390 391 392
	return 0;
}

393 394 395 396 397
static struct subsys_interface mc_cpu_interface = {
	.name			= "microcode",
	.subsys			= &cpu_subsys,
	.add_dev		= mc_device_add,
	.remove_dev		= mc_device_remove,
398 399 400 401 402 403
};

/**
 * mc_bp_resume - Update boot CPU microcode during resume.
 */
static void mc_bp_resume(void)
P
Peter Oruba 已提交
404
{
405
	int cpu = smp_processor_id();
406
	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
P
Peter Oruba 已提交
407

408 409
	if (uci->valid && uci->mc)
		microcode_ops->apply_microcode(cpu);
410
	else if (!uci->mc)
411
		reload_early_microcode();
P
Peter Oruba 已提交
412 413
}

414 415
static struct syscore_ops mc_syscore_ops = {
	.resume			= mc_bp_resume,
P
Peter Oruba 已提交
416 417
};

418
static int
P
Peter Oruba 已提交
419 420 421
mc_cpu_callback(struct notifier_block *nb, unsigned long action, void *hcpu)
{
	unsigned int cpu = (unsigned long)hcpu;
422
	struct device *dev;
P
Peter Oruba 已提交
423

424
	dev = get_cpu_device(cpu);
425 426

	switch (action & ~CPU_TASKS_FROZEN) {
P
Peter Oruba 已提交
427
	case CPU_ONLINE:
428
		microcode_update_cpu(cpu);
429
		pr_debug("CPU%d added\n", cpu);
430 431 432 433 434 435
		/*
		 * "break" is missing on purpose here because we want to fall
		 * through in order to create the sysfs group.
		 */

	case CPU_DOWN_FAILED:
436
		if (sysfs_create_group(&dev->kobj, &mc_attr_group))
437
			pr_err("Failed to create group for CPU%d\n", cpu);
P
Peter Oruba 已提交
438
		break;
439

P
Peter Oruba 已提交
440 441
	case CPU_DOWN_PREPARE:
		/* Suspend is in progress, only remove the interface */
442
		sysfs_remove_group(&dev->kobj, &mc_attr_group);
443
		pr_debug("CPU%d removed\n", cpu);
444
		break;
445 446

	/*
447 448
	 * case CPU_DEAD:
	 *
449 450 451 452 453
	 * 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.
	 */
P
Peter Oruba 已提交
454
	}
455 456 457 458 459

	/* The CPU refused to come up during a system resume */
	if (action == CPU_UP_CANCELED_FROZEN)
		microcode_fini_cpu(cpu);

P
Peter Oruba 已提交
460 461 462 463
	return NOTIFY_OK;
}

static struct notifier_block __refdata mc_cpu_notifier = {
I
Ingo Molnar 已提交
464
	.notifier_call	= mc_cpu_callback,
P
Peter Oruba 已提交
465 466
};

467 468
#ifdef MODULE
/* Autoload on Intel and AMD systems */
469
static const struct x86_cpu_id __initconst microcode_id[] = {
470 471 472 473 474 475 476 477 478 479 480
#ifdef CONFIG_MICROCODE_INTEL
	{ X86_VENDOR_INTEL, X86_FAMILY_ANY, X86_MODEL_ANY, },
#endif
#ifdef CONFIG_MICROCODE_AMD
	{ X86_VENDOR_AMD, X86_FAMILY_ANY, X86_MODEL_ANY, },
#endif
	{}
};
MODULE_DEVICE_TABLE(x86cpu, microcode_id);
#endif

481 482 483 484 485 486 487 488 489 490
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,
};

491
static int __init microcode_init(void)
P
Peter Oruba 已提交
492
{
493
	struct cpuinfo_x86 *c = &cpu_data(0);
P
Peter Oruba 已提交
494 495
	int error;

496
	if (paravirt_enabled() || dis_ucode_ldr)
497
		return -EINVAL;
498

499 500
	if (c->x86_vendor == X86_VENDOR_INTEL)
		microcode_ops = init_intel_microcode();
P
Peter Oruba 已提交
501
	else if (c->x86_vendor == X86_VENDOR_AMD)
502
		microcode_ops = init_amd_microcode();
503
	else
504
		pr_err("no support for this CPU vendor\n");
505 506

	if (!microcode_ops)
507
		return -ENODEV;
P
Peter Oruba 已提交
508 509 510

	microcode_pdev = platform_device_register_simple("microcode", -1,
							 NULL, 0);
511
	if (IS_ERR(microcode_pdev))
P
Peter Oruba 已提交
512 513 514
		return PTR_ERR(microcode_pdev);

	get_online_cpus();
515 516
	mutex_lock(&microcode_mutex);

517
	error = subsys_interface_register(&mc_cpu_interface);
518 519
	if (!error)
		perf_check_microcode();
520
	mutex_unlock(&microcode_mutex);
P
Peter Oruba 已提交
521
	put_online_cpus();
522

523 524
	if (error)
		goto out_pdev;
P
Peter Oruba 已提交
525

526 527 528 529 530 531 532 533
	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;
	}

534 535
	error = microcode_dev_init();
	if (error)
536
		goto out_ucode_group;
537

538
	register_syscore_ops(&mc_syscore_ops);
P
Peter Oruba 已提交
539
	register_hotcpu_notifier(&mc_cpu_notifier);
P
Peter Oruba 已提交
540

541
	pr_info("Microcode Update Driver: v" MICROCODE_VERSION
542
		" <tigran@aivazian.fsnet.co.uk>, Peter Oruba\n");
P
Peter Oruba 已提交
543

P
Peter Oruba 已提交
544
	return 0;
545

546 547 548 549 550
 out_ucode_group:
	sysfs_remove_group(&cpu_subsys.dev_root->kobj,
			   &cpu_root_microcode_group);

 out_driver:
551 552 553
	get_online_cpus();
	mutex_lock(&microcode_mutex);

554
	subsys_interface_unregister(&mc_cpu_interface);
555 556 557 558

	mutex_unlock(&microcode_mutex);
	put_online_cpus();

559
 out_pdev:
560 561 562
	platform_device_unregister(microcode_pdev);
	return error;

P
Peter Oruba 已提交
563
}
564
module_init(microcode_init);
P
Peter Oruba 已提交
565

566
static void __exit microcode_exit(void)
P
Peter Oruba 已提交
567
{
568 569
	struct cpuinfo_x86 *c = &cpu_data(0);

P
Peter Oruba 已提交
570 571 572
	microcode_dev_exit();

	unregister_hotcpu_notifier(&mc_cpu_notifier);
573
	unregister_syscore_ops(&mc_syscore_ops);
P
Peter Oruba 已提交
574

575 576 577
	sysfs_remove_group(&cpu_subsys.dev_root->kobj,
			   &cpu_root_microcode_group);

P
Peter Oruba 已提交
578
	get_online_cpus();
579 580
	mutex_lock(&microcode_mutex);

581
	subsys_interface_unregister(&mc_cpu_interface);
582 583

	mutex_unlock(&microcode_mutex);
P
Peter Oruba 已提交
584 585 586 587
	put_online_cpus();

	platform_device_unregister(microcode_pdev);

P
Peter Oruba 已提交
588 589
	microcode_ops = NULL;

590 591 592
	if (c->x86_vendor == X86_VENDOR_AMD)
		exit_amd_microcode();

593
	pr_info("Microcode Update Driver: v" MICROCODE_VERSION " removed.\n");
P
Peter Oruba 已提交
594
}
595
module_exit(microcode_exit);