2 * Intel CPU Microcode Update driver for Linux
4 * Copyright (C) 2000 Tigran Aivazian
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.
10 * Reference: Section 8.10 of Volume III, Intel Pentium 4 Manual,
11 * Order Number 245472 or free download from:
13 * http://developer.intel.com/design/pentium4/manuals/245472.htm
15 * For more information, go to http://www.urbanmyth.org/microcode
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * as published by the Free Software Foundation; either version
20 * 2 of the License, or (at your option) any later version.
22 * 1.0 16 Feb 2000, Tigran Aivazian <tigran@sco.com>
24 * 1.01 18 Feb 2000, Tigran Aivazian <tigran@sco.com>
25 * Added read() support + cleanups.
26 * 1.02 21 Feb 2000, Tigran Aivazian <tigran@sco.com>
27 * Added 'device trimming' support. open(O_WRONLY) zeroes
28 * and frees the saved copy of applied microcode.
29 * 1.03 29 Feb 2000, Tigran Aivazian <tigran@sco.com>
30 * Made to use devfs (/dev/cpu/microcode) + cleanups.
31 * 1.04 06 Jun 2000, Simon Trimmer <simon@veritas.com>
32 * Added misc device support (now uses both devfs and misc).
33 * Added MICROCODE_IOCFREE ioctl to clear memory.
34 * 1.05 09 Jun 2000, Simon Trimmer <simon@veritas.com>
35 * Messages for error cases (non intel & no suitable microcode).
36 * 1.06 03 Aug 2000, Tigran Aivazian <tigran@veritas.com>
37 * Removed ->release(). Removed exclusive open and status bitmap.
38 * Added microcode_rwsem to serialize read()/write()/ioctl().
39 * Removed global kernel lock usage.
40 * 1.07 07 Sep 2000, Tigran Aivazian <tigran@veritas.com>
41 * Write 0 to 0x8B msr and then cpuid before reading revision,
42 * so that it works even if there were no update done by the
43 * BIOS. Otherwise, reading from 0x8B gives junk (which happened
44 * to be 0 on my machine which is why it worked even when I
45 * disabled update by the BIOS)
46 * Thanks to Eric W. Biederman <ebiederman@lnxi.com> for the fix.
47 * 1.08 11 Dec 2000, Richard Schaal <richard.schaal@intel.com> and
48 * Tigran Aivazian <tigran@veritas.com>
49 * Intel Pentium 4 processor support and bugfixes.
50 * 1.09 30 Oct 2001, Tigran Aivazian <tigran@veritas.com>
51 * Bugfix for HT (Hyper-Threading) enabled processors
52 * whereby processor resources are shared by all logical processors
53 * in a single CPU package.
54 * 1.10 28 Feb 2002 Asit K Mallick <asit.k.mallick@intel.com> and
55 * Tigran Aivazian <tigran@veritas.com>,
56 * Serialize updates as required on HT processors due to speculative
57 * nature of implementation.
58 * 1.11 22 Mar 2001 Tigran Aivazian <tigran@veritas.com>
59 * Fix the panic when writing zero-length microcode chunk.
62 #include <linux/init.h>
63 #include <linux/sched.h>
64 #include <linux/module.h>
65 #include <linux/slab.h>
66 #include <linux/vmalloc.h>
67 #include <linux/miscdevice.h>
68 #include <linux/devfs_fs_kernel.h>
69 #include <linux/spinlock.h>
73 #include <asm/uaccess.h>
74 #include <asm/processor.h>
77 static spinlock_t microcode_update_lock
= SPIN_LOCK_UNLOCKED
;
79 #define MICROCODE_VERSION "1.11"
81 MODULE_DESCRIPTION("Intel CPU (IA-32) microcode update driver");
82 MODULE_AUTHOR("Tigran Aivazian <tigran@veritas.com>");
83 MODULE_LICENSE("GPL");
88 #define printf(x...) printk(##x)
94 static int microcode_open(struct inode
*, struct file
*);
95 static ssize_t
microcode_read(struct file
*, char *, size_t, loff_t
*);
96 static ssize_t
microcode_write(struct file
*, const char *, size_t, loff_t
*);
97 static int microcode_ioctl(struct inode
*, struct file
*, unsigned int, unsigned long);
99 static int do_microcode_update(void);
100 static void do_update_one(void *);
102 /* read()/write()/ioctl() are serialized on this */
103 static DECLARE_RWSEM(microcode_rwsem
);
105 static struct microcode
*microcode
; /* array of 2048byte microcode blocks */
106 static unsigned int microcode_num
; /* number of chunks in microcode */
107 static char *mc_applied
; /* array of applied microcode blocks */
108 static unsigned int mc_fsize
; /* file size of /dev/cpu/microcode */
110 /* we share file_operations between misc and devfs mechanisms */
111 static struct file_operations microcode_fops
= {
112 .owner
= THIS_MODULE
,
113 .read
= microcode_read
,
114 .write
= microcode_write
,
115 .ioctl
= microcode_ioctl
,
116 .open
= microcode_open
,
119 static struct miscdevice microcode_dev
= {
120 .minor
= MICROCODE_MINOR
,
122 .fops
= µcode_fops
,
125 static devfs_handle_t devfs_handle
;
127 static int __init
microcode_init(void)
131 error
= misc_register(µcode_dev
);
134 "microcode: can't misc_register on minor=%d\n",
137 devfs_handle
= devfs_register(NULL
, "cpu/microcode",
138 DEVFS_FL_DEFAULT
, 0, 0, S_IFREG
| S_IRUSR
| S_IWUSR
,
139 µcode_fops
, NULL
);
140 if (devfs_handle
== NULL
&& error
) {
141 printk(KERN_ERR
"microcode: failed to devfs_register()\n");
146 "IA-32 Microcode Update Driver: v%s <tigran@veritas.com>\n",
153 static void __exit
microcode_exit(void)
155 misc_deregister(µcode_dev
);
156 devfs_unregister(devfs_handle
);
159 printk(KERN_INFO
"IA-32 Microcode Update Driver v%s unregistered\n",
163 module_init(microcode_init
)
164 module_exit(microcode_exit
)
166 static int microcode_open(struct inode
*unused1
, struct file
*unused2
)
168 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
172 * update_req[cpu].err is set to 1 if update failed on 'cpu', 0 otherwise
173 * if err==0, microcode[update_req[cpu].slot] points to applied block of microcode
178 } update_req
[NR_CPUS
];
180 static int do_microcode_update(void)
182 int i
, error
= 0, err
;
185 if (smp_call_function(do_update_one
, NULL
, 1, 1) != 0) {
186 printk(KERN_ERR
"microcode: IPI timeout, giving up\n");
191 for (i
=0; i
<NR_CPUS
; i
++) {
192 err
= update_req
[i
].err
;
195 m
= (struct microcode
*)mc_applied
+ i
;
196 memcpy(m
, µcode
[update_req
[i
].slot
], sizeof(struct microcode
));
202 static void do_update_one(void *unused
)
204 int cpu_num
= smp_processor_id();
205 struct cpuinfo_x86
*c
= cpu_data
+ cpu_num
;
206 struct update_req
*req
= update_req
+ cpu_num
;
207 unsigned int pf
= 0, val
[2], rev
, sig
;
211 req
->err
= 1; /* assume update will fail on this cpu */
213 if (c
->x86_vendor
!= X86_VENDOR_INTEL
|| c
->x86
< 6 ||
214 cpu_has(c
, X86_FEATURE_IA64
)) {
215 printk(KERN_ERR
"microcode: CPU%d not a capable Intel processor\n", cpu_num
);
219 sig
= c
->x86_mask
+ (c
->x86_model
<<4) + (c
->x86
<<8);
221 if ((c
->x86_model
>= 5) || (c
->x86
> 6)) {
222 /* get processor flags from MSR 0x17 */
223 rdmsr(MSR_IA32_PLATFORM_ID
, val
[0], val
[1]);
224 pf
= 1 << ((val
[1] >> 18) & 7);
227 for (i
=0; i
<microcode_num
; i
++)
228 if (microcode
[i
].sig
== sig
&& microcode
[i
].pf
== pf
&&
229 microcode
[i
].ldrver
== 1 && microcode
[i
].hdrver
== 1) {
231 struct microcode
*m
= µcode
[i
];
232 unsigned int *sump
= (unsigned int *)(m
+1);
234 printf("Microcode\n");
235 printf(" Header Revision %d\n",microcode
[i
].hdrver
);
236 printf(" Date %x/%x/%x\n",
237 ((microcode
[i
].date
>> 24 ) & 0xff),
238 ((microcode
[i
].date
>> 16 ) & 0xff),
239 (microcode
[i
].date
& 0xFFFF));
240 printf(" Type %x Family %x Model %x Stepping %x\n",
241 ((microcode
[i
].sig
>> 12) & 0x3),
242 ((microcode
[i
].sig
>> 8) & 0xf),
243 ((microcode
[i
].sig
>> 4) & 0xf),
244 ((microcode
[i
].sig
& 0xf)));
245 printf(" Checksum %x\n",microcode
[i
].cksum
);
246 printf(" Loader Revision %x\n",microcode
[i
].ldrver
);
247 printf(" Processor Flags %x\n\n",microcode
[i
].pf
);
251 /* serialize access to update decision */
252 spin_lock_irqsave(µcode_update_lock
, flags
);
254 /* trick, to work even if there was no prior update by the BIOS */
255 wrmsr(MSR_IA32_UCODE_REV
, 0, 0);
256 __asm__
__volatile__ ("cpuid" : : : "ax", "bx", "cx", "dx");
258 /* get current (on-cpu) revision into rev (ignore val[0]) */
259 rdmsr(MSR_IA32_UCODE_REV
, val
[0], rev
);
261 if (microcode
[i
].rev
< rev
) {
262 spin_unlock_irqrestore(µcode_update_lock
, flags
);
264 "microcode: CPU%d not 'upgrading' to earlier revision"
265 " %d (current=%d)\n", cpu_num
, microcode
[i
].rev
, rev
);
267 } else if (microcode
[i
].rev
== rev
) {
268 /* notify the caller of success on this cpu */
270 spin_unlock_irqrestore(µcode_update_lock
, flags
);
272 "microcode: CPU%d already at revision"
273 " %d (current=%d)\n", cpu_num
, microcode
[i
].rev
, rev
);
277 /* Verify the checksum */
278 while (--sump
>= (unsigned int *)m
)
282 spin_unlock_irqrestore(µcode_update_lock
, flags
);
283 printk(KERN_ERR
"microcode: CPU%d aborting, "
284 "bad checksum\n", cpu_num
);
288 /* write microcode via MSR 0x79 */
289 wrmsr(MSR_IA32_UCODE_WRITE
, (unsigned int)(m
->bits
), 0);
292 __asm__
__volatile__ ("cpuid" : : : "ax", "bx", "cx", "dx");
294 /* get the current revision from MSR 0x8B */
295 rdmsr(MSR_IA32_UCODE_REV
, val
[0], val
[1]);
297 /* notify the caller of success on this cpu */
299 spin_unlock_irqrestore(µcode_update_lock
, flags
);
300 printk(KERN_INFO
"microcode: CPU%d updated from revision "
301 "%d to %d, date=%08x\n",
302 cpu_num
, rev
, val
[1], microcode
[i
].date
);
307 "microcode: CPU%d no microcode found! (sig=%x, pflags=%d)\n",
312 static ssize_t
microcode_read(struct file
*file
, char *buf
, size_t len
, loff_t
*ppos
)
316 down_read(µcode_rwsem
);
317 if (*ppos
>= mc_fsize
)
319 if (*ppos
+ len
> mc_fsize
)
320 len
= mc_fsize
- *ppos
;
322 if (copy_to_user(buf
, mc_applied
+ *ppos
, len
))
327 up_read(µcode_rwsem
);
331 static ssize_t
microcode_write(struct file
*file
, const char *buf
, size_t len
, loff_t
*ppos
)
335 if (!len
|| len
% sizeof(struct microcode
) != 0) {
336 printk(KERN_ERR
"microcode: can only write in N*%d bytes units\n",
337 sizeof(struct microcode
));
340 if ((len
>> PAGE_SHIFT
) > num_physpages
) {
341 printk(KERN_ERR
"microcode: too much data (max %ld pages)\n", num_physpages
);
344 down_write(µcode_rwsem
);
346 mc_applied
= kmalloc(NR_CPUS
*sizeof(struct microcode
),
349 up_write(µcode_rwsem
);
350 printk(KERN_ERR
"microcode: out of memory for saved microcode\n");
355 microcode_num
= len
/sizeof(struct microcode
);
356 microcode
= vmalloc(len
);
362 if (copy_from_user(microcode
, buf
, len
)) {
367 if(do_microcode_update()) {
371 mc_fsize
= NR_CPUS
* sizeof(struct microcode
);
375 devfs_set_file_size(devfs_handle
, mc_fsize
);
378 up_write(µcode_rwsem
);
382 static int microcode_ioctl(struct inode
*inode
, struct file
*file
,
383 unsigned int cmd
, unsigned long arg
)
386 case MICROCODE_IOCFREE
:
387 down_write(µcode_rwsem
);
389 int bytes
= NR_CPUS
* sizeof(struct microcode
);
391 devfs_set_file_size(devfs_handle
, 0);
394 printk(KERN_INFO
"microcode: freed %d bytes\n", bytes
);
396 up_write(µcode_rwsem
);
399 up_write(µcode_rwsem
);
403 printk(KERN_ERR
"microcode: unknown ioctl cmd=%d\n", cmd
);