blob: f8f1ec5f26c14082cc9bc7d3c8ad7f147880b0ab [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>
Shaohua Lia30a6a22006-09-27 01:50:52 -070086#include <linux/cpu.h>
87#include <linux/firmware.h>
88#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90#include <asm/msr.h>
91#include <asm/uaccess.h>
92#include <asm/processor.h>
93
94MODULE_DESCRIPTION("Intel CPU (IA-32) Microcode Update Driver");
95MODULE_AUTHOR("Tigran Aivazian <tigran@veritas.com>");
96MODULE_LICENSE("GPL");
97
Jan Beulichba528f22006-06-23 02:04:19 -070098#define MICROCODE_VERSION "1.14a"
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100#define DEFAULT_UCODE_DATASIZE (2000) /* 2000 bytes */
101#define MC_HEADER_SIZE (sizeof (microcode_header_t)) /* 48 bytes */
102#define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE) /* 2048 bytes */
103#define EXT_HEADER_SIZE (sizeof (struct extended_sigtable)) /* 20 bytes */
104#define EXT_SIGNATURE_SIZE (sizeof (struct extended_signature)) /* 12 bytes */
105#define DWSIZE (sizeof (u32))
106#define get_totalsize(mc) \
107 (((microcode_t *)mc)->hdr.totalsize ? \
108 ((microcode_t *)mc)->hdr.totalsize : DEFAULT_UCODE_TOTALSIZE)
109#define get_datasize(mc) \
110 (((microcode_t *)mc)->hdr.datasize ? \
111 ((microcode_t *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE)
112
113#define sigmatch(s1, s2, p1, p2) \
114 (((s1) == (s2)) && (((p1) & (p2)) || (((p1) == 0) && ((p2) == 0))))
115
116#define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE)
117
118/* serialize access to the physical write to MSR 0x79 */
119static DEFINE_SPINLOCK(microcode_update_lock);
120
121/* no concurrent ->write()s are allowed on /dev/cpu/microcode */
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800122static DEFINE_MUTEX(microcode_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124static struct ucode_cpu_info {
Shaohua Li9a3110b2006-09-27 01:50:51 -0700125 int valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 unsigned int sig;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700127 unsigned int pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 unsigned int rev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 microcode_t *mc;
130} ucode_cpu_info[NR_CPUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Shaohua Li9a3110b2006-09-27 01:50:51 -0700132static void collect_cpu_info(int cpu_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 struct cpuinfo_x86 *c = cpu_data + cpu_num;
135 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
136 unsigned int val[2];
137
Shaohua Li9a3110b2006-09-27 01:50:51 -0700138 /* We should bind the task to the CPU */
139 BUG_ON(raw_smp_processor_id() != cpu_num);
140 uci->pf = uci->rev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 uci->mc = NULL;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700142 uci->valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
145 cpu_has(c, X86_FEATURE_IA64)) {
Shaohua Li9a3110b2006-09-27 01:50:51 -0700146 printk(KERN_ERR "microcode: CPU%d not a capable Intel "
147 "processor\n", cpu_num);
148 uci->valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Shaohua Li9a3110b2006-09-27 01:50:51 -0700152 uci->sig = cpuid_eax(0x00000001);
153
154 if ((c->x86_model >= 5) || (c->x86 > 6)) {
155 /* get processor flags from MSR 0x17 */
156 rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
157 uci->pf = 1 << ((val[1] >> 18) & 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
159
160 wrmsr(MSR_IA32_UCODE_REV, 0, 0);
Zachary Amsden245067d2005-09-03 15:56:37 -0700161 /* see notes above for revision 1.07. Apparent chip bug */
Andi Kleen487472b2006-01-11 22:45:27 +0100162 sync_core();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 /* get the current revision from MSR 0x8B */
164 rdmsr(MSR_IA32_UCODE_REV, val[0], uci->rev);
165 pr_debug("microcode: collect_cpu_info : sig=0x%x, pf=0x%x, rev=0x%x\n",
166 uci->sig, uci->pf, uci->rev);
167}
168
Shaohua Li9a3110b2006-09-27 01:50:51 -0700169static inline int microcode_update_match(int cpu_num,
170 microcode_header_t *mc_header, int sig, int pf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
172 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
173
Shaohua Li9a3110b2006-09-27 01:50:51 -0700174 if (!sigmatch(sig, uci->sig, pf, uci->pf)
175 || mc_header->rev <= uci->rev)
176 return 0;
177 return 1;
178}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Shaohua Li9a3110b2006-09-27 01:50:51 -0700180static int microcode_sanity_check(void *mc)
181{
182 microcode_header_t *mc_header = mc;
183 struct extended_sigtable *ext_header = NULL;
184 struct extended_signature *ext_sig;
185 unsigned long total_size, data_size, ext_table_size;
186 int sum, orig_sum, ext_sigcount = 0, i;
187
188 total_size = get_totalsize(mc_header);
189 data_size = get_datasize(mc_header);
190 if ((data_size + MC_HEADER_SIZE > total_size)
191 || (data_size < DEFAULT_UCODE_DATASIZE)) {
192 printk(KERN_ERR "microcode: error! "
193 "Bad data size in microcode data file\n");
194 return -EINVAL;
195 }
196
197 if (mc_header->ldrver != 1 || mc_header->hdrver != 1) {
198 printk(KERN_ERR "microcode: error! "
199 "Unknown microcode update format\n");
200 return -EINVAL;
201 }
202 ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
203 if (ext_table_size) {
204 if ((ext_table_size < EXT_HEADER_SIZE)
205 || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
206 printk(KERN_ERR "microcode: error! "
207 "Small exttable size in microcode data file\n");
208 return -EINVAL;
Jan Beulichba528f22006-06-23 02:04:19 -0700209 }
Shaohua Li9a3110b2006-09-27 01:50:51 -0700210 ext_header = mc + MC_HEADER_SIZE + data_size;
211 if (ext_table_size != exttable_size(ext_header)) {
212 printk(KERN_ERR "microcode: error! "
213 "Bad exttable size in microcode data file\n");
214 return -EFAULT;
Jan Beulichba528f22006-06-23 02:04:19 -0700215 }
Shaohua Li9a3110b2006-09-27 01:50:51 -0700216 ext_sigcount = ext_header->count;
217 }
218
219 /* check extended table checksum */
220 if (ext_table_size) {
221 int ext_table_sum = 0;
222 int * ext_tablep = (int *)ext_header;
223
224 i = ext_table_size / DWSIZE;
225 while (i--)
226 ext_table_sum += ext_tablep[i];
227 if (ext_table_sum) {
228 printk(KERN_WARNING "microcode: aborting, "
229 "bad extended signature table checksum\n");
230 return -EINVAL;
Jan Beulichba528f22006-06-23 02:04:19 -0700231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
Shaohua Li9a3110b2006-09-27 01:50:51 -0700233
234 /* calculate the checksum */
235 orig_sum = 0;
236 i = (MC_HEADER_SIZE + data_size) / DWSIZE;
237 while (i--)
238 orig_sum += ((int *)mc)[i];
239 if (orig_sum) {
240 printk(KERN_ERR "microcode: aborting, bad checksum\n");
241 return -EINVAL;
242 }
243 if (!ext_table_size)
244 return 0;
245 /* check extended signature checksum */
246 for (i = 0; i < ext_sigcount; i++) {
247 ext_sig = (struct extended_signature *)((void *)ext_header
248 + EXT_HEADER_SIZE + EXT_SIGNATURE_SIZE * i);
249 sum = orig_sum
250 - (mc_header->sig + mc_header->pf + mc_header->cksum)
251 + (ext_sig->sig + ext_sig->pf + ext_sig->cksum);
252 if (sum) {
253 printk(KERN_ERR "microcode: aborting, bad checksum\n");
254 return -EINVAL;
255 }
256 }
257 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
Shaohua Li9a3110b2006-09-27 01:50:51 -0700260/*
261 * return 0 - no update found
262 * return 1 - found update
263 * return < 0 - error
264 */
265static int get_maching_microcode(void *mc, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Shaohua Li9a3110b2006-09-27 01:50:51 -0700267 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
268 microcode_header_t *mc_header = mc;
269 struct extended_sigtable *ext_header;
270 unsigned long total_size = get_totalsize(mc_header);
271 int ext_sigcount, i;
272 struct extended_signature *ext_sig;
273 void *new_mc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Shaohua Li9a3110b2006-09-27 01:50:51 -0700275 if (microcode_update_match(cpu, mc_header,
276 mc_header->sig, mc_header->pf))
277 goto find;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Shaohua Li9a3110b2006-09-27 01:50:51 -0700279 if (total_size <= get_datasize(mc_header) + MC_HEADER_SIZE)
280 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Shaohua Li9a3110b2006-09-27 01:50:51 -0700282 ext_header = (struct extended_sigtable *)(mc +
283 get_datasize(mc_header) + MC_HEADER_SIZE);
284 ext_sigcount = ext_header->count;
285 ext_sig = (struct extended_signature *)((void *)ext_header
286 + EXT_HEADER_SIZE);
287 for (i = 0; i < ext_sigcount; i++) {
288 if (microcode_update_match(cpu, mc_header,
289 ext_sig->sig, ext_sig->pf))
290 goto find;
291 ext_sig++;
292 }
293 return 0;
294find:
295 pr_debug("microcode: CPU %d found a matching microcode update with"
296 " version 0x%x (current=0x%x)\n", cpu, mc_header->rev,uci->rev);
297 new_mc = vmalloc(total_size);
298 if (!new_mc) {
299 printk(KERN_ERR "microcode: error! Can not allocate memory\n");
300 return -ENOMEM;
301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Shaohua Li9a3110b2006-09-27 01:50:51 -0700303 /* free previous update file */
304 vfree(uci->mc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Shaohua Li9a3110b2006-09-27 01:50:51 -0700306 memcpy(new_mc, mc, total_size);
307 uci->mc = new_mc;
308 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
Shaohua Li9a3110b2006-09-27 01:50:51 -0700311static void apply_microcode(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 unsigned long flags;
314 unsigned int val[2];
Shaohua Li9a3110b2006-09-27 01:50:51 -0700315 int cpu_num = raw_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
317
Shaohua Li9a3110b2006-09-27 01:50:51 -0700318 /* We should bind the task to the CPU */
319 BUG_ON(cpu_num != cpu);
320
321 if (uci->mc == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 /* serialize access to the physical write to MSR 0x79 */
325 spin_lock_irqsave(&microcode_update_lock, flags);
326
327 /* write microcode via MSR 0x79 */
328 wrmsr(MSR_IA32_UCODE_WRITE,
329 (unsigned long) uci->mc->bits,
330 (unsigned long) uci->mc->bits >> 16 >> 16);
331 wrmsr(MSR_IA32_UCODE_REV, 0, 0);
332
Zachary Amsden245067d2005-09-03 15:56:37 -0700333 /* see notes above for revision 1.07. Apparent chip bug */
Andi Kleen487472b2006-01-11 22:45:27 +0100334 sync_core();
Zachary Amsden245067d2005-09-03 15:56:37 -0700335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 /* get the current revision from MSR 0x8B */
337 rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 spin_unlock_irqrestore(&microcode_update_lock, flags);
Shaohua Li9a3110b2006-09-27 01:50:51 -0700340 if (val[1] != uci->mc->hdr.rev) {
341 printk(KERN_ERR "microcode: CPU%d updated from revision "
342 "0x%x to 0x%x failed\n", cpu_num, uci->rev, val[1]);
343 return;
344 }
345 pr_debug("microcode: CPU%d updated from revision "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 "0x%x to 0x%x, date = %08x \n",
347 cpu_num, uci->rev, val[1], uci->mc->hdr.date);
Shaohua Li9a3110b2006-09-27 01:50:51 -0700348 uci->rev = val[1];
349}
350
351#ifdef CONFIG_MICROCODE_OLD_INTERFACE
352static void __user *user_buffer; /* user area microcode data buffer */
353static unsigned int user_buffer_size; /* it's size */
354
355static long get_next_ucode(void **mc, long offset)
356{
357 microcode_header_t mc_header;
358 unsigned long total_size;
359
360 /* No more data */
361 if (offset >= user_buffer_size)
362 return 0;
363 if (copy_from_user(&mc_header, user_buffer + offset, MC_HEADER_SIZE)) {
364 printk(KERN_ERR "microcode: error! Can not read user data\n");
365 return -EFAULT;
366 }
367 total_size = get_totalsize(&mc_header);
368 if ((offset + total_size > user_buffer_size)
369 || (total_size < DEFAULT_UCODE_TOTALSIZE)) {
370 printk(KERN_ERR "microcode: error! Bad total size in microcode "
371 "data file\n");
372 return -EINVAL;
373 }
374 *mc = vmalloc(total_size);
375 if (!*mc)
376 return -ENOMEM;
377 if (copy_from_user(*mc, user_buffer + offset, total_size)) {
378 printk(KERN_ERR "microcode: error! Can not read user data\n");
379 vfree(*mc);
380 return -EFAULT;
381 }
382 return offset + total_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
385static int do_microcode_update (void)
386{
Shaohua Li9a3110b2006-09-27 01:50:51 -0700387 long cursor = 0;
388 int error = 0;
389 void * new_mc;
390 int cpu;
391 cpumask_t old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Shaohua Li9a3110b2006-09-27 01:50:51 -0700393 old = current->cpus_allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Shaohua Li9a3110b2006-09-27 01:50:51 -0700395 while ((cursor = get_next_ucode(&new_mc, cursor)) > 0) {
396 error = microcode_sanity_check(new_mc);
397 if (error)
398 goto out;
399 /*
400 * It's possible the data file has multiple matching ucode,
401 * lets keep searching till the latest version
402 */
403 for_each_online_cpu(cpu) {
404 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Shaohua Li9a3110b2006-09-27 01:50:51 -0700406 if (!uci->valid)
407 continue;
408 set_cpus_allowed(current, cpumask_of_cpu(cpu));
409 error = get_maching_microcode(new_mc, cpu);
410 if (error < 0)
411 goto out;
412 if (error == 1)
413 apply_microcode(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
Shaohua Li9a3110b2006-09-27 01:50:51 -0700415 vfree(new_mc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417out:
Shaohua Li9a3110b2006-09-27 01:50:51 -0700418 if (cursor > 0)
419 vfree(new_mc);
420 if (cursor < 0)
421 error = cursor;
422 set_cpus_allowed(current, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return error;
424}
425
Shaohua Li9a3110b2006-09-27 01:50:51 -0700426static int microcode_open (struct inode *unused1, struct file *unused2)
427{
428 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
429}
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431static ssize_t microcode_write (struct file *file, const char __user *buf, size_t len, loff_t *ppos)
432{
433 ssize_t ret;
434
435 if (len < DEFAULT_UCODE_TOTALSIZE) {
436 printk(KERN_ERR "microcode: not enough data\n");
437 return -EINVAL;
438 }
439
440 if ((len >> PAGE_SHIFT) > num_physpages) {
441 printk(KERN_ERR "microcode: too much data (max %ld pages)\n", num_physpages);
442 return -EINVAL;
443 }
444
Shaohua Li9a3110b2006-09-27 01:50:51 -0700445 lock_cpu_hotplug();
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800446 mutex_lock(&microcode_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 user_buffer = (void __user *) buf;
449 user_buffer_size = (int) len;
450
451 ret = do_microcode_update();
452 if (!ret)
453 ret = (ssize_t)len;
454
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800455 mutex_unlock(&microcode_mutex);
Shaohua Li9a3110b2006-09-27 01:50:51 -0700456 unlock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 return ret;
459}
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461static struct file_operations microcode_fops = {
462 .owner = THIS_MODULE,
463 .write = microcode_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 .open = microcode_open,
465};
466
467static struct miscdevice microcode_dev = {
468 .minor = MICROCODE_MINOR,
469 .name = "microcode",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 .fops = &microcode_fops,
471};
472
Shaohua Li9a3110b2006-09-27 01:50:51 -0700473static int __init microcode_dev_init (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 int error;
476
477 error = misc_register(&microcode_dev);
478 if (error) {
479 printk(KERN_ERR
480 "microcode: can't misc_register on minor=%d\n",
481 MICROCODE_MINOR);
482 return error;
483 }
484
Shaohua Li9a3110b2006-09-27 01:50:51 -0700485 return 0;
486}
487
488static void __exit microcode_dev_exit (void)
489{
490 misc_deregister(&microcode_dev);
491}
492
493MODULE_ALIAS_MISCDEV(MICROCODE_MINOR);
494#else
495#define microcode_dev_init() 0
496#define microcode_dev_exit() do { } while(0)
497#endif
498
Shaohua Lia30a6a22006-09-27 01:50:52 -0700499static long get_next_ucode_from_buffer(void **mc, void *buf,
500 unsigned long size, long offset)
501{
502 microcode_header_t *mc_header;
503 unsigned long total_size;
504
505 /* No more data */
506 if (offset >= size)
507 return 0;
508 mc_header = (microcode_header_t *)(buf + offset);
509 total_size = get_totalsize(mc_header);
510
511 if ((offset + total_size > size)
512 || (total_size < DEFAULT_UCODE_TOTALSIZE)) {
513 printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
514 return -EINVAL;
515 }
516
517 *mc = vmalloc(total_size);
518 if (!*mc) {
519 printk(KERN_ERR "microcode: error! Can not allocate memory\n");
520 return -ENOMEM;
521 }
522 memcpy(*mc, buf + offset, total_size);
523 return offset + total_size;
524}
525
526/* fake device for request_firmware */
527static struct platform_device *microcode_pdev;
528
529static int cpu_request_microcode(int cpu)
530{
531 char name[30];
532 struct cpuinfo_x86 *c = cpu_data + cpu;
533 const struct firmware *firmware;
534 void * buf;
535 unsigned long size;
536 long offset = 0;
537 int error;
538 void *mc;
539
540 /* We should bind the task to the CPU */
541 BUG_ON(cpu != raw_smp_processor_id());
542 sprintf(name,"intel-ucode/%02x-%02x-%02x",
543 c->x86, c->x86_model, c->x86_mask);
544 error = request_firmware(&firmware, name, &microcode_pdev->dev);
545 if (error) {
546 pr_debug("ucode data file %s load failed\n", name);
547 return error;
548 }
549 buf = (void *)firmware->data;
550 size = firmware->size;
551 while ((offset = get_next_ucode_from_buffer(&mc, buf, size, offset))
552 > 0) {
553 error = microcode_sanity_check(mc);
554 if (error)
555 break;
556 error = get_maching_microcode(mc, cpu);
557 if (error < 0)
558 break;
559 /*
560 * It's possible the data file has multiple matching ucode,
561 * lets keep searching till the latest version
562 */
563 if (error == 1) {
564 apply_microcode(cpu);
565 error = 0;
566 }
567 vfree(mc);
568 }
569 if (offset > 0)
570 vfree(mc);
571 if (offset < 0)
572 error = offset;
573 release_firmware(firmware);
574
575 return error;
576}
577
578static void microcode_init_cpu(int cpu)
579{
580 cpumask_t old;
581 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
582
583 old = current->cpus_allowed;
584
585 set_cpus_allowed(current, cpumask_of_cpu(cpu));
586 mutex_lock(&microcode_mutex);
587 collect_cpu_info(cpu);
588 if (uci->valid)
589 cpu_request_microcode(cpu);
590 mutex_unlock(&microcode_mutex);
591 set_cpus_allowed(current, old);
592}
593
594static void microcode_fini_cpu(int cpu)
595{
596 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
597
598 mutex_lock(&microcode_mutex);
599 uci->valid = 0;
600 vfree(uci->mc);
601 uci->mc = NULL;
602 mutex_unlock(&microcode_mutex);
603}
604
Shaohua Li9a3110b2006-09-27 01:50:51 -0700605static int __init microcode_init (void)
606{
607 int error;
608
609 error = microcode_dev_init();
610 if (error)
611 return error;
Shaohua Lia30a6a22006-09-27 01:50:52 -0700612 microcode_pdev = platform_device_register_simple("microcode", -1,
613 NULL, 0);
614 if (IS_ERR(microcode_pdev)) {
615 microcode_dev_exit();
616 return PTR_ERR(microcode_pdev);
617 }
Shaohua Li9a3110b2006-09-27 01:50:51 -0700618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 printk(KERN_INFO
620 "IA-32 Microcode Update Driver: v" MICROCODE_VERSION " <tigran@veritas.com>\n");
621 return 0;
622}
623
624static void __exit microcode_exit (void)
625{
Shaohua Li9a3110b2006-09-27 01:50:51 -0700626 microcode_dev_exit();
Shaohua Lia30a6a22006-09-27 01:50:52 -0700627 platform_device_unregister(microcode_pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
630module_init(microcode_init)
631module_exit(microcode_exit)