blob: 00bdc3dd693e5b35419bd57b20491508021fa6e3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Intel CPU Microcode Update Driver for Linux
3 *
4 * Copyright (C) 2000-2004 Tigran Aivazian
Shaohua Li9a3110b2006-09-27 01:50:51 -07005 * 2006 Shaohua Li <shaohua.li@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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.
10 *
11 * Reference: Section 8.10 of Volume III, Intel Pentium 4 Manual,
12 * Order Number 245472 or free download from:
13 *
14 * http://developer.intel.com/design/pentium4/manuals/245472.htm
15 *
16 * For more information, go to http://www.urbanmyth.org/microcode
17 *
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.
22 *
23 * 1.0 16 Feb 2000, Tigran Aivazian <tigran@sco.com>
24 * Initial release.
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
67 * in kernel memory.
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.
71 */
72
73//#define DEBUG /* pr_debug */
Randy Dunlapa9415642006-01-11 12:17:48 -080074#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <linux/kernel.h>
76#include <linux/init.h>
77#include <linux/sched.h>
Dave Jones5cf6c542006-02-28 16:58:53 -080078#include <linux/cpumask.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include <linux/module.h>
80#include <linux/slab.h>
81#include <linux/vmalloc.h>
82#include <linux/miscdevice.h>
83#include <linux/spinlock.h>
84#include <linux/mm.h>
Ingo Molnar14cc3e22006-03-26 01:37:14 -080085#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87#include <asm/msr.h>
88#include <asm/uaccess.h>
89#include <asm/processor.h>
90
91MODULE_DESCRIPTION("Intel CPU (IA-32) Microcode Update Driver");
92MODULE_AUTHOR("Tigran Aivazian <tigran@veritas.com>");
93MODULE_LICENSE("GPL");
94
Jan Beulichba528f22006-06-23 02:04:19 -070095#define MICROCODE_VERSION "1.14a"
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
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)
109
110#define sigmatch(s1, s2, p1, p2) \
111 (((s1) == (s2)) && (((p1) & (p2)) || (((p1) == 0) && ((p2) == 0))))
112
113#define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE)
114
115/* serialize access to the physical write to MSR 0x79 */
116static DEFINE_SPINLOCK(microcode_update_lock);
117
118/* no concurrent ->write()s are allowed on /dev/cpu/microcode */
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800119static DEFINE_MUTEX(microcode_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121static struct ucode_cpu_info {
Shaohua Li9a3110b2006-09-27 01:50:51 -0700122 int valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 unsigned int sig;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700124 unsigned int pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 unsigned int rev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 microcode_t *mc;
127} ucode_cpu_info[NR_CPUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Shaohua Li9a3110b2006-09-27 01:50:51 -0700129static void collect_cpu_info(int cpu_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 struct cpuinfo_x86 *c = cpu_data + cpu_num;
132 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
133 unsigned int val[2];
134
Shaohua Li9a3110b2006-09-27 01:50:51 -0700135 /* We should bind the task to the CPU */
136 BUG_ON(raw_smp_processor_id() != cpu_num);
137 uci->pf = uci->rev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 uci->mc = NULL;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700139 uci->valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
142 cpu_has(c, X86_FEATURE_IA64)) {
Shaohua Li9a3110b2006-09-27 01:50:51 -0700143 printk(KERN_ERR "microcode: CPU%d not a capable Intel "
144 "processor\n", cpu_num);
145 uci->valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Shaohua Li9a3110b2006-09-27 01:50:51 -0700149 uci->sig = cpuid_eax(0x00000001);
150
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
156
157 wrmsr(MSR_IA32_UCODE_REV, 0, 0);
Zachary Amsden245067d2005-09-03 15:56:37 -0700158 /* see notes above for revision 1.07. Apparent chip bug */
Andi Kleen487472b2006-01-11 22:45:27 +0100159 sync_core();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 /* 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);
164}
165
Shaohua Li9a3110b2006-09-27 01:50:51 -0700166static inline int microcode_update_match(int cpu_num,
167 microcode_header_t *mc_header, int sig, int pf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
170
Shaohua Li9a3110b2006-09-27 01:50:51 -0700171 if (!sigmatch(sig, uci->sig, pf, uci->pf)
172 || mc_header->rev <= uci->rev)
173 return 0;
174 return 1;
175}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Shaohua Li9a3110b2006-09-27 01:50:51 -0700177static int microcode_sanity_check(void *mc)
178{
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;
184
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");
191 return -EINVAL;
192 }
193
194 if (mc_header->ldrver != 1 || mc_header->hdrver != 1) {
195 printk(KERN_ERR "microcode: error! "
196 "Unknown microcode update format\n");
197 return -EINVAL;
198 }
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");
205 return -EINVAL;
Jan Beulichba528f22006-06-23 02:04:19 -0700206 }
Shaohua Li9a3110b2006-09-27 01:50:51 -0700207 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");
211 return -EFAULT;
Jan Beulichba528f22006-06-23 02:04:19 -0700212 }
Shaohua Li9a3110b2006-09-27 01:50:51 -0700213 ext_sigcount = ext_header->count;
214 }
215
216 /* check extended table checksum */
217 if (ext_table_size) {
218 int ext_table_sum = 0;
219 int * ext_tablep = (int *)ext_header;
220
221 i = ext_table_size / DWSIZE;
222 while (i--)
223 ext_table_sum += ext_tablep[i];
224 if (ext_table_sum) {
225 printk(KERN_WARNING "microcode: aborting, "
226 "bad extended signature table checksum\n");
227 return -EINVAL;
Jan Beulichba528f22006-06-23 02:04:19 -0700228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
Shaohua Li9a3110b2006-09-27 01:50:51 -0700230
231 /* calculate the checksum */
232 orig_sum = 0;
233 i = (MC_HEADER_SIZE + data_size) / DWSIZE;
234 while (i--)
235 orig_sum += ((int *)mc)[i];
236 if (orig_sum) {
237 printk(KERN_ERR "microcode: aborting, bad checksum\n");
238 return -EINVAL;
239 }
240 if (!ext_table_size)
241 return 0;
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);
246 sum = orig_sum
247 - (mc_header->sig + mc_header->pf + mc_header->cksum)
248 + (ext_sig->sig + ext_sig->pf + ext_sig->cksum);
249 if (sum) {
250 printk(KERN_ERR "microcode: aborting, bad checksum\n");
251 return -EINVAL;
252 }
253 }
254 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Shaohua Li9a3110b2006-09-27 01:50:51 -0700257/*
258 * return 0 - no update found
259 * return 1 - found update
260 * return < 0 - error
261 */
262static int get_maching_microcode(void *mc, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Shaohua Li9a3110b2006-09-27 01:50:51 -0700264 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);
268 int ext_sigcount, i;
269 struct extended_signature *ext_sig;
270 void *new_mc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Shaohua Li9a3110b2006-09-27 01:50:51 -0700272 if (microcode_update_match(cpu, mc_header,
273 mc_header->sig, mc_header->pf))
274 goto find;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Shaohua Li9a3110b2006-09-27 01:50:51 -0700276 if (total_size <= get_datasize(mc_header) + MC_HEADER_SIZE)
277 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Shaohua Li9a3110b2006-09-27 01:50:51 -0700279 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
283 + EXT_HEADER_SIZE);
284 for (i = 0; i < ext_sigcount; i++) {
285 if (microcode_update_match(cpu, mc_header,
286 ext_sig->sig, ext_sig->pf))
287 goto find;
288 ext_sig++;
289 }
290 return 0;
291find:
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);
295 if (!new_mc) {
296 printk(KERN_ERR "microcode: error! Can not allocate memory\n");
297 return -ENOMEM;
298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Shaohua Li9a3110b2006-09-27 01:50:51 -0700300 /* free previous update file */
301 vfree(uci->mc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Shaohua Li9a3110b2006-09-27 01:50:51 -0700303 memcpy(new_mc, mc, total_size);
304 uci->mc = new_mc;
305 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Shaohua Li9a3110b2006-09-27 01:50:51 -0700308static void apply_microcode(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 unsigned long flags;
311 unsigned int val[2];
Shaohua Li9a3110b2006-09-27 01:50:51 -0700312 int cpu_num = raw_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
314
Shaohua Li9a3110b2006-09-27 01:50:51 -0700315 /* We should bind the task to the CPU */
316 BUG_ON(cpu_num != cpu);
317
318 if (uci->mc == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 /* serialize access to the physical write to MSR 0x79 */
322 spin_lock_irqsave(&microcode_update_lock, flags);
323
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);
329
Zachary Amsden245067d2005-09-03 15:56:37 -0700330 /* see notes above for revision 1.07. Apparent chip bug */
Andi Kleen487472b2006-01-11 22:45:27 +0100331 sync_core();
Zachary Amsden245067d2005-09-03 15:56:37 -0700332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 /* get the current revision from MSR 0x8B */
334 rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 spin_unlock_irqrestore(&microcode_update_lock, flags);
Shaohua Li9a3110b2006-09-27 01:50:51 -0700337 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]);
340 return;
341 }
342 pr_debug("microcode: CPU%d updated from revision "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 "0x%x to 0x%x, date = %08x \n",
344 cpu_num, uci->rev, val[1], uci->mc->hdr.date);
Shaohua Li9a3110b2006-09-27 01:50:51 -0700345 uci->rev = val[1];
346}
347
348#ifdef CONFIG_MICROCODE_OLD_INTERFACE
349static void __user *user_buffer; /* user area microcode data buffer */
350static unsigned int user_buffer_size; /* it's size */
351
352static long get_next_ucode(void **mc, long offset)
353{
354 microcode_header_t mc_header;
355 unsigned long total_size;
356
357 /* No more data */
358 if (offset >= user_buffer_size)
359 return 0;
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");
362 return -EFAULT;
363 }
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 "
368 "data file\n");
369 return -EINVAL;
370 }
371 *mc = vmalloc(total_size);
372 if (!*mc)
373 return -ENOMEM;
374 if (copy_from_user(*mc, user_buffer + offset, total_size)) {
375 printk(KERN_ERR "microcode: error! Can not read user data\n");
376 vfree(*mc);
377 return -EFAULT;
378 }
379 return offset + total_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
382static int do_microcode_update (void)
383{
Shaohua Li9a3110b2006-09-27 01:50:51 -0700384 long cursor = 0;
385 int error = 0;
386 void * new_mc;
387 int cpu;
388 cpumask_t old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Shaohua Li9a3110b2006-09-27 01:50:51 -0700390 old = current->cpus_allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Shaohua Li9a3110b2006-09-27 01:50:51 -0700392 while ((cursor = get_next_ucode(&new_mc, cursor)) > 0) {
393 error = microcode_sanity_check(new_mc);
394 if (error)
395 goto out;
396 /*
397 * It's possible the data file has multiple matching ucode,
398 * lets keep searching till the latest version
399 */
400 for_each_online_cpu(cpu) {
401 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Shaohua Li9a3110b2006-09-27 01:50:51 -0700403 if (!uci->valid)
404 continue;
405 set_cpus_allowed(current, cpumask_of_cpu(cpu));
406 error = get_maching_microcode(new_mc, cpu);
407 if (error < 0)
408 goto out;
409 if (error == 1)
410 apply_microcode(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
Shaohua Li9a3110b2006-09-27 01:50:51 -0700412 vfree(new_mc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414out:
Shaohua Li9a3110b2006-09-27 01:50:51 -0700415 if (cursor > 0)
416 vfree(new_mc);
417 if (cursor < 0)
418 error = cursor;
419 set_cpus_allowed(current, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return error;
421}
422
Shaohua Li9a3110b2006-09-27 01:50:51 -0700423static int microcode_open (struct inode *unused1, struct file *unused2)
424{
425 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
426}
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428static 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
Shaohua Li9a3110b2006-09-27 01:50:51 -0700442 lock_cpu_hotplug();
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800443 mutex_lock(&microcode_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 user_buffer = (void __user *) buf;
446 user_buffer_size = (int) len;
447
448 ret = do_microcode_update();
449 if (!ret)
450 ret = (ssize_t)len;
451
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800452 mutex_unlock(&microcode_mutex);
Shaohua Li9a3110b2006-09-27 01:50:51 -0700453 unlock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 return ret;
456}
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458static struct file_operations microcode_fops = {
459 .owner = THIS_MODULE,
460 .write = microcode_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 .open = microcode_open,
462};
463
464static struct miscdevice microcode_dev = {
465 .minor = MICROCODE_MINOR,
466 .name = "microcode",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 .fops = &microcode_fops,
468};
469
Shaohua Li9a3110b2006-09-27 01:50:51 -0700470static int __init microcode_dev_init (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 int error;
473
474 error = misc_register(&microcode_dev);
475 if (error) {
476 printk(KERN_ERR
477 "microcode: can't misc_register on minor=%d\n",
478 MICROCODE_MINOR);
479 return error;
480 }
481
Shaohua Li9a3110b2006-09-27 01:50:51 -0700482 return 0;
483}
484
485static void __exit microcode_dev_exit (void)
486{
487 misc_deregister(&microcode_dev);
488}
489
490MODULE_ALIAS_MISCDEV(MICROCODE_MINOR);
491#else
492#define microcode_dev_init() 0
493#define microcode_dev_exit() do { } while(0)
494#endif
495
496static int __init microcode_init (void)
497{
498 int error;
499
500 error = microcode_dev_init();
501 if (error)
502 return error;
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 printk(KERN_INFO
505 "IA-32 Microcode Update Driver: v" MICROCODE_VERSION " <tigran@veritas.com>\n");
506 return 0;
507}
508
509static void __exit microcode_exit (void)
510{
Shaohua Li9a3110b2006-09-27 01:50:51 -0700511 microcode_dev_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
514module_init(microcode_init)
515module_exit(microcode_exit)