patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / arch / i386 / kernel / microcode.c
1 /*
2  *      Intel CPU Microcode Update driver for Linux
3  *
4  *      Copyright (C) 2000 Tigran Aivazian
5  *
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.10 of Volume III, Intel Pentium 4 Manual, 
11  *      Order Number 245472 or free download from:
12  *              
13  *      http://developer.intel.com/design/pentium4/manuals/245472.htm
14  *
15  *      For more information, go to http://www.urbanmyth.org/microcode
16  *
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.
21  *
22  *      1.0     16 Feb 2000, Tigran Aivazian <tigran@sco.com>
23  *              Initial release.
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 2002 Tigran Aivazian <tigran@veritas.com>
59  *              Fix the panic when writing zero-length microcode chunk.
60  *      1.12    29 Sep 2003 Nitin Kamble <nitin.a.kamble@intel.com>, 
61  *              Jun Nakajima <jun.nakajima@intel.com>
62  *              Support for the microcode updates in the new format.
63  *      1.13    10 Oct 2003 Tigran Aivazian <tigran@veritas.com>
64  *              Removed ->read() method and obsoleted MICROCODE_IOCFREE ioctl
65  *              because we no longer hold a copy of applied microcode 
66  *              in kernel memory.
67  */
68
69
70 #include <linux/init.h>
71 #include <linux/sched.h>
72 #include <linux/module.h>
73 #include <linux/slab.h>
74 #include <linux/vmalloc.h>
75 #include <linux/miscdevice.h>
76 #include <linux/spinlock.h>
77 #include <linux/mm.h>
78
79 #include <asm/msr.h>
80 #include <asm/uaccess.h>
81 #include <asm/processor.h>
82
83 MODULE_DESCRIPTION("Intel CPU (IA-32) microcode update driver");
84 MODULE_AUTHOR("Tigran Aivazian <tigran@veritas.com>");
85 MODULE_LICENSE("GPL");
86
87 #define MICROCODE_VERSION       "1.13"
88 #define MICRO_DEBUG             0
89 #if MICRO_DEBUG
90 #define dprintk(x...) printk(KERN_INFO x)
91 #else
92 #define dprintk(x...)
93 #endif
94
95 #define DEFAULT_UCODE_DATASIZE  (2000)    /* 2000 bytes */
96 #define MC_HEADER_SIZE          (sizeof (microcode_header_t))     /* 48 bytes */
97 #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE) /* 2048 bytes */
98 #define EXT_HEADER_SIZE         (sizeof (struct extended_sigtable)) /* 20 bytes */
99 #define EXT_SIGNATURE_SIZE      (sizeof (struct extended_signature)) /* 12 bytes */
100 #define DWSIZE                  (sizeof (u32))
101 #define get_totalsize(mc) \
102         (((microcode_t *)mc)->hdr.totalsize ? \
103          ((microcode_t *)mc)->hdr.totalsize : DEFAULT_UCODE_TOTALSIZE)
104 #define get_datasize(mc) \
105         (((microcode_t *)mc)->hdr.datasize ? \
106          ((microcode_t *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE)
107 #define sigmatch(s1, s2, p1, p2) (((s1) == (s2)) && ((p1) & (p2)))
108 #define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE)
109
110 /* serialize access to the physical write to MSR 0x79 */
111 static spinlock_t microcode_update_lock = SPIN_LOCK_UNLOCKED;
112
113 /* no concurrent ->write()s are allowed on /dev/cpu/microcode */
114 static DECLARE_MUTEX(microcode_sem);
115
116 static void __user *user_buffer;        /* user area microcode data buffer */
117 static unsigned int user_buffer_size;   /* it's size */
118
119 typedef enum mc_error_code {
120         MC_SUCCESS      = 0,
121         MC_NOTFOUND     = 1,
122         MC_MARKED       = 2,
123         MC_ALLOCATED    = 3,
124 } mc_error_code_t;
125
126 static struct ucode_cpu_info {
127         unsigned int sig;
128         unsigned int pf;
129         unsigned int rev;
130         unsigned int cksum;
131         mc_error_code_t err;
132         microcode_t *mc;
133 } ucode_cpu_info[NR_CPUS];
134                                 
135 static int microcode_open (struct inode *unused1, struct file *unused2)
136 {
137         return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
138 }
139
140 static void collect_cpu_info (void *unused)
141 {
142         int cpu_num = smp_processor_id();
143         struct cpuinfo_x86 *c = cpu_data + cpu_num;
144         struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
145         unsigned int val[2];
146
147         uci->sig = uci->pf = uci->rev = uci->cksum = 0;
148         uci->err = MC_NOTFOUND; 
149         uci->mc = NULL;
150
151         if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
152                 cpu_has(c, X86_FEATURE_IA64)) {
153                 printk(KERN_ERR "microcode: CPU%d not a capable Intel processor\n", cpu_num);
154                 return;
155         } else {
156                 uci->sig = cpuid_eax(0x00000001);
157
158                 if ((c->x86_model >= 5) || (c->x86 > 6)) {
159                         /* get processor flags from MSR 0x17 */
160                         rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
161                         uci->pf = 1 << ((val[1] >> 18) & 7);
162                 }
163         }
164
165         wrmsr(MSR_IA32_UCODE_REV, 0, 0);
166         __asm__ __volatile__ ("cpuid" : : : "ax", "bx", "cx", "dx");
167         /* get the current revision from MSR 0x8B */
168         rdmsr(MSR_IA32_UCODE_REV, val[0], uci->rev);
169         dprintk("microcode: collect_cpu_info : sig=0x%x, pf=0x%x, rev=0x%x\n", 
170                         uci->sig, uci->pf, uci->rev);
171 }
172
173 static inline void mark_microcode_update (int cpu_num, microcode_header_t *mc_header, int sig, int pf, int cksum)
174 {
175         struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
176
177         dprintk("Microcode Found.\n");
178         dprintk("   Header Revision 0x%x\n", mc_header->hdrver);
179         dprintk("   Loader Revision 0x%x\n", mc_header->ldrver);
180         dprintk("   Revision 0x%x \n", mc_header->rev);
181         dprintk("   Date %x/%x/%x\n",
182                 ((mc_header->date >> 24 ) & 0xff),
183                 ((mc_header->date >> 16 ) & 0xff),
184                 (mc_header->date & 0xFFFF));
185         dprintk("   Signature 0x%x\n", sig);
186         dprintk("   Type 0x%x Family 0x%x Model 0x%x Stepping 0x%x\n",
187                 ((sig >> 12) & 0x3),
188                 ((sig >> 8) & 0xf),
189                 ((sig >> 4) & 0xf),
190                 ((sig & 0xf)));
191         dprintk("   Processor Flags 0x%x\n", pf);
192         dprintk("   Checksum 0x%x\n", cksum);
193
194         if (mc_header->rev < uci->rev) {
195                 printk(KERN_ERR "microcode: CPU%d not 'upgrading' to earlier revision"
196                        " 0x%x (current=0x%x)\n", cpu_num, mc_header->rev, uci->rev);
197                 goto out;
198         } else if (mc_header->rev == uci->rev) {
199                 /* notify the caller of success on this cpu */
200                 uci->err = MC_SUCCESS;
201                 printk(KERN_ERR "microcode: CPU%d already at revision"
202                         " 0x%x (current=0x%x)\n", cpu_num, mc_header->rev, uci->rev);
203                 goto out;
204         }
205
206         dprintk("microcode: CPU%d found a matching microcode update with "
207                 " revision 0x%x (current=0x%x)\n", cpu_num, mc_header->rev, uci->rev);
208         uci->cksum = cksum;
209         uci->pf = pf; /* keep the original mc pf for cksum calculation */
210         uci->err = MC_MARKED; /* found the match */
211 out:
212         return;
213 }
214
215 static int find_matching_ucodes (void) 
216 {
217         int cursor = 0;
218         int error = 0;
219
220         while (cursor + MC_HEADER_SIZE < user_buffer_size) {
221                 microcode_header_t mc_header;
222                 void *newmc = NULL;
223                 int i, sum, cpu_num, allocated_flag, total_size, data_size, ext_table_size;
224
225                 if (copy_from_user(&mc_header, user_buffer + cursor, MC_HEADER_SIZE)) {
226                         printk(KERN_ERR "microcode: error! Can not read user data\n");
227                         error = -EFAULT;
228                         goto out;
229                 }
230
231                 total_size = get_totalsize(&mc_header);
232                 if ((cursor + total_size > user_buffer_size) || (total_size < DEFAULT_UCODE_TOTALSIZE)) {
233                         printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
234                         error = -EINVAL;
235                         goto out;
236                 }
237
238                 data_size = get_datasize(&mc_header);
239                 if ((data_size + MC_HEADER_SIZE > total_size) || (data_size < DEFAULT_UCODE_DATASIZE)) {
240                         printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
241                         error = -EINVAL;
242                         goto out;
243                 }
244
245                 if (mc_header.ldrver != 1 || mc_header.hdrver != 1) {
246                         printk(KERN_ERR "microcode: error! Unknown microcode update format\n");
247                         error = -EINVAL;
248                         goto out;
249                 }
250                 
251                 for (cpu_num = 0; cpu_num < num_online_cpus(); cpu_num++) {
252                         struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
253                         if (uci->err != MC_NOTFOUND) /* already found a match or not an online cpu*/
254                                 continue;
255
256                         if (sigmatch(mc_header.sig, uci->sig, mc_header.pf, uci->pf))
257                                 mark_microcode_update(cpu_num, &mc_header, mc_header.sig, mc_header.pf, mc_header.cksum);
258                 }
259
260                 ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
261                 if (ext_table_size) {
262                         struct extended_sigtable ext_header;
263                         struct extended_signature ext_sig;
264                         int ext_sigcount;
265
266                         if ((ext_table_size < EXT_HEADER_SIZE) 
267                                         || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
268                                 printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
269                                 error = -EINVAL;
270                                 goto out;
271                         }
272                         if (copy_from_user(&ext_header, user_buffer + cursor 
273                                         + MC_HEADER_SIZE + data_size, EXT_HEADER_SIZE)) {
274                                 printk(KERN_ERR "microcode: error! Can not read user data\n");
275                                 error = -EFAULT;
276                                 goto out;
277                         }
278                         if (ext_table_size != exttable_size(&ext_header)) {
279                                 printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
280                                 error = -EFAULT;
281                                 goto out;
282                         }
283
284                         ext_sigcount = ext_header.count;
285                         
286                         for (i = 0; i < ext_sigcount; i++) {
287                                 if (copy_from_user(&ext_sig, user_buffer + cursor + MC_HEADER_SIZE + data_size + EXT_HEADER_SIZE 
288                                                 + EXT_SIGNATURE_SIZE * i, EXT_SIGNATURE_SIZE)) {
289                                         printk(KERN_ERR "microcode: error! Can not read user data\n");
290                                         error = -EFAULT;
291                                         goto out;
292                                 }
293                                 for (cpu_num = 0; cpu_num < num_online_cpus(); cpu_num++) {
294                                         struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
295                                         if (uci->err != MC_NOTFOUND) /* already found a match or not an online cpu*/
296                                                 continue;
297                                         if (sigmatch(ext_sig.sig, uci->sig, ext_sig.pf, uci->pf)) {
298                                                 mark_microcode_update(cpu_num, &mc_header, ext_sig.sig, ext_sig.pf, ext_sig.cksum);
299                                         }
300                                 }
301                         }
302                 }
303                 /* now check if any cpu has matched */
304                 for (cpu_num = 0, allocated_flag = 0, sum = 0; cpu_num < num_online_cpus(); cpu_num++) {
305                         if (ucode_cpu_info[cpu_num].err == MC_MARKED) { 
306                                 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
307                                 if (!allocated_flag) {
308                                         allocated_flag = 1;
309                                         newmc = vmalloc(total_size);
310                                         if (!newmc) {
311                                                 printk(KERN_ERR "microcode: error! Can not allocate memory\n");
312                                                 error = -ENOMEM;
313                                                 goto out;
314                                         }
315                                         if (copy_from_user(newmc + MC_HEADER_SIZE, 
316                                                                 user_buffer + cursor + MC_HEADER_SIZE, 
317                                                                 total_size - MC_HEADER_SIZE)) {
318                                                 printk(KERN_ERR "microcode: error! Can not read user data\n");
319                                                 vfree(newmc);
320                                                 error = -EFAULT;
321                                                 goto out;
322                                         }
323                                         memcpy(newmc, &mc_header, MC_HEADER_SIZE);
324                                         /* check extended table checksum */
325                                         if (ext_table_size) {
326                                                 int ext_table_sum = 0;
327                                                 int * ext_tablep = (((void *) newmc) + MC_HEADER_SIZE + data_size);
328                                                 i = ext_table_size / DWSIZE;
329                                                 while (i--) ext_table_sum += ext_tablep[i];
330                                                 if (ext_table_sum) {
331                                                         printk(KERN_WARNING "microcode: aborting, bad extended signature table checksum\n");
332                                                         vfree(newmc);
333                                                         error = -EINVAL;
334                                                         goto out;
335                                                 }
336                                         }
337
338                                         /* calculate the checksum */
339                                         i = (MC_HEADER_SIZE + data_size) / DWSIZE;
340                                         while (i--) sum += ((int *)newmc)[i];
341                                         sum -= (mc_header.sig + mc_header.pf + mc_header.cksum);
342                                 }
343                                 ucode_cpu_info[cpu_num].mc = newmc;
344                                 ucode_cpu_info[cpu_num].err = MC_ALLOCATED; /* mc updated */
345                                 if (sum + uci->sig + uci->pf + uci->cksum != 0) {
346                                         printk(KERN_ERR "microcode: CPU%d aborting, bad checksum\n", cpu_num);
347                                         error = -EINVAL;
348                                         goto out;
349                                 }
350                         }
351                 }
352                 cursor += total_size; /* goto the next update patch */
353         } /* end of while */
354 out:
355         return error;
356 }
357
358 static void do_update_one (void * unused)
359 {
360         unsigned long flags;
361         unsigned int val[2];
362         int cpu_num = smp_processor_id();
363         struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
364
365         if (uci->mc == NULL) {
366                 printk(KERN_INFO "microcode: No suitable data for cpu %d\n", cpu_num);
367                 return;
368         }
369
370         /* serialize access to the physical write to MSR 0x79 */
371         spin_lock_irqsave(&microcode_update_lock, flags);          
372
373         /* write microcode via MSR 0x79 */
374         wrmsr(MSR_IA32_UCODE_WRITE,
375                 (unsigned long) uci->mc->bits, 
376                 (unsigned long) uci->mc->bits >> 16 >> 16);
377         wrmsr(MSR_IA32_UCODE_REV, 0, 0);
378
379         __asm__ __volatile__ ("cpuid" : : : "ax", "bx", "cx", "dx");
380         /* get the current revision from MSR 0x8B */
381         rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
382
383         /* notify the caller of success on this cpu */
384         uci->err = MC_SUCCESS;
385         spin_unlock_irqrestore(&microcode_update_lock, flags);
386         printk(KERN_INFO "microcode: CPU%d updated from revision "
387                "0x%x to 0x%x, date = %08x \n", 
388                cpu_num, uci->rev, val[1], uci->mc->hdr.date);
389         return;
390 }
391
392 static int do_microcode_update (void)
393 {
394         int i, error;
395
396         if (on_each_cpu(collect_cpu_info, NULL, 1, 1) != 0) {
397                 printk(KERN_ERR "microcode: Error! Could not run on all processors\n");
398                 error = -EIO;
399                 goto out;
400         }
401
402         if ((error = find_matching_ucodes())) {
403                 printk(KERN_ERR "microcode: Error in the microcode data\n");
404                 goto out_free;
405         }
406
407         if (on_each_cpu(do_update_one, NULL, 1, 1) != 0) {
408                 printk(KERN_ERR "microcode: Error! Could not run on all processors\n");
409                 error = -EIO;
410         }
411
412 out_free:
413         for (i = 0; i < num_online_cpus(); i++) {
414                 if (ucode_cpu_info[i].mc) {
415                         int j;
416                         void *tmp = ucode_cpu_info[i].mc;
417                         vfree(tmp);
418                         for (j = i; j < num_online_cpus(); j++) {
419                                 if (ucode_cpu_info[j].mc == tmp)
420                                         ucode_cpu_info[j].mc = NULL;
421                         }
422                 }
423         }
424 out:
425         return error;
426 }
427
428 static ssize_t microcode_write (struct file *file, const char __user *buf, size_t len, loff_t *ppos)
429 {
430         ssize_t ret;
431
432         if (len < DEFAULT_UCODE_TOTALSIZE) {
433                 printk(KERN_ERR "microcode: not enough data\n"); 
434                 return -EINVAL;
435         }
436
437         if ((len >> PAGE_SHIFT) > num_physpages) {
438                 printk(KERN_ERR "microcode: too much data (max %ld pages)\n", num_physpages);
439                 return -EINVAL;
440         }
441
442         down(&microcode_sem);
443
444         user_buffer = (void __user *) buf;
445         user_buffer_size = (int) len;
446
447         ret = do_microcode_update();
448         if (!ret)
449                 ret = (ssize_t)len;
450
451         up(&microcode_sem);
452
453         return ret;
454 }
455
456 static int microcode_ioctl (struct inode *inode, struct file *file, 
457                 unsigned int cmd, unsigned long arg)
458 {
459         switch (cmd) {
460                 /* 
461                  *  XXX: will be removed after microcode_ctl 
462                  *  is updated to ignore failure of this ioctl()
463                  */
464                 case MICROCODE_IOCFREE:
465                         return 0;
466                 default:
467                         return -EINVAL;
468         }
469         return -EINVAL;
470 }
471
472 static struct file_operations microcode_fops = {
473         .owner          = THIS_MODULE,
474         .write          = microcode_write,
475         .ioctl          = microcode_ioctl,
476         .open           = microcode_open,
477 };
478
479 static struct miscdevice microcode_dev = {
480         .minor          = MICROCODE_MINOR,
481         .name           = "microcode",
482         .fops           = &microcode_fops,
483 };
484
485 static int __init microcode_init (void)
486 {
487         int error;
488
489         error = misc_register(&microcode_dev);
490         if (error) {
491                 printk(KERN_ERR
492                         "microcode: can't misc_register on minor=%d\n",
493                         MICROCODE_MINOR);
494                 return error;
495         }
496
497         printk(KERN_INFO 
498                 "IA-32 Microcode Update Driver: v%s <tigran@veritas.com>\n", 
499                 MICROCODE_VERSION);
500         return 0;
501 }
502
503 static void __exit microcode_exit (void)
504 {
505         misc_deregister(&microcode_dev);
506         printk(KERN_INFO "IA-32 Microcode Update Driver v%s unregistered\n", 
507                         MICROCODE_VERSION);
508 }
509
510 module_init(microcode_init)
511 module_exit(microcode_exit)
512 MODULE_ALIAS_MISCDEV(MICROCODE_MINOR);