ia64/xen-unstable

view xen/arch/x86/microcode_intel.c @ 18499:3eb7a0cfffc2

x86, microcode: More code cleanups.
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Sep 16 13:09:04 2008 +0100 (2008-09-16)
parents 087b8b29b6b2
children fb0dc8143932
line source
1 /*
2 * Intel CPU Microcode Update Driver for Linux
3 *
4 * Copyright (C) 2000-2006 Tigran Aivazian <tigran@aivazian.fsnet.co.uk>
5 * 2006 Shaohua Li <shaohua.li@intel.com> *
6 * This driver allows to upgrade microcode on Intel processors
7 * belonging to IA-32 family - PentiumPro, Pentium II,
8 * Pentium III, Xeon, Pentium 4, etc.
9 *
10 * Reference: Section 8.11 of Volume 3a, IA-32 Intel? Architecture
11 * Software Developer's Manual
12 * Order Number 253668 or free download from:
13 *
14 * http://developer.intel.com/design/pentium4/manuals/253668.htm
15 *
16 * For more information, go to http://www.urbanmyth.org/microcode
17 *
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License
20 * as published by the Free Software Foundation; either version
21 * 2 of the License, or (at your option) any later version.
22 */
24 #include <xen/config.h>
25 #include <xen/lib.h>
26 #include <xen/kernel.h>
27 #include <xen/init.h>
28 #include <xen/sched.h>
29 #include <xen/smp.h>
30 #include <xen/spinlock.h>
32 #include <asm/msr.h>
33 #include <asm/uaccess.h>
34 #include <asm/processor.h>
35 #include <asm/microcode.h>
37 #define pr_debug(x...) ((void)0)
39 #define DEFAULT_UCODE_DATASIZE (2000)
40 #define MC_HEADER_SIZE (sizeof(struct microcode_header_intel))
41 #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE)
42 #define EXT_HEADER_SIZE (sizeof(struct extended_sigtable))
43 #define EXT_SIGNATURE_SIZE (sizeof(struct extended_signature))
44 #define DWSIZE (sizeof(u32))
45 #define get_totalsize(mc) \
46 (((struct microcode_intel *)mc)->hdr.totalsize ? \
47 ((struct microcode_intel *)mc)->hdr.totalsize : \
48 DEFAULT_UCODE_TOTALSIZE)
50 #define get_datasize(mc) \
51 (((struct microcode_intel *)mc)->hdr.datasize ? \
52 ((struct microcode_intel *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE)
54 #define sigmatch(s1, s2, p1, p2) \
55 (((s1) == (s2)) && (((p1) & (p2)) || (((p1) == 0) && ((p2) == 0))))
57 #define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE)
59 /* serialize access to the physical write to MSR 0x79 */
60 static DEFINE_SPINLOCK(microcode_update_lock);
62 static int collect_cpu_info(int cpu_num, struct cpu_signature *csig)
63 {
64 struct cpuinfo_x86 *c = &cpu_data[cpu_num];
65 unsigned int val[2];
67 memset(csig, 0, sizeof(*csig));
69 if ( (c->x86_vendor != X86_VENDOR_INTEL) || (c->x86 < 6) ||
70 cpu_has(c, X86_FEATURE_IA64) )
71 {
72 printk(KERN_ERR "microcode: CPU%d not a capable Intel "
73 "processor\n", cpu_num);
74 return -1;
75 }
77 csig->sig = cpuid_eax(0x00000001);
79 if ( (c->x86_model >= 5) || (c->x86 > 6) )
80 {
81 /* get processor flags from MSR 0x17 */
82 rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
83 csig->pf = 1 << ((val[1] >> 18) & 7);
84 }
86 wrmsr(MSR_IA32_UCODE_REV, 0, 0);
87 /* see notes above for revision 1.07. Apparent chip bug */
88 sync_core();
89 /* get the current revision from MSR 0x8B */
90 rdmsr(MSR_IA32_UCODE_REV, val[0], csig->rev);
91 pr_debug("microcode: collect_cpu_info : sig=0x%x, pf=0x%x, rev=0x%x\n",
92 csig->sig, csig->pf, csig->rev);
94 return 0;
95 }
97 static inline int microcode_update_match(
98 int cpu_num, struct microcode_header_intel *mc_header, int sig, int pf)
99 {
100 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
102 return (sigmatch(sig, uci->cpu_sig.sig, pf, uci->cpu_sig.pf) &&
103 (mc_header->rev > uci->cpu_sig.rev));
104 }
106 static int microcode_sanity_check(void *mc)
107 {
108 struct microcode_header_intel *mc_header = mc;
109 struct extended_sigtable *ext_header = NULL;
110 struct extended_signature *ext_sig;
111 unsigned long total_size, data_size, ext_table_size;
112 int sum, orig_sum, ext_sigcount = 0, i;
114 total_size = get_totalsize(mc_header);
115 data_size = get_datasize(mc_header);
116 if ( (data_size + MC_HEADER_SIZE) > total_size )
117 {
118 printk(KERN_ERR "microcode: error! "
119 "Bad data size in microcode data file\n");
120 return -EINVAL;
121 }
123 if ( (mc_header->ldrver != 1) || (mc_header->hdrver != 1) )
124 {
125 printk(KERN_ERR "microcode: error! "
126 "Unknown microcode update format\n");
127 return -EINVAL;
128 }
129 ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
130 if ( ext_table_size )
131 {
132 if ( (ext_table_size < EXT_HEADER_SIZE) ||
133 ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE) )
134 {
135 printk(KERN_ERR "microcode: error! "
136 "Small exttable size in microcode data file\n");
137 return -EINVAL;
138 }
139 ext_header = mc + MC_HEADER_SIZE + data_size;
140 if ( ext_table_size != exttable_size(ext_header) )
141 {
142 printk(KERN_ERR "microcode: error! "
143 "Bad exttable size in microcode data file\n");
144 return -EFAULT;
145 }
146 ext_sigcount = ext_header->count;
147 }
149 /* check extended table checksum */
150 if ( ext_table_size )
151 {
152 int ext_table_sum = 0;
153 int *ext_tablep = (int *)ext_header;
155 i = ext_table_size / DWSIZE;
156 while ( i-- )
157 ext_table_sum += ext_tablep[i];
158 if ( ext_table_sum )
159 {
160 printk(KERN_WARNING "microcode: aborting, "
161 "bad extended signature table checksum\n");
162 return -EINVAL;
163 }
164 }
166 /* calculate the checksum */
167 orig_sum = 0;
168 i = (MC_HEADER_SIZE + data_size) / DWSIZE;
169 while ( i-- )
170 orig_sum += ((int *)mc)[i];
171 if ( orig_sum )
172 {
173 printk(KERN_ERR "microcode: aborting, bad checksum\n");
174 return -EINVAL;
175 }
176 if ( !ext_table_size )
177 return 0;
178 /* check extended signature checksum */
179 for ( i = 0; i < ext_sigcount; i++ )
180 {
181 ext_sig = (void *)ext_header + EXT_HEADER_SIZE +
182 EXT_SIGNATURE_SIZE * i;
183 sum = orig_sum
184 - (mc_header->sig + mc_header->pf + mc_header->cksum)
185 + (ext_sig->sig + ext_sig->pf + ext_sig->cksum);
186 if ( sum )
187 {
188 printk(KERN_ERR "microcode: aborting, bad checksum\n");
189 return -EINVAL;
190 }
191 }
192 return 0;
193 }
195 /*
196 * return 0 - no update found
197 * return 1 - found update
198 * return < 0 - error
199 */
200 static int get_matching_microcode(void *mc, int cpu)
201 {
202 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
203 struct microcode_header_intel *mc_header = mc;
204 struct extended_sigtable *ext_header;
205 unsigned long total_size = get_totalsize(mc_header);
206 int ext_sigcount, i;
207 struct extended_signature *ext_sig;
208 void *new_mc;
210 if ( microcode_update_match(cpu, mc_header,
211 mc_header->sig, mc_header->pf) )
212 goto find;
214 if ( total_size <= (get_datasize(mc_header) + MC_HEADER_SIZE) )
215 return 0;
217 ext_header = mc + get_datasize(mc_header) + MC_HEADER_SIZE;
218 ext_sigcount = ext_header->count;
219 ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
220 for ( i = 0; i < ext_sigcount; i++ )
221 {
222 if ( microcode_update_match(cpu, mc_header,
223 ext_sig->sig, ext_sig->pf) )
224 goto find;
225 ext_sig++;
226 }
227 return 0;
228 find:
229 pr_debug("microcode: CPU%d found a matching microcode update with"
230 " version 0x%x (current=0x%x)\n",
231 cpu, mc_header->rev, uci->cpu_sig.rev);
232 new_mc = xmalloc_bytes(total_size);
233 if ( new_mc == NULL )
234 {
235 printk(KERN_ERR "microcode: error! Can not allocate memory\n");
236 return -ENOMEM;
237 }
239 /* free previous update file */
240 xfree(uci->mc.mc_intel);
242 memcpy(new_mc, mc, total_size);
243 uci->mc.mc_intel = new_mc;
244 return 1;
245 }
247 static int apply_microcode(int cpu)
248 {
249 unsigned long flags;
250 unsigned int val[2];
251 int cpu_num = raw_smp_processor_id();
252 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
254 /* We should bind the task to the CPU */
255 BUG_ON(cpu_num != cpu);
257 if ( uci->mc.mc_intel == NULL )
258 return -EINVAL;
260 /* serialize access to the physical write to MSR 0x79 */
261 spin_lock_irqsave(&microcode_update_lock, flags);
263 /* write microcode via MSR 0x79 */
264 wrmsr(MSR_IA32_UCODE_WRITE,
265 (unsigned long) uci->mc.mc_intel->bits,
266 (unsigned long) uci->mc.mc_intel->bits >> 16 >> 16);
267 wrmsr(MSR_IA32_UCODE_REV, 0, 0);
269 /* see notes above for revision 1.07. Apparent chip bug */
270 sync_core();
272 /* get the current revision from MSR 0x8B */
273 rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
275 spin_unlock_irqrestore(&microcode_update_lock, flags);
276 if ( val[1] != uci->mc.mc_intel->hdr.rev )
277 {
278 printk(KERN_ERR "microcode: CPU%d update from revision "
279 "0x%x to 0x%x failed\n", cpu_num, uci->cpu_sig.rev, val[1]);
280 return -EIO;
281 }
282 printk(KERN_INFO "microcode: CPU%d updated from revision "
283 "0x%x to 0x%x, date = %04x-%02x-%02x \n",
284 cpu_num, uci->cpu_sig.rev, val[1],
285 uci->mc.mc_intel->hdr.date & 0xffff,
286 uci->mc.mc_intel->hdr.date >> 24,
287 (uci->mc.mc_intel->hdr.date >> 16) & 0xff);
288 uci->cpu_sig.rev = val[1];
290 return 0;
291 }
293 static long get_next_ucode_from_buffer(void **mc, const u8 *buf,
294 unsigned long size, long offset)
295 {
296 struct microcode_header_intel *mc_header;
297 unsigned long total_size;
299 /* No more data */
300 if ( offset >= size )
301 return 0;
302 mc_header = (struct microcode_header_intel *)(buf + offset);
303 total_size = get_totalsize(mc_header);
305 if ( (offset + total_size) > size )
306 {
307 printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
308 return -EINVAL;
309 }
311 *mc = xmalloc_bytes(total_size);
312 if ( *mc == NULL )
313 {
314 printk(KERN_ERR "microcode: error! Can not allocate memory\n");
315 return -ENOMEM;
316 }
317 memcpy(*mc, (const void *)(buf + offset), total_size);
318 return offset + total_size;
319 }
321 static int cpu_request_microcode(int cpu, const void *buf, size_t size)
322 {
323 long offset = 0;
324 int error = 0;
325 void *mc;
327 /* We should bind the task to the CPU */
328 BUG_ON(cpu != raw_smp_processor_id());
330 while ( (offset = get_next_ucode_from_buffer(&mc, buf, size, offset)) > 0 )
331 {
332 error = microcode_sanity_check(mc);
333 if ( error )
334 break;
335 error = get_matching_microcode(mc, cpu);
336 if ( error < 0 )
337 break;
338 /*
339 * It's possible the data file has multiple matching ucode,
340 * lets keep searching till the latest version
341 */
342 if ( error == 1 )
343 {
344 apply_microcode(cpu);
345 error = 0;
346 }
347 xfree(mc);
348 }
349 if ( offset > 0 )
350 xfree(mc);
351 if ( offset < 0 )
352 error = offset;
354 return error;
355 }
357 static struct microcode_ops microcode_intel_ops = {
358 .get_matching_microcode = get_matching_microcode,
359 .cpu_request_microcode = cpu_request_microcode,
360 .collect_cpu_info = collect_cpu_info,
361 .apply_microcode = apply_microcode,
362 };
364 static __init int microcode_init_intel(void)
365 {
366 if ( boot_cpu_data.x86_vendor == X86_VENDOR_INTEL )
367 microcode_ops = &microcode_intel_ops;
368 return 0;
369 }
370 __initcall(microcode_init_intel);