2 * Intel CPU Microcode Update Driver for Linux
4 * Copyright (C) 2000-2004 Tigran Aivazian
5 * 2006 Shaohua Li <shaohua.li@intel.com>
7 * This driver allows to upgrade microcode on Intel processors
8 * belonging to IA-32 family - PentiumPro, Pentium II,
9 * Pentium III, Xeon, Pentium 4, etc.
11 * Reference: Section 8.10 of Volume III, Intel Pentium 4 Manual,
12 * Order Number 245472 or free download from:
14 * http://developer.intel.com/design/pentium4/manuals/245472.htm
16 * For more information, go to http://www.urbanmyth.org/microcode
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.
23 * 1.0 16 Feb 2000, Tigran Aivazian <tigran@sco.com>
25 * 1.01 18 Feb 2000, Tigran Aivazian <tigran@sco.com>
26 * Added read() support + cleanups.
27 * 1.02 21 Feb 2000, Tigran Aivazian <tigran@sco.com>
28 * Added 'device trimming' support. open(O_WRONLY) zeroes
29 * and frees the saved copy of applied microcode.
30 * 1.03 29 Feb 2000, Tigran Aivazian <tigran@sco.com>
31 * Made to use devfs (/dev/cpu/microcode) + cleanups.
32 * 1.04 06 Jun 2000, Simon Trimmer <simon@veritas.com>
33 * Added misc device support (now uses both devfs and misc).
34 * Added MICROCODE_IOCFREE ioctl to clear memory.
35 * 1.05 09 Jun 2000, Simon Trimmer <simon@veritas.com>
36 * Messages for error cases (non Intel & no suitable microcode).
37 * 1.06 03 Aug 2000, Tigran Aivazian <tigran@veritas.com>
38 * Removed ->release(). Removed exclusive open and status bitmap.
39 * Added microcode_rwsem to serialize read()/write()/ioctl().
40 * Removed global kernel lock usage.
41 * 1.07 07 Sep 2000, Tigran Aivazian <tigran@veritas.com>
42 * Write 0 to 0x8B msr and then cpuid before reading revision,
43 * so that it works even if there were no update done by the
44 * BIOS. Otherwise, reading from 0x8B gives junk (which happened
45 * to be 0 on my machine which is why it worked even when I
46 * disabled update by the BIOS)
47 * Thanks to Eric W. Biederman <ebiederman@lnxi.com> for the fix.
48 * 1.08 11 Dec 2000, Richard Schaal <richard.schaal@intel.com> and
49 * Tigran Aivazian <tigran@veritas.com>
50 * Intel Pentium 4 processor support and bugfixes.
51 * 1.09 30 Oct 2001, Tigran Aivazian <tigran@veritas.com>
52 * Bugfix for HT (Hyper-Threading) enabled processors
53 * whereby processor resources are shared by all logical processors
54 * in a single CPU package.
55 * 1.10 28 Feb 2002 Asit K Mallick <asit.k.mallick@intel.com> and
56 * Tigran Aivazian <tigran@veritas.com>,
57 * Serialize updates as required on HT processors due to speculative
58 * nature of implementation.
59 * 1.11 22 Mar 2002 Tigran Aivazian <tigran@veritas.com>
60 * Fix the panic when writing zero-length microcode chunk.
61 * 1.12 29 Sep 2003 Nitin Kamble <nitin.a.kamble@intel.com>,
62 * Jun Nakajima <jun.nakajima@intel.com>
63 * Support for the microcode updates in the new format.
64 * 1.13 10 Oct 2003 Tigran Aivazian <tigran@veritas.com>
65 * Removed ->read() method and obsoleted MICROCODE_IOCFREE ioctl
66 * because we no longer hold a copy of applied microcode
68 * 1.14 25 Jun 2004 Tigran Aivazian <tigran@veritas.com>
69 * Fix sigmatch() macro to handle old CPUs with pf == 0.
70 * Thanks to Stuart Swales for pointing out this bug.
73 //#define DEBUG /* pr_debug */
74 #include <linux/capability.h>
75 #include <linux/kernel.h>
76 #include <linux/init.h>
77 #include <linux/sched.h>
78 #include <linux/cpumask.h>
79 #include <linux/module.h>
80 #include <linux/slab.h>
81 #include <linux/vmalloc.h>
82 #include <linux/miscdevice.h>
83 #include <linux/spinlock.h>
85 #include <linux/mutex.h>
88 #include <asm/uaccess.h>
89 #include <asm/processor.h>
91 MODULE_DESCRIPTION("Intel CPU (IA-32) Microcode Update Driver");
92 MODULE_AUTHOR("Tigran Aivazian <tigran@veritas.com>");
93 MODULE_LICENSE("GPL");
95 #define MICROCODE_VERSION "1.14a"
97 #define DEFAULT_UCODE_DATASIZE (2000) /* 2000 bytes */
98 #define MC_HEADER_SIZE (sizeof (microcode_header_t)) /* 48 bytes */
99 #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE) /* 2048 bytes */
100 #define EXT_HEADER_SIZE (sizeof (struct extended_sigtable)) /* 20 bytes */
101 #define EXT_SIGNATURE_SIZE (sizeof (struct extended_signature)) /* 12 bytes */
102 #define DWSIZE (sizeof (u32))
103 #define get_totalsize(mc) \
104 (((microcode_t *)mc)->hdr.totalsize ? \
105 ((microcode_t *)mc)->hdr.totalsize : DEFAULT_UCODE_TOTALSIZE)
106 #define get_datasize(mc) \
107 (((microcode_t *)mc)->hdr.datasize ? \
108 ((microcode_t *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE)
110 #define sigmatch(s1, s2, p1, p2) \
111 (((s1) == (s2)) && (((p1) & (p2)) || (((p1) == 0) && ((p2) == 0))))
113 #define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE)
115 /* serialize access to the physical write to MSR 0x79 */
116 static DEFINE_SPINLOCK(microcode_update_lock);
118 /* no concurrent ->write()s are allowed on /dev/cpu/microcode */
119 static DEFINE_MUTEX(microcode_mutex);
121 static struct ucode_cpu_info {
127 } ucode_cpu_info[NR_CPUS];
129 static void collect_cpu_info(int cpu_num)
131 struct cpuinfo_x86 *c = cpu_data + cpu_num;
132 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
135 /* We should bind the task to the CPU */
136 BUG_ON(raw_smp_processor_id() != cpu_num);
137 uci->pf = uci->rev = 0;
141 if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
142 cpu_has(c, X86_FEATURE_IA64)) {
143 printk(KERN_ERR "microcode: CPU%d not a capable Intel "
144 "processor\n", cpu_num);
149 uci->sig = cpuid_eax(0x00000001);
151 if ((c->x86_model >= 5) || (c->x86 > 6)) {
152 /* get processor flags from MSR 0x17 */
153 rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
154 uci->pf = 1 << ((val[1] >> 18) & 7);
157 wrmsr(MSR_IA32_UCODE_REV, 0, 0);
158 /* see notes above for revision 1.07. Apparent chip bug */
160 /* get the current revision from MSR 0x8B */
161 rdmsr(MSR_IA32_UCODE_REV, val[0], uci->rev);
162 pr_debug("microcode: collect_cpu_info : sig=0x%x, pf=0x%x, rev=0x%x\n",
163 uci->sig, uci->pf, uci->rev);
166 static inline int microcode_update_match(int cpu_num,
167 microcode_header_t *mc_header, int sig, int pf)
169 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
171 if (!sigmatch(sig, uci->sig, pf, uci->pf)
172 || mc_header->rev <= uci->rev)
177 static int microcode_sanity_check(void *mc)
179 microcode_header_t *mc_header = mc;
180 struct extended_sigtable *ext_header = NULL;
181 struct extended_signature *ext_sig;
182 unsigned long total_size, data_size, ext_table_size;
183 int sum, orig_sum, ext_sigcount = 0, i;
185 total_size = get_totalsize(mc_header);
186 data_size = get_datasize(mc_header);
187 if ((data_size + MC_HEADER_SIZE > total_size)
188 || (data_size < DEFAULT_UCODE_DATASIZE)) {
189 printk(KERN_ERR "microcode: error! "
190 "Bad data size in microcode data file\n");
194 if (mc_header->ldrver != 1 || mc_header->hdrver != 1) {
195 printk(KERN_ERR "microcode: error! "
196 "Unknown microcode update format\n");
199 ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
200 if (ext_table_size) {
201 if ((ext_table_size < EXT_HEADER_SIZE)
202 || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
203 printk(KERN_ERR "microcode: error! "
204 "Small exttable size in microcode data file\n");
207 ext_header = mc + MC_HEADER_SIZE + data_size;
208 if (ext_table_size != exttable_size(ext_header)) {
209 printk(KERN_ERR "microcode: error! "
210 "Bad exttable size in microcode data file\n");
213 ext_sigcount = ext_header->count;
216 /* check extended table checksum */
217 if (ext_table_size) {
218 int ext_table_sum = 0;
219 int * ext_tablep = (int *)ext_header;
221 i = ext_table_size / DWSIZE;
223 ext_table_sum += ext_tablep[i];
225 printk(KERN_WARNING "microcode: aborting, "
226 "bad extended signature table checksum\n");
231 /* calculate the checksum */
233 i = (MC_HEADER_SIZE + data_size) / DWSIZE;
235 orig_sum += ((int *)mc)[i];
237 printk(KERN_ERR "microcode: aborting, bad checksum\n");
242 /* check extended signature checksum */
243 for (i = 0; i < ext_sigcount; i++) {
244 ext_sig = (struct extended_signature *)((void *)ext_header
245 + EXT_HEADER_SIZE + EXT_SIGNATURE_SIZE * i);
247 - (mc_header->sig + mc_header->pf + mc_header->cksum)
248 + (ext_sig->sig + ext_sig->pf + ext_sig->cksum);
250 printk(KERN_ERR "microcode: aborting, bad checksum\n");
258 * return 0 - no update found
259 * return 1 - found update
262 static int get_maching_microcode(void *mc, int cpu)
264 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
265 microcode_header_t *mc_header = mc;
266 struct extended_sigtable *ext_header;
267 unsigned long total_size = get_totalsize(mc_header);
269 struct extended_signature *ext_sig;
272 if (microcode_update_match(cpu, mc_header,
273 mc_header->sig, mc_header->pf))
276 if (total_size <= get_datasize(mc_header) + MC_HEADER_SIZE)
279 ext_header = (struct extended_sigtable *)(mc +
280 get_datasize(mc_header) + MC_HEADER_SIZE);
281 ext_sigcount = ext_header->count;
282 ext_sig = (struct extended_signature *)((void *)ext_header
284 for (i = 0; i < ext_sigcount; i++) {
285 if (microcode_update_match(cpu, mc_header,
286 ext_sig->sig, ext_sig->pf))
292 pr_debug("microcode: CPU %d found a matching microcode update with"
293 " version 0x%x (current=0x%x)\n", cpu, mc_header->rev,uci->rev);
294 new_mc = vmalloc(total_size);
296 printk(KERN_ERR "microcode: error! Can not allocate memory\n");
300 /* free previous update file */
303 memcpy(new_mc, mc, total_size);
308 static void apply_microcode(int cpu)
312 int cpu_num = raw_smp_processor_id();
313 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
315 /* We should bind the task to the CPU */
316 BUG_ON(cpu_num != cpu);
321 /* serialize access to the physical write to MSR 0x79 */
322 spin_lock_irqsave(µcode_update_lock, flags);
324 /* write microcode via MSR 0x79 */
325 wrmsr(MSR_IA32_UCODE_WRITE,
326 (unsigned long) uci->mc->bits,
327 (unsigned long) uci->mc->bits >> 16 >> 16);
328 wrmsr(MSR_IA32_UCODE_REV, 0, 0);
330 /* see notes above for revision 1.07. Apparent chip bug */
333 /* get the current revision from MSR 0x8B */
334 rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
336 spin_unlock_irqrestore(µcode_update_lock, flags);
337 if (val[1] != uci->mc->hdr.rev) {
338 printk(KERN_ERR "microcode: CPU%d updated from revision "
339 "0x%x to 0x%x failed\n", cpu_num, uci->rev, val[1]);
342 pr_debug("microcode: CPU%d updated from revision "
343 "0x%x to 0x%x, date = %08x \n",
344 cpu_num, uci->rev, val[1], uci->mc->hdr.date);
348 #ifdef CONFIG_MICROCODE_OLD_INTERFACE
349 static void __user *user_buffer; /* user area microcode data buffer */
350 static unsigned int user_buffer_size; /* it's size */
352 static long get_next_ucode(void **mc, long offset)
354 microcode_header_t mc_header;
355 unsigned long total_size;
358 if (offset >= user_buffer_size)
360 if (copy_from_user(&mc_header, user_buffer + offset, MC_HEADER_SIZE)) {
361 printk(KERN_ERR "microcode: error! Can not read user data\n");
364 total_size = get_totalsize(&mc_header);
365 if ((offset + total_size > user_buffer_size)
366 || (total_size < DEFAULT_UCODE_TOTALSIZE)) {
367 printk(KERN_ERR "microcode: error! Bad total size in microcode "
371 *mc = vmalloc(total_size);
374 if (copy_from_user(*mc, user_buffer + offset, total_size)) {
375 printk(KERN_ERR "microcode: error! Can not read user data\n");
379 return offset + total_size;
382 static int do_microcode_update (void)
390 old = current->cpus_allowed;
392 while ((cursor = get_next_ucode(&new_mc, cursor)) > 0) {
393 error = microcode_sanity_check(new_mc);
397 * It's possible the data file has multiple matching ucode,
398 * lets keep searching till the latest version
400 for_each_online_cpu(cpu) {
401 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
405 set_cpus_allowed(current, cpumask_of_cpu(cpu));
406 error = get_maching_microcode(new_mc, cpu);
410 apply_microcode(cpu);
419 set_cpus_allowed(current, old);
423 static int microcode_open (struct inode *unused1, struct file *unused2)
425 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
428 static ssize_t microcode_write (struct file *file, const char __user *buf, size_t len, loff_t *ppos)
432 if (len < DEFAULT_UCODE_TOTALSIZE) {
433 printk(KERN_ERR "microcode: not enough data\n");
437 if ((len >> PAGE_SHIFT) > num_physpages) {
438 printk(KERN_ERR "microcode: too much data (max %ld pages)\n", num_physpages);
443 mutex_lock(µcode_mutex);
445 user_buffer = (void __user *) buf;
446 user_buffer_size = (int) len;
448 ret = do_microcode_update();
452 mutex_unlock(µcode_mutex);
453 unlock_cpu_hotplug();
458 static struct file_operations microcode_fops = {
459 .owner = THIS_MODULE,
460 .write = microcode_write,
461 .open = microcode_open,
464 static struct miscdevice microcode_dev = {
465 .minor = MICROCODE_MINOR,
467 .fops = µcode_fops,
470 static int __init microcode_dev_init (void)
474 error = misc_register(µcode_dev);
477 "microcode: can't misc_register on minor=%d\n",
485 static void __exit microcode_dev_exit (void)
487 misc_deregister(µcode_dev);
490 MODULE_ALIAS_MISCDEV(MICROCODE_MINOR);
492 #define microcode_dev_init() 0
493 #define microcode_dev_exit() do { } while(0)
496 static int __init microcode_init (void)
500 error = microcode_dev_init();
505 "IA-32 Microcode Update Driver: v" MICROCODE_VERSION " <tigran@veritas.com>\n");
509 static void __exit microcode_exit (void)
511 microcode_dev_exit();
514 module_init(microcode_init)
515 module_exit(microcode_exit)