intel.c 6.5 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * Intel CPU Microcode Update Driver for Linux
L
Linus Torvalds 已提交
3
 *
4 5
 * Copyright (C) 2000-2006 Tigran Aivazian <tigran@aivazian.fsnet.co.uk>
 *		 2006 Shaohua Li <shaohua.li@intel.com>
L
Linus Torvalds 已提交
6
 *
7 8 9 10
 * 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.
L
Linus Torvalds 已提交
11
 */
12 13 14

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

I
Ingo Molnar 已提交
15 16 17 18
#include <linux/firmware.h>
#include <linux/uaccess.h>
#include <linux/kernel.h>
#include <linux/module.h>
19
#include <linux/vmalloc.h>
L
Linus Torvalds 已提交
20

21
#include <asm/microcode_intel.h>
I
Ingo Molnar 已提交
22 23
#include <asm/processor.h>
#include <asm/msr.h>
L
Linus Torvalds 已提交
24

P
Peter Oruba 已提交
25
MODULE_DESCRIPTION("Microcode Update Driver");
26
MODULE_AUTHOR("Tigran Aivazian <tigran@aivazian.fsnet.co.uk>");
L
Linus Torvalds 已提交
27 28
MODULE_LICENSE("GPL");

29
static int collect_cpu_info(int cpu_num, struct cpu_signature *csig)
L
Linus Torvalds 已提交
30
{
31
	struct cpuinfo_x86 *c = &cpu_data(cpu_num);
L
Linus Torvalds 已提交
32 33
	unsigned int val[2];

34
	memset(csig, 0, sizeof(*csig));
L
Linus Torvalds 已提交
35

36
	csig->sig = cpuid_eax(0x00000001);
37 38 39 40

	if ((c->x86_model >= 5) || (c->x86 > 6)) {
		/* get processor flags from MSR 0x17 */
		rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
41
		csig->pf = 1 << ((val[1] >> 18) & 7);
L
Linus Torvalds 已提交
42 43
	}

44
	csig->rev = c->microcode;
45 46
	pr_info("CPU%d sig=0x%x, pf=0x%x, revision=0x%x\n",
		cpu_num, csig->sig, csig->pf, csig->rev);
47 48

	return 0;
L
Linus Torvalds 已提交
49 50
}

51 52 53 54
/*
 * return 0 - no update found
 * return 1 - found update
 */
55
static int get_matching_mc(struct microcode_intel *mc_intel, int cpu)
56
{
57 58
	struct cpu_signature cpu_sig;
	unsigned int csig, cpf, crev;
59

60
	collect_cpu_info(cpu, &cpu_sig);
D
Dmitry Adamushko 已提交
61

62 63 64
	csig = cpu_sig.sig;
	cpf = cpu_sig.pf;
	crev = cpu_sig.rev;
65

66
	return has_newer_microcode(mc_intel, csig, cpf, crev);
L
Linus Torvalds 已提交
67 68
}

69
static int apply_microcode_intel(int cpu)
L
Linus Torvalds 已提交
70
{
I
Ingo Molnar 已提交
71 72
	struct microcode_intel *mc_intel;
	struct ucode_cpu_info *uci;
L
Linus Torvalds 已提交
73
	unsigned int val[2];
74 75
	int cpu_num = raw_smp_processor_id();
	struct cpuinfo_x86 *c = &cpu_data(cpu_num);
I
Ingo Molnar 已提交
76 77 78

	uci = ucode_cpu_info + cpu;
	mc_intel = uci->mc;
L
Linus Torvalds 已提交
79

80 81 82
	/* We should bind the task to the CPU */
	BUG_ON(cpu_num != cpu);

83
	if (mc_intel == NULL)
84
		return 0;
L
Linus Torvalds 已提交
85

86 87 88 89 90 91 92 93
	/*
	 * Microcode on this CPU could be updated earlier. Only apply the
	 * microcode patch in mc_intel when it is newer than the one on this
	 * CPU.
	 */
	if (get_matching_mc(mc_intel, cpu) == 0)
		return 0;

L
Linus Torvalds 已提交
94 95
	/* write microcode via MSR 0x79 */
	wrmsr(MSR_IA32_UCODE_WRITE,
96 97
	      (unsigned long) mc_intel->bits,
	      (unsigned long) mc_intel->bits >> 16 >> 16);
L
Linus Torvalds 已提交
98 99
	wrmsr(MSR_IA32_UCODE_REV, 0, 0);

100
	/* As documented in the SDM: Do a CPUID 1 here */
101
	sync_core();
102

L
Linus Torvalds 已提交
103 104 105
	/* get the current revision from MSR 0x8B */
	rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);

106
	if (val[1] != mc_intel->hdr.rev) {
107 108
		pr_err("CPU%d update to revision 0x%x failed\n",
		       cpu_num, mc_intel->hdr.rev);
109
		return -1;
110
	}
111
	pr_info("CPU%d updated to revision 0x%x, date = %04x-%02x-%02x\n",
112
		cpu_num, val[1],
113 114 115
		mc_intel->hdr.date & 0xffff,
		mc_intel->hdr.date >> 24,
		(mc_intel->hdr.date >> 16) & 0xff);
I
Ingo Molnar 已提交
116

117
	uci->cpu_sig.rev = val[1];
118
	c->microcode = val[1];
119 120

	return 0;
L
Linus Torvalds 已提交
121 122
}

123 124
static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size,
				int (*get_ucode_data)(void *, const void *, size_t))
125
{
D
Dmitry Adamushko 已提交
126
	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
127
	u8 *ucode_ptr = data, *new_mc = NULL, *mc = NULL;
D
Dmitry Adamushko 已提交
128 129
	int new_rev = uci->cpu_sig.rev;
	unsigned int leftover = size;
130
	enum ucode_state state = UCODE_OK;
131
	unsigned int curr_mc_size = 0;
132
	unsigned int csig, cpf;
133

D
Dmitry Adamushko 已提交
134 135 136
	while (leftover) {
		struct microcode_header_intel mc_header;
		unsigned int mc_size;
137

138 139 140 141 142
		if (leftover < sizeof(mc_header)) {
			pr_err("error! Truncated header in microcode data file\n");
			break;
		}

D
Dmitry Adamushko 已提交
143 144
		if (get_ucode_data(&mc_header, ucode_ptr, sizeof(mc_header)))
			break;
145

D
Dmitry Adamushko 已提交
146 147
		mc_size = get_totalsize(&mc_header);
		if (!mc_size || mc_size > leftover) {
148
			pr_err("error! Bad data in microcode data file\n");
D
Dmitry Adamushko 已提交
149 150
			break;
		}
151

152 153
		/* For performance reasons, reuse mc area when possible */
		if (!mc || mc_size > curr_mc_size) {
154
			vfree(mc);
155 156 157 158 159
			mc = vmalloc(mc_size);
			if (!mc)
				break;
			curr_mc_size = mc_size;
		}
D
Dmitry Adamushko 已提交
160 161

		if (get_ucode_data(mc, ucode_ptr, mc_size) ||
162
		    microcode_sanity_check(mc, 1) < 0) {
D
Dmitry Adamushko 已提交
163 164 165
			break;
		}

166 167
		csig = uci->cpu_sig.sig;
		cpf = uci->cpu_sig.pf;
168
		if (has_newer_microcode(mc, csig, cpf, new_rev)) {
169
			vfree(new_mc);
D
Dmitry Adamushko 已提交
170 171
			new_rev = mc_header.rev;
			new_mc  = mc;
172 173
			mc = NULL;	/* trigger new vmalloc */
		}
D
Dmitry Adamushko 已提交
174 175 176

		ucode_ptr += mc_size;
		leftover  -= mc_size;
177 178
	}

179
	vfree(mc);
180

181
	if (leftover) {
182
		vfree(new_mc);
183
		state = UCODE_ERROR;
I
Ingo Molnar 已提交
184
		goto out;
185
	}
I
Ingo Molnar 已提交
186

187 188
	if (!new_mc) {
		state = UCODE_NFOUND;
I
Ingo Molnar 已提交
189
		goto out;
190
	}
D
Dmitry Adamushko 已提交
191

192
	vfree(uci->mc);
I
Ingo Molnar 已提交
193 194
	uci->mc = (struct microcode_intel *)new_mc;

195 196 197 198 199 200 201
	/*
	 * If early loading microcode is supported, save this mc into
	 * permanent memory. So it will be loaded early when a CPU is hot added
	 * or resumes.
	 */
	save_mc_for_early(new_mc);

202 203
	pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n",
		 cpu, new_rev, uci->cpu_sig.rev);
204 205
out:
	return state;
206 207
}

D
Dmitry Adamushko 已提交
208 209 210 211 212
static int get_ucode_fw(void *to, const void *from, size_t n)
{
	memcpy(to, from, n);
	return 0;
}
213

214 215
static enum ucode_state request_microcode_fw(int cpu, struct device *device,
					     bool refresh_fw)
216 217
{
	char name[30];
218
	struct cpuinfo_x86 *c = &cpu_data(cpu);
219
	const struct firmware *firmware;
220
	enum ucode_state ret;
221

P
Peter Oruba 已提交
222
	sprintf(name, "intel-ucode/%02x-%02x-%02x",
223
		c->x86, c->x86_model, c->x86_mask);
224

225
	if (request_firmware_direct(&firmware, name, device)) {
226
		pr_debug("data file %s load failed\n", name);
227
		return UCODE_NFOUND;
228
	}
D
Dmitry Adamushko 已提交
229

230 231
	ret = generic_load_microcode(cpu, (void *)firmware->data,
				     firmware->size, &get_ucode_fw);
D
Dmitry Adamushko 已提交
232

233 234
	release_firmware(firmware);

D
Dmitry Adamushko 已提交
235 236 237 238 239 240 241 242
	return ret;
}

static int get_ucode_user(void *to, const void *from, size_t n)
{
	return copy_from_user(to, from, n);
}

243 244
static enum ucode_state
request_microcode_user(int cpu, const void __user *buf, size_t size)
D
Dmitry Adamushko 已提交
245
{
246
	return generic_load_microcode(cpu, (void *)buf, size, &get_ucode_user);
247 248
}

P
Peter Oruba 已提交
249
static void microcode_fini_cpu(int cpu)
250 251 252
{
	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;

253 254
	vfree(uci->mc);
	uci->mc = NULL;
255
}
P
Peter Oruba 已提交
256

H
Hannes Eder 已提交
257
static struct microcode_ops microcode_intel_ops = {
D
Dmitry Adamushko 已提交
258 259
	.request_microcode_user		  = request_microcode_user,
	.request_microcode_fw             = request_microcode_fw,
P
Peter Oruba 已提交
260
	.collect_cpu_info                 = collect_cpu_info,
261
	.apply_microcode                  = apply_microcode_intel,
P
Peter Oruba 已提交
262 263 264
	.microcode_fini_cpu               = microcode_fini_cpu,
};

265
struct microcode_ops * __init init_intel_microcode(void)
P
Peter Oruba 已提交
266
{
267
	struct cpuinfo_x86 *c = &boot_cpu_data;
268 269 270 271 272 273 274

	if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
	    cpu_has(c, X86_FEATURE_IA64)) {
		pr_err("Intel CPU family 0x%x not supported\n", c->x86);
		return NULL;
	}

275
	return &microcode_intel_ops;
P
Peter Oruba 已提交
276 277
}