File: /usr/src/linux/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      */
51     
52     #include <linux/init.h>
53     #include <linux/sched.h>
54     #include <linux/module.h>
55     #include <linux/slab.h>
56     #include <linux/vmalloc.h>
57     #include <linux/miscdevice.h>
58     #include <linux/devfs_fs_kernel.h>
59     
60     #include <asm/msr.h>
61     #include <asm/uaccess.h>
62     #include <asm/processor.h>
63     
64     #define MICROCODE_VERSION 	"1.08"
65     
66     MODULE_DESCRIPTION("Intel CPU (IA-32) microcode update driver");
67     MODULE_AUTHOR("Tigran Aivazian <tigran@veritas.com>");
68     EXPORT_NO_SYMBOLS;
69     
70     #define MICRO_DEBUG 0
71     
72     #if MICRO_DEBUG
73     #define printf(x...) printk(##x)
74     #else
75     #define printf(x...)
76     #endif
77     
78     /* VFS interface */
79     static int microcode_open(struct inode *, struct file *);
80     static ssize_t microcode_read(struct file *, char *, size_t, loff_t *);
81     static ssize_t microcode_write(struct file *, const char *, size_t, loff_t *);
82     static int microcode_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
83     
84     static int do_microcode_update(void);
85     static void do_update_one(void *);
86     
87     /* read()/write()/ioctl() are serialized on this */
88     static DECLARE_RWSEM(microcode_rwsem);
89     
90     static struct microcode *microcode; /* array of 2048byte microcode blocks */
91     static unsigned int microcode_num;  /* number of chunks in microcode */
92     static char *mc_applied;            /* array of applied microcode blocks */
93     static unsigned int mc_fsize;       /* file size of /dev/cpu/microcode */
94     
95     /* we share file_operations between misc and devfs mechanisms */
96     static struct file_operations microcode_fops = {
97     	owner:		THIS_MODULE,
98     	read:		microcode_read,
99     	write:		microcode_write,
100     	ioctl:		microcode_ioctl,
101     	open:		microcode_open,
102     };
103     
104     static struct miscdevice microcode_dev = {
105     	minor: MICROCODE_MINOR,
106     	name:	"microcode",
107     	fops:	&microcode_fops,
108     };
109     
110     static devfs_handle_t devfs_handle;
111     
112     static int __init microcode_init(void)
113     {
114     	int error;
115     
116     	error = misc_register(&microcode_dev);
117     	if (error)
118     		printk(KERN_WARNING 
119     			"microcode: can't misc_register on minor=%d\n",
120     			MICROCODE_MINOR);
121     
122     	devfs_handle = devfs_register(NULL, "cpu/microcode",
123     			DEVFS_FL_DEFAULT, 0, 0, S_IFREG | S_IRUSR | S_IWUSR, 
124     			&microcode_fops, NULL);
125     	if (devfs_handle == NULL && error) {
126     		printk(KERN_ERR "microcode: failed to devfs_register()\n");
127     		goto out;
128     	}
129     	error = 0;
130     	printk(KERN_INFO 
131     		"IA-32 Microcode Update Driver: v%s <tigran@veritas.com>\n", 
132     		MICROCODE_VERSION);
133     
134     out:
135     	return error;
136     }
137     
138     static void __exit microcode_exit(void)
139     {
140     	misc_deregister(&microcode_dev);
141     	devfs_unregister(devfs_handle);
142     	if (mc_applied)
143     		kfree(mc_applied);
144     	printk(KERN_INFO "IA-32 Microcode Update Driver v%s unregistered\n", 
145     			MICROCODE_VERSION);
146     }
147     
148     module_init(microcode_init)
149     module_exit(microcode_exit)
150     
151     static int microcode_open(struct inode *unused1, struct file *unused2)
152     {
153     	return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
154     }
155     
156     /*
157      * update_req[cpu].err is set to 1 if update failed on 'cpu', 0 otherwise
158      * if err==0, microcode[update_req[cpu].slot] points to applied block of microcode
159      */
160     struct update_req {
161     	int err;
162     	int slot;
163     } update_req[NR_CPUS];
164     
165     static int do_microcode_update(void)
166     {
167     	int i, error = 0, err;
168     	struct microcode *m;
169     
170     	if (smp_call_function(do_update_one, NULL, 1, 1) != 0) {
171     		printk(KERN_ERR "microcode: IPI timeout, giving up\n");
172     		return -EIO;
173     	}
174     	do_update_one(NULL);
175     
176     	for (i=0; i<smp_num_cpus; i++) {
177     		err = update_req[i].err;
178     		error += err;
179     		if (!err) {
180     			m = (struct microcode *)mc_applied + i;
181     			memcpy(m, &microcode[update_req[i].slot], sizeof(struct microcode));
182     		}
183     	}
184     	return error;
185     }
186     
187     static void do_update_one(void *unused)
188     {
189     	int cpu_num = smp_processor_id();
190     	struct cpuinfo_x86 *c = cpu_data + cpu_num;
191     	struct update_req *req = update_req + cpu_num;
192     	unsigned int pf = 0, val[2], rev, sig;
193     	int i,found=0;
194     
195     	req->err = 1; /* assume update will fail on this cpu */
196     
197     	if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
198     		test_bit(X86_FEATURE_IA64, &c->x86_capability)){
199     		printk(KERN_ERR "microcode: CPU%d not a capable Intel processor\n", cpu_num);
200     		return;
201     	}
202     
203     	sig = c->x86_mask + (c->x86_model<<4) + (c->x86<<8);
204     
205     	if ((c->x86_model >= 5) || (c->x86 > 6)) {
206     		/* get processor flags from MSR 0x17 */
207     		rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
208     		pf = 1 << ((val[1] >> 18) & 7);
209     	}
210     
211     	for (i=0; i<microcode_num; i++)
212     		if (microcode[i].sig == sig && microcode[i].pf == pf &&
213     		    microcode[i].ldrver == 1 && microcode[i].hdrver == 1) {
214     
215     			found=1;
216     
217     			printf("Microcode\n");
218     			printf("   Header Revision %d\n",microcode[i].hdrver);
219     			printf("   Date %x/%x/%x\n",
220     				((microcode[i].date >> 24 ) & 0xff),
221     				((microcode[i].date >> 16 ) & 0xff),
222     				(microcode[i].date & 0xFFFF));
223     			printf("   Type %x Family %x Model %x Stepping %x\n",
224     				((microcode[i].sig >> 12) & 0x3),
225     				((microcode[i].sig >> 8) & 0xf),
226     				((microcode[i].sig >> 4) & 0xf),
227     				((microcode[i].sig & 0xf)));
228     			printf("   Checksum %x\n",microcode[i].cksum);
229     			printf("   Loader Revision %x\n",microcode[i].ldrver);
230     			printf("   Processor Flags %x\n\n",microcode[i].pf);
231     
232     			/* trick, to work even if there was no prior update by the BIOS */
233     			wrmsr(MSR_IA32_UCODE_REV, 0, 0);
234     			__asm__ __volatile__ ("cpuid" : : : "ax", "bx", "cx", "dx");
235     
236     			/* get current (on-cpu) revision into rev (ignore val[0]) */
237     			rdmsr(MSR_IA32_UCODE_REV, val[0], rev);
238     			if (microcode[i].rev < rev) {
239     				printk(KERN_ERR 
240     					"microcode: CPU%d not 'upgrading' to earlier revision"
241     					" %d (current=%d)\n", cpu_num, microcode[i].rev, rev);
242     			} else if (microcode[i].rev == rev) {
243     				printk(KERN_ERR
244     					"microcode: CPU%d already up-to-date (revision %d)\n",
245     						cpu_num, rev);
246     			} else {
247     				int sum = 0;
248     				struct microcode *m = &microcode[i];
249     				unsigned int *sump = (unsigned int *)(m+1);
250     
251     				while (--sump >= (unsigned int *)m)
252     					sum += *sump;
253     				if (sum != 0) {
254     					printk(KERN_ERR "microcode: CPU%d aborting, "
255     							"bad checksum\n", cpu_num);
256     					break;
257     				}
258     
259     				/* write microcode via MSR 0x79 */
260     				wrmsr(MSR_IA32_UCODE_WRITE, (unsigned int)(m->bits), 0);
261     
262     				/* serialize */
263     				__asm__ __volatile__ ("cpuid" : : : "ax", "bx", "cx", "dx");
264     
265     				/* get the current revision from MSR 0x8B */
266     				rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
267     
268     				/* notify the caller of success on this cpu */
269     				req->err = 0;
270     				req->slot = i;
271     
272     				printk(KERN_INFO "microcode: CPU%d updated from revision "
273     						"%d to %d, date=%08x\n", 
274     						cpu_num, rev, val[1], m->date);
275     			}
276     			break;
277     		}
278     
279     	if(!found)
280     		printk(KERN_ERR "microcode: CPU%d no microcode found! (sig=%x, pflags=%d)\n",
281     				cpu_num, sig, pf);
282     }
283     
284     static ssize_t microcode_read(struct file *file, char *buf, size_t len, loff_t *ppos)
285     {
286     	ssize_t ret = 0;
287     
288     	down_read(&microcode_rwsem);
289     	if (*ppos >= mc_fsize)
290     		goto out;
291     	if (*ppos + len > mc_fsize)
292     		len = mc_fsize - *ppos;
293     	ret = -EFAULT;
294     	if (copy_to_user(buf, mc_applied + *ppos, len))
295     		goto out;
296     	*ppos += len;
297     	ret = len;
298     out:
299     	up_read(&microcode_rwsem);
300     	return ret;
301     }
302     
303     static ssize_t microcode_write(struct file *file, const char *buf, size_t len, loff_t *ppos)
304     {
305     	ssize_t ret;
306     
307     	if (len % sizeof(struct microcode) != 0) {
308     		printk(KERN_ERR "microcode: can only write in N*%d bytes units\n", 
309     			sizeof(struct microcode));
310     		return -EINVAL;
311     	}
312     	down_write(&microcode_rwsem);
313     	if (!mc_applied) {
314     		mc_applied = kmalloc(smp_num_cpus*sizeof(struct microcode),
315     				GFP_KERNEL);
316     		if (!mc_applied) {
317     			up_write(&microcode_rwsem);
318     			printk(KERN_ERR "microcode: out of memory for saved microcode\n");
319     			return -ENOMEM;
320     		}
321     	}
322     	
323     	microcode_num = len/sizeof(struct microcode);
324     	microcode = vmalloc(len);
325     	if (!microcode) {
326     		ret = -ENOMEM;
327     		goto out_unlock;
328     	}
329     
330     	if (copy_from_user(microcode, buf, len)) {
331     		ret = -EFAULT;
332     		goto out_fsize;
333     	}
334     
335     	if(do_microcode_update()) {
336     		ret = -EIO;
337     		goto out_fsize;
338     	} else {
339     		mc_fsize = smp_num_cpus * sizeof(struct microcode);
340     		ret = (ssize_t)len;
341     	}
342     out_fsize:
343     	devfs_set_file_size(devfs_handle, mc_fsize);
344     	vfree(microcode);
345     out_unlock:
346     	up_write(&microcode_rwsem);
347     	return ret;
348     }
349     
350     static int microcode_ioctl(struct inode *inode, struct file *file, 
351     		unsigned int cmd, unsigned long arg)
352     {
353     	switch(cmd) {
354     		case MICROCODE_IOCFREE:
355     			down_write(&microcode_rwsem);
356     			if (mc_applied) {
357     				int bytes = smp_num_cpus * sizeof(struct microcode);
358     
359     				devfs_set_file_size(devfs_handle, 0);
360     				kfree(mc_applied);
361     				mc_applied = NULL;
362     				printk(KERN_INFO "microcode: freed %d bytes\n", bytes);
363     				mc_fsize = 0;
364     				up_write(&microcode_rwsem);
365     				return 0;
366     			}
367     			up_write(&microcode_rwsem);
368     			return -ENODATA;
369     
370     		default:
371     			printk(KERN_ERR "microcode: unknown ioctl cmd=%d\n", cmd);
372     			return -EINVAL;
373     	}
374     	return -EINVAL;
375     }
376