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 */
23
24 #include <xen/lib.h>
25 #include <xen/kernel.h>
26 #include <xen/init.h>
27 #include <xen/sched.h>
28 #include <xen/smp.h>
29 #include <xen/spinlock.h>
30
31 #include <asm/msr.h>
32 #include <asm/processor.h>
33 #include <asm/microcode.h>
34
35 #define pr_debug(x...) ((void)0)
36
37 struct microcode_header_intel {
38 unsigned int hdrver;
39 unsigned int rev;
40 unsigned int date;
41 unsigned int sig;
42 unsigned int cksum;
43 unsigned int ldrver;
44 unsigned int pf;
45 unsigned int datasize;
46 unsigned int totalsize;
47 unsigned int reserved[3];
48 };
49
50 struct microcode_intel {
51 struct microcode_header_intel hdr;
52 unsigned int bits[0];
53 };
54
55 /* microcode format is extended from prescott processors */
56 struct extended_signature {
57 unsigned int sig;
58 unsigned int pf;
59 unsigned int cksum;
60 };
61
62 struct extended_sigtable {
63 unsigned int count;
64 unsigned int cksum;
65 unsigned int reserved[3];
66 struct extended_signature sigs[0];
67 };
68
69 #define DEFAULT_UCODE_DATASIZE (2000)
70 #define MC_HEADER_SIZE (sizeof(struct microcode_header_intel))
71 #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE)
72 #define EXT_HEADER_SIZE (sizeof(struct extended_sigtable))
73 #define EXT_SIGNATURE_SIZE (sizeof(struct extended_signature))
74 #define DWSIZE (sizeof(u32))
75 #define get_totalsize(mc) \
76 (((struct microcode_intel *)mc)->hdr.totalsize ? \
77 ((struct microcode_intel *)mc)->hdr.totalsize : \
78 DEFAULT_UCODE_TOTALSIZE)
79
80 #define get_datasize(mc) \
81 (((struct microcode_intel *)mc)->hdr.datasize ? \
82 ((struct microcode_intel *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE)
83
84 #define sigmatch(s1, s2, p1, p2) \
85 (((s1) == (s2)) && (((p1) & (p2)) || (((p1) == 0) && ((p2) == 0))))
86
87 #define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE)
88
89 /* serialize access to the physical write to MSR 0x79 */
90 static DEFINE_SPINLOCK(microcode_update_lock);
91
collect_cpu_info(unsigned int cpu_num,struct cpu_signature * csig)92 static int collect_cpu_info(unsigned int cpu_num, struct cpu_signature *csig)
93 {
94 struct cpuinfo_x86 *c = &cpu_data[cpu_num];
95 uint64_t msr_content;
96
97 BUG_ON(cpu_num != smp_processor_id());
98
99 memset(csig, 0, sizeof(*csig));
100
101 if ( (c->x86_vendor != X86_VENDOR_INTEL) || (c->x86 < 6) )
102 {
103 printk(KERN_ERR "microcode: CPU%d not a capable Intel "
104 "processor\n", cpu_num);
105 return -1;
106 }
107
108 csig->sig = cpuid_eax(0x00000001);
109
110 if ( (c->x86_model >= 5) || (c->x86 > 6) )
111 {
112 /* get processor flags from MSR 0x17 */
113 rdmsrl(MSR_IA32_PLATFORM_ID, msr_content);
114 csig->pf = 1 << ((msr_content >> 50) & 7);
115 }
116
117 wrmsrl(MSR_IA32_UCODE_REV, 0x0ULL);
118 /* As documented in the SDM: Do a CPUID 1 here */
119 cpuid_eax(1);
120
121 /* get the current revision from MSR 0x8B */
122 rdmsrl(MSR_IA32_UCODE_REV, msr_content);
123 csig->rev = (uint32_t)(msr_content >> 32);
124 pr_debug("microcode: collect_cpu_info : sig=%#x, pf=%#x, rev=%#x\n",
125 csig->sig, csig->pf, csig->rev);
126
127 return 0;
128 }
129
microcode_update_match(unsigned int cpu_num,const struct microcode_header_intel * mc_header,int sig,int pf)130 static inline int microcode_update_match(
131 unsigned int cpu_num, const struct microcode_header_intel *mc_header,
132 int sig, int pf)
133 {
134 struct ucode_cpu_info *uci = &per_cpu(ucode_cpu_info, cpu_num);
135
136 return (sigmatch(sig, uci->cpu_sig.sig, pf, uci->cpu_sig.pf) &&
137 (mc_header->rev > uci->cpu_sig.rev));
138 }
139
microcode_sanity_check(void * mc)140 static int microcode_sanity_check(void *mc)
141 {
142 struct microcode_header_intel *mc_header = mc;
143 struct extended_sigtable *ext_header = NULL;
144 struct extended_signature *ext_sig;
145 unsigned long total_size, data_size, ext_table_size;
146 unsigned int ext_sigcount = 0, i;
147 uint32_t sum, orig_sum;
148
149 total_size = get_totalsize(mc_header);
150 data_size = get_datasize(mc_header);
151 if ( (data_size + MC_HEADER_SIZE) > total_size )
152 {
153 printk(KERN_ERR "microcode: error! "
154 "Bad data size in microcode data file\n");
155 return -EINVAL;
156 }
157
158 if ( (mc_header->ldrver != 1) || (mc_header->hdrver != 1) )
159 {
160 printk(KERN_ERR "microcode: error! "
161 "Unknown microcode update format\n");
162 return -EINVAL;
163 }
164 ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
165 if ( ext_table_size )
166 {
167 if ( (ext_table_size < EXT_HEADER_SIZE) ||
168 ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE) )
169 {
170 printk(KERN_ERR "microcode: error! "
171 "Small exttable size in microcode data file\n");
172 return -EINVAL;
173 }
174 ext_header = mc + MC_HEADER_SIZE + data_size;
175 if ( ext_table_size != exttable_size(ext_header) )
176 {
177 printk(KERN_ERR "microcode: error! "
178 "Bad exttable size in microcode data file\n");
179 return -EFAULT;
180 }
181 ext_sigcount = ext_header->count;
182 }
183
184 /* check extended table checksum */
185 if ( ext_table_size )
186 {
187 uint32_t ext_table_sum = 0;
188 uint32_t *ext_tablep = (uint32_t *)ext_header;
189
190 i = ext_table_size / DWSIZE;
191 while ( i-- )
192 ext_table_sum += ext_tablep[i];
193 if ( ext_table_sum )
194 {
195 printk(KERN_WARNING "microcode: aborting, "
196 "bad extended signature table checksum\n");
197 return -EINVAL;
198 }
199 }
200
201 /* calculate the checksum */
202 orig_sum = 0;
203 i = (MC_HEADER_SIZE + data_size) / DWSIZE;
204 while ( i-- )
205 orig_sum += ((uint32_t *)mc)[i];
206 if ( orig_sum )
207 {
208 printk(KERN_ERR "microcode: aborting, bad checksum\n");
209 return -EINVAL;
210 }
211 if ( !ext_table_size )
212 return 0;
213 /* check extended signature checksum */
214 for ( i = 0; i < ext_sigcount; i++ )
215 {
216 ext_sig = (void *)ext_header + EXT_HEADER_SIZE +
217 EXT_SIGNATURE_SIZE * i;
218 sum = orig_sum
219 - (mc_header->sig + mc_header->pf + mc_header->cksum)
220 + (ext_sig->sig + ext_sig->pf + ext_sig->cksum);
221 if ( sum )
222 {
223 printk(KERN_ERR "microcode: aborting, bad checksum\n");
224 return -EINVAL;
225 }
226 }
227 return 0;
228 }
229
230 /*
231 * return 0 - no update found
232 * return 1 - found update
233 * return < 0 - error
234 */
get_matching_microcode(const void * mc,unsigned int cpu)235 static int get_matching_microcode(const void *mc, unsigned int cpu)
236 {
237 struct ucode_cpu_info *uci = &per_cpu(ucode_cpu_info, cpu);
238 const struct microcode_header_intel *mc_header = mc;
239 const struct extended_sigtable *ext_header;
240 unsigned long total_size = get_totalsize(mc_header);
241 int ext_sigcount, i;
242 struct extended_signature *ext_sig;
243 void *new_mc;
244
245 if ( microcode_update_match(cpu, mc_header,
246 mc_header->sig, mc_header->pf) )
247 goto find;
248
249 if ( total_size <= (get_datasize(mc_header) + MC_HEADER_SIZE) )
250 return 0;
251
252 ext_header = mc + get_datasize(mc_header) + MC_HEADER_SIZE;
253 ext_sigcount = ext_header->count;
254 ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
255 for ( i = 0; i < ext_sigcount; i++ )
256 {
257 if ( microcode_update_match(cpu, mc_header,
258 ext_sig->sig, ext_sig->pf) )
259 goto find;
260 ext_sig++;
261 }
262 return 0;
263 find:
264 pr_debug("microcode: CPU%d found a matching microcode update with"
265 " version %#x (current=%#x)\n",
266 cpu, mc_header->rev, uci->cpu_sig.rev);
267 new_mc = xmalloc_bytes(total_size);
268 if ( new_mc == NULL )
269 {
270 printk(KERN_ERR "microcode: error! Can not allocate memory\n");
271 return -ENOMEM;
272 }
273
274 memcpy(new_mc, mc, total_size);
275 xfree(uci->mc.mc_intel);
276 uci->mc.mc_intel = new_mc;
277 return 1;
278 }
279
apply_microcode(unsigned int cpu)280 static int apply_microcode(unsigned int cpu)
281 {
282 unsigned long flags;
283 uint64_t msr_content;
284 unsigned int val[2];
285 unsigned int cpu_num = raw_smp_processor_id();
286 struct ucode_cpu_info *uci = &per_cpu(ucode_cpu_info, cpu_num);
287
288 /* We should bind the task to the CPU */
289 BUG_ON(cpu_num != cpu);
290
291 if ( uci->mc.mc_intel == NULL )
292 return -EINVAL;
293
294 /* serialize access to the physical write to MSR 0x79 */
295 spin_lock_irqsave(µcode_update_lock, flags);
296
297 /* write microcode via MSR 0x79 */
298 wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)uci->mc.mc_intel->bits);
299 wrmsrl(MSR_IA32_UCODE_REV, 0x0ULL);
300
301 /* As documented in the SDM: Do a CPUID 1 here */
302 cpuid_eax(1);
303
304 /* get the current revision from MSR 0x8B */
305 rdmsrl(MSR_IA32_UCODE_REV, msr_content);
306 val[1] = (uint32_t)(msr_content >> 32);
307
308 spin_unlock_irqrestore(µcode_update_lock, flags);
309 if ( val[1] != uci->mc.mc_intel->hdr.rev )
310 {
311 printk(KERN_ERR "microcode: CPU%d update from revision "
312 "%#x to %#x failed\n", cpu_num, uci->cpu_sig.rev, val[1]);
313 return -EIO;
314 }
315 printk(KERN_INFO "microcode: CPU%d updated from revision "
316 "%#x to %#x, date = %04x-%02x-%02x \n",
317 cpu_num, uci->cpu_sig.rev, val[1],
318 uci->mc.mc_intel->hdr.date & 0xffff,
319 uci->mc.mc_intel->hdr.date >> 24,
320 (uci->mc.mc_intel->hdr.date >> 16) & 0xff);
321 uci->cpu_sig.rev = val[1];
322
323 return 0;
324 }
325
get_next_ucode_from_buffer(void ** mc,const u8 * buf,unsigned long size,long offset)326 static long get_next_ucode_from_buffer(void **mc, const u8 *buf,
327 unsigned long size, long offset)
328 {
329 struct microcode_header_intel *mc_header;
330 unsigned long total_size;
331
332 /* No more data */
333 if ( offset >= size )
334 return 0;
335 mc_header = (struct microcode_header_intel *)(buf + offset);
336 total_size = get_totalsize(mc_header);
337
338 if ( (offset + total_size) > size )
339 {
340 printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
341 return -EINVAL;
342 }
343
344 *mc = xmalloc_bytes(total_size);
345 if ( *mc == NULL )
346 {
347 printk(KERN_ERR "microcode: error! Can not allocate memory\n");
348 return -ENOMEM;
349 }
350 memcpy(*mc, (const void *)(buf + offset), total_size);
351 return offset + total_size;
352 }
353
cpu_request_microcode(unsigned int cpu,const void * buf,size_t size)354 static int cpu_request_microcode(unsigned int cpu, const void *buf,
355 size_t size)
356 {
357 long offset = 0;
358 int error = 0;
359 void *mc;
360 unsigned int matching_count = 0;
361
362 /* We should bind the task to the CPU */
363 BUG_ON(cpu != raw_smp_processor_id());
364
365 while ( (offset = get_next_ucode_from_buffer(&mc, buf, size, offset)) > 0 )
366 {
367 error = microcode_sanity_check(mc);
368 if ( error )
369 break;
370 error = get_matching_microcode(mc, cpu);
371 if ( error < 0 )
372 break;
373 /*
374 * It's possible the data file has multiple matching ucode,
375 * lets keep searching till the latest version
376 */
377 if ( error == 1 )
378 {
379 matching_count++;
380 error = 0;
381 }
382 xfree(mc);
383 }
384 if ( offset > 0 )
385 xfree(mc);
386 if ( offset < 0 )
387 error = offset;
388
389 if ( !error && matching_count )
390 apply_microcode(cpu);
391
392 return error;
393 }
394
microcode_resume_match(unsigned int cpu,const void * mc)395 static int microcode_resume_match(unsigned int cpu, const void *mc)
396 {
397 return get_matching_microcode(mc, cpu);
398 }
399
400 static const struct microcode_ops microcode_intel_ops = {
401 .microcode_resume_match = microcode_resume_match,
402 .cpu_request_microcode = cpu_request_microcode,
403 .collect_cpu_info = collect_cpu_info,
404 .apply_microcode = apply_microcode,
405 };
406
microcode_init_intel(void)407 int __init microcode_init_intel(void)
408 {
409 if ( boot_cpu_data.x86_vendor == X86_VENDOR_INTEL )
410 microcode_ops = µcode_intel_ops;
411 return 0;
412 }
413