ia64/xen-unstable

view xen/arch/x86/microcode_intel.c @ 19835:edfdeb150f27

Fix buildsystem to detect udev > version 124

udev removed the udevinfo symlink from versions higher than 123 and
xen's build-system could not detect if udev is in place and has the
required version.

Signed-off-by: Marc-A. Dahlhaus <mad@wol.de>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Jun 25 13:02:37 2009 +0100 (2009-06-25)
parents 9f9ba1a7cc92
children
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 BUG_ON(cpu_num != smp_processor_id());
69 memset(csig, 0, sizeof(*csig));
71 if ( (c->x86_vendor != X86_VENDOR_INTEL) || (c->x86 < 6) ||
72 cpu_has(c, X86_FEATURE_IA64) )
73 {
74 printk(KERN_ERR "microcode: CPU%d not a capable Intel "
75 "processor\n", cpu_num);
76 return -1;
77 }
79 csig->sig = cpuid_eax(0x00000001);
81 if ( (c->x86_model >= 5) || (c->x86 > 6) )
82 {
83 /* get processor flags from MSR 0x17 */
84 rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
85 csig->pf = 1 << ((val[1] >> 18) & 7);
86 }
88 wrmsr(MSR_IA32_UCODE_REV, 0, 0);
89 /* see notes above for revision 1.07. Apparent chip bug */
90 sync_core();
91 /* get the current revision from MSR 0x8B */
92 rdmsr(MSR_IA32_UCODE_REV, val[0], csig->rev);
93 pr_debug("microcode: collect_cpu_info : sig=0x%x, pf=0x%x, rev=0x%x\n",
94 csig->sig, csig->pf, csig->rev);
96 return 0;
97 }
99 static inline int microcode_update_match(
100 int cpu_num, struct microcode_header_intel *mc_header, int sig, int pf)
101 {
102 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
104 return (sigmatch(sig, uci->cpu_sig.sig, pf, uci->cpu_sig.pf) &&
105 (mc_header->rev > uci->cpu_sig.rev));
106 }
108 static int microcode_sanity_check(void *mc)
109 {
110 struct microcode_header_intel *mc_header = mc;
111 struct extended_sigtable *ext_header = NULL;
112 struct extended_signature *ext_sig;
113 unsigned long total_size, data_size, ext_table_size;
114 int sum, orig_sum, ext_sigcount = 0, i;
116 total_size = get_totalsize(mc_header);
117 data_size = get_datasize(mc_header);
118 if ( (data_size + MC_HEADER_SIZE) > total_size )
119 {
120 printk(KERN_ERR "microcode: error! "
121 "Bad data size in microcode data file\n");
122 return -EINVAL;
123 }
125 if ( (mc_header->ldrver != 1) || (mc_header->hdrver != 1) )
126 {
127 printk(KERN_ERR "microcode: error! "
128 "Unknown microcode update format\n");
129 return -EINVAL;
130 }
131 ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
132 if ( ext_table_size )
133 {
134 if ( (ext_table_size < EXT_HEADER_SIZE) ||
135 ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE) )
136 {
137 printk(KERN_ERR "microcode: error! "
138 "Small exttable size in microcode data file\n");
139 return -EINVAL;
140 }
141 ext_header = mc + MC_HEADER_SIZE + data_size;
142 if ( ext_table_size != exttable_size(ext_header) )
143 {
144 printk(KERN_ERR "microcode: error! "
145 "Bad exttable size in microcode data file\n");
146 return -EFAULT;
147 }
148 ext_sigcount = ext_header->count;
149 }
151 /* check extended table checksum */
152 if ( ext_table_size )
153 {
154 int ext_table_sum = 0;
155 int *ext_tablep = (int *)ext_header;
157 i = ext_table_size / DWSIZE;
158 while ( i-- )
159 ext_table_sum += ext_tablep[i];
160 if ( ext_table_sum )
161 {
162 printk(KERN_WARNING "microcode: aborting, "
163 "bad extended signature table checksum\n");
164 return -EINVAL;
165 }
166 }
168 /* calculate the checksum */
169 orig_sum = 0;
170 i = (MC_HEADER_SIZE + data_size) / DWSIZE;
171 while ( i-- )
172 orig_sum += ((int *)mc)[i];
173 if ( orig_sum )
174 {
175 printk(KERN_ERR "microcode: aborting, bad checksum\n");
176 return -EINVAL;
177 }
178 if ( !ext_table_size )
179 return 0;
180 /* check extended signature checksum */
181 for ( i = 0; i < ext_sigcount; i++ )
182 {
183 ext_sig = (void *)ext_header + EXT_HEADER_SIZE +
184 EXT_SIGNATURE_SIZE * i;
185 sum = orig_sum
186 - (mc_header->sig + mc_header->pf + mc_header->cksum)
187 + (ext_sig->sig + ext_sig->pf + ext_sig->cksum);
188 if ( sum )
189 {
190 printk(KERN_ERR "microcode: aborting, bad checksum\n");
191 return -EINVAL;
192 }
193 }
194 return 0;
195 }
197 /*
198 * return 0 - no update found
199 * return 1 - found update
200 * return < 0 - error
201 */
202 static int get_matching_microcode(void *mc, int cpu)
203 {
204 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
205 struct microcode_header_intel *mc_header = mc;
206 struct extended_sigtable *ext_header;
207 unsigned long total_size = get_totalsize(mc_header);
208 int ext_sigcount, i;
209 struct extended_signature *ext_sig;
210 void *new_mc;
212 if ( microcode_update_match(cpu, mc_header,
213 mc_header->sig, mc_header->pf) )
214 goto find;
216 if ( total_size <= (get_datasize(mc_header) + MC_HEADER_SIZE) )
217 return 0;
219 ext_header = mc + get_datasize(mc_header) + MC_HEADER_SIZE;
220 ext_sigcount = ext_header->count;
221 ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
222 for ( i = 0; i < ext_sigcount; i++ )
223 {
224 if ( microcode_update_match(cpu, mc_header,
225 ext_sig->sig, ext_sig->pf) )
226 goto find;
227 ext_sig++;
228 }
229 return 0;
230 find:
231 pr_debug("microcode: CPU%d found a matching microcode update with"
232 " version 0x%x (current=0x%x)\n",
233 cpu, mc_header->rev, uci->cpu_sig.rev);
234 new_mc = xmalloc_bytes(total_size);
235 if ( new_mc == NULL )
236 {
237 printk(KERN_ERR "microcode: error! Can not allocate memory\n");
238 return -ENOMEM;
239 }
241 /* free previous update file */
242 xfree(uci->mc.mc_intel);
244 memcpy(new_mc, mc, total_size);
245 uci->mc.mc_intel = new_mc;
246 return 1;
247 }
249 static int apply_microcode(int cpu)
250 {
251 unsigned long flags;
252 unsigned int val[2];
253 int cpu_num = raw_smp_processor_id();
254 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
256 /* We should bind the task to the CPU */
257 BUG_ON(cpu_num != cpu);
259 if ( uci->mc.mc_intel == NULL )
260 return -EINVAL;
262 /* serialize access to the physical write to MSR 0x79 */
263 spin_lock_irqsave(&microcode_update_lock, flags);
265 /* write microcode via MSR 0x79 */
266 wrmsr(MSR_IA32_UCODE_WRITE,
267 (unsigned long) uci->mc.mc_intel->bits,
268 (unsigned long) uci->mc.mc_intel->bits >> 16 >> 16);
269 wrmsr(MSR_IA32_UCODE_REV, 0, 0);
271 /* see notes above for revision 1.07. Apparent chip bug */
272 sync_core();
274 /* get the current revision from MSR 0x8B */
275 rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
277 spin_unlock_irqrestore(&microcode_update_lock, flags);
278 if ( val[1] != uci->mc.mc_intel->hdr.rev )
279 {
280 printk(KERN_ERR "microcode: CPU%d update from revision "
281 "0x%x to 0x%x failed\n", cpu_num, uci->cpu_sig.rev, val[1]);
282 return -EIO;
283 }
284 printk(KERN_INFO "microcode: CPU%d updated from revision "
285 "0x%x to 0x%x, date = %04x-%02x-%02x \n",
286 cpu_num, uci->cpu_sig.rev, val[1],
287 uci->mc.mc_intel->hdr.date & 0xffff,
288 uci->mc.mc_intel->hdr.date >> 24,
289 (uci->mc.mc_intel->hdr.date >> 16) & 0xff);
290 uci->cpu_sig.rev = val[1];
292 return 0;
293 }
295 static long get_next_ucode_from_buffer(void **mc, const u8 *buf,
296 unsigned long size, long offset)
297 {
298 struct microcode_header_intel *mc_header;
299 unsigned long total_size;
301 /* No more data */
302 if ( offset >= size )
303 return 0;
304 mc_header = (struct microcode_header_intel *)(buf + offset);
305 total_size = get_totalsize(mc_header);
307 if ( (offset + total_size) > size )
308 {
309 printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
310 return -EINVAL;
311 }
313 *mc = xmalloc_bytes(total_size);
314 if ( *mc == NULL )
315 {
316 printk(KERN_ERR "microcode: error! Can not allocate memory\n");
317 return -ENOMEM;
318 }
319 memcpy(*mc, (const void *)(buf + offset), total_size);
320 return offset + total_size;
321 }
323 static int cpu_request_microcode(int cpu, const void *buf, size_t size)
324 {
325 long offset = 0;
326 int error = 0;
327 void *mc;
328 unsigned int matching_count = 0;
330 /* We should bind the task to the CPU */
331 BUG_ON(cpu != raw_smp_processor_id());
333 while ( (offset = get_next_ucode_from_buffer(&mc, buf, size, offset)) > 0 )
334 {
335 error = microcode_sanity_check(mc);
336 if ( error )
337 break;
338 error = get_matching_microcode(mc, cpu);
339 if ( error < 0 )
340 break;
341 /*
342 * It's possible the data file has multiple matching ucode,
343 * lets keep searching till the latest version
344 */
345 if ( error == 1 )
346 {
347 matching_count++;
348 error = 0;
349 }
350 xfree(mc);
351 }
352 if ( offset > 0 )
353 xfree(mc);
354 if ( offset < 0 )
355 error = offset;
357 if ( !error && matching_count )
358 apply_microcode(cpu);
360 return error;
361 }
363 static int microcode_resume_match(int cpu, struct cpu_signature *nsig)
364 {
365 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
367 return (sigmatch(nsig->sig, uci->cpu_sig.sig, nsig->pf, uci->cpu_sig.pf) &&
368 (uci->cpu_sig.rev > nsig->rev));
369 }
371 static struct microcode_ops microcode_intel_ops = {
372 .microcode_resume_match = microcode_resume_match,
373 .cpu_request_microcode = cpu_request_microcode,
374 .collect_cpu_info = collect_cpu_info,
375 .apply_microcode = apply_microcode,
376 };
378 static __init int microcode_init_intel(void)
379 {
380 if ( boot_cpu_data.x86_vendor == X86_VENDOR_INTEL )
381 microcode_ops = &microcode_intel_ops;
382 return 0;
383 }
384 __initcall(microcode_init_intel);