2 * Intel CPU Microcode Update Driver for Linux
4 * Copyright (C) 2000-2004 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.
23 //#define DEBUG /* pr_debug */
24 #include <linux/kernel.h>
25 #include <linux/init.h>
26 #include <linux/sched.h>
27 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/vmalloc.h>
30 #include <linux/miscdevice.h>
31 #include <linux/spinlock.h>
33 #include <linux/syscalls.h>
36 #include <asm/uaccess.h>
37 #include <asm/processor.h>
39 MODULE_DESCRIPTION("Intel CPU (IA-32) Microcode Update Driver");
40 MODULE_AUTHOR("Tigran Aivazian <tigran@veritas.com>");
41 MODULE_LICENSE("GPL");
43 #define MICROCODE_VERSION "1.14-xen"
45 #define DEFAULT_UCODE_DATASIZE (2000) /* 2000 bytes */
46 #define MC_HEADER_SIZE (sizeof (microcode_header_t)) /* 48 bytes */
47 #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE) /* 2048 bytes */
49 /* no concurrent ->write()s are allowed on /dev/cpu/microcode */
50 static DECLARE_MUTEX(microcode_sem);
52 static void __user *user_buffer; /* user area microcode data buffer */
53 static unsigned int user_buffer_size; /* it's size */
55 static int microcode_open (struct inode *unused1, struct file *unused2)
57 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
61 static int do_microcode_update (void)
66 err = sys_mlock((unsigned long)user_buffer, user_buffer_size);
70 op.cmd = DOM0_MICROCODE;
71 op.u.microcode.data = user_buffer;
72 op.u.microcode.length = user_buffer_size;
73 err = HYPERVISOR_dom0_op(&op);
75 (void)sys_munlock((unsigned long)user_buffer, user_buffer_size);
80 static ssize_t microcode_write (struct file *file, const char __user *buf, size_t len, loff_t *ppos)
84 if (len < DEFAULT_UCODE_TOTALSIZE) {
85 printk(KERN_ERR "microcode: not enough data\n");
89 if ((len >> PAGE_SHIFT) > num_physpages) {
90 printk(KERN_ERR "microcode: too much data (max %ld pages)\n", num_physpages);
96 user_buffer = (void __user *) buf;
97 user_buffer_size = (int) len;
99 ret = do_microcode_update();
108 static int microcode_ioctl (struct inode *inode, struct file *file,
109 unsigned int cmd, unsigned long arg)
113 * XXX: will be removed after microcode_ctl
114 * is updated to ignore failure of this ioctl()
116 case MICROCODE_IOCFREE:
124 static struct file_operations microcode_fops = {
125 .owner = THIS_MODULE,
126 .write = microcode_write,
127 .ioctl = microcode_ioctl,
128 .open = microcode_open,
131 static struct miscdevice microcode_dev = {
132 .minor = MICROCODE_MINOR,
134 .devfs_name = "cpu/microcode",
135 .fops = µcode_fops,
138 static int __init microcode_init (void)
142 error = misc_register(µcode_dev);
145 "microcode: can't misc_register on minor=%d\n",
151 "IA-32 Microcode Update Driver: v" MICROCODE_VERSION " <tigran@veritas.com>\n");
155 static void __exit microcode_exit (void)
157 misc_deregister(µcode_dev);
158 printk(KERN_INFO "IA-32 Microcode Update Driver v" MICROCODE_VERSION " unregistered\n");
161 module_init(microcode_init)
162 module_exit(microcode_exit)
163 MODULE_ALIAS_MISCDEV(MICROCODE_MINOR);