blob: 652fa5c38ebe34c0846fc56f0a8e0eb89e5f3c6a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Intel CPU Microcode Update Driver for Linux
3 *
Tigran Aivazian69688262006-12-13 00:35:14 -08004 * Copyright (C) 2000-2006 Tigran Aivazian <tigran@aivazian.fsnet.co.uk>
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
Ben Castricumbc4e0f92008-06-10 13:15:12 +02008 * belonging to IA-32 family - PentiumPro, Pentium II,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Pentium III, Xeon, Pentium 4, etc.
10 *
Ben Castricumbc4e0f92008-06-10 13:15:12 +020011 * Reference: Section 8.11 of Volume 3a, IA-32 Intel? Architecture
12 * Software Developer's Manual
13 * Order Number 253668 or free download from:
14 *
15 * http://developer.intel.com/design/pentium4/manuals/253668.htm
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 *
17 * For more information, go to http://www.urbanmyth.org/microcode
18 *
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License
21 * as published by the Free Software Foundation; either version
22 * 2 of the License, or (at your option) any later version.
23 *
24 * 1.0 16 Feb 2000, Tigran Aivazian <tigran@sco.com>
25 * Initial release.
26 * 1.01 18 Feb 2000, Tigran Aivazian <tigran@sco.com>
27 * Added read() support + cleanups.
28 * 1.02 21 Feb 2000, Tigran Aivazian <tigran@sco.com>
29 * Added 'device trimming' support. open(O_WRONLY) zeroes
30 * and frees the saved copy of applied microcode.
31 * 1.03 29 Feb 2000, Tigran Aivazian <tigran@sco.com>
32 * Made to use devfs (/dev/cpu/microcode) + cleanups.
33 * 1.04 06 Jun 2000, Simon Trimmer <simon@veritas.com>
34 * Added misc device support (now uses both devfs and misc).
35 * Added MICROCODE_IOCFREE ioctl to clear memory.
36 * 1.05 09 Jun 2000, Simon Trimmer <simon@veritas.com>
37 * Messages for error cases (non Intel & no suitable microcode).
38 * 1.06 03 Aug 2000, Tigran Aivazian <tigran@veritas.com>
39 * Removed ->release(). Removed exclusive open and status bitmap.
40 * Added microcode_rwsem to serialize read()/write()/ioctl().
41 * Removed global kernel lock usage.
42 * 1.07 07 Sep 2000, Tigran Aivazian <tigran@veritas.com>
43 * Write 0 to 0x8B msr and then cpuid before reading revision,
44 * so that it works even if there were no update done by the
45 * BIOS. Otherwise, reading from 0x8B gives junk (which happened
46 * to be 0 on my machine which is why it worked even when I
47 * disabled update by the BIOS)
48 * Thanks to Eric W. Biederman <ebiederman@lnxi.com> for the fix.
49 * 1.08 11 Dec 2000, Richard Schaal <richard.schaal@intel.com> and
50 * Tigran Aivazian <tigran@veritas.com>
51 * Intel Pentium 4 processor support and bugfixes.
52 * 1.09 30 Oct 2001, Tigran Aivazian <tigran@veritas.com>
53 * Bugfix for HT (Hyper-Threading) enabled processors
54 * whereby processor resources are shared by all logical processors
55 * in a single CPU package.
56 * 1.10 28 Feb 2002 Asit K Mallick <asit.k.mallick@intel.com> and
57 * Tigran Aivazian <tigran@veritas.com>,
58 * Serialize updates as required on HT processors due to speculative
59 * nature of implementation.
60 * 1.11 22 Mar 2002 Tigran Aivazian <tigran@veritas.com>
61 * Fix the panic when writing zero-length microcode chunk.
Ben Castricumbc4e0f92008-06-10 13:15:12 +020062 * 1.12 29 Sep 2003 Nitin Kamble <nitin.a.kamble@intel.com>,
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * Jun Nakajima <jun.nakajima@intel.com>
64 * Support for the microcode updates in the new format.
65 * 1.13 10 Oct 2003 Tigran Aivazian <tigran@veritas.com>
66 * Removed ->read() method and obsoleted MICROCODE_IOCFREE ioctl
Ben Castricumbc4e0f92008-06-10 13:15:12 +020067 * because we no longer hold a copy of applied microcode
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 * in kernel memory.
69 * 1.14 25 Jun 2004 Tigran Aivazian <tigran@veritas.com>
70 * Fix sigmatch() macro to handle old CPUs with pf == 0.
71 * Thanks to Stuart Swales for pointing out this bug.
72 */
73
74//#define DEBUG /* pr_debug */
Randy Dunlapa9415642006-01-11 12:17:48 -080075#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/kernel.h>
77#include <linux/init.h>
78#include <linux/sched.h>
Arnd Bergmann77149362008-05-20 19:16:16 +020079#include <linux/smp_lock.h>
Dave Jones5cf6c542006-02-28 16:58:53 -080080#include <linux/cpumask.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/module.h>
82#include <linux/slab.h>
83#include <linux/vmalloc.h>
84#include <linux/miscdevice.h>
85#include <linux/spinlock.h>
86#include <linux/mm.h>
Alexey Dobriyan4e950f62007-07-30 02:36:13 +040087#include <linux/fs.h>
Ingo Molnar14cc3e22006-03-26 01:37:14 -080088#include <linux/mutex.h>
Shaohua Lia30a6a22006-09-27 01:50:52 -070089#include <linux/cpu.h>
90#include <linux/firmware.h>
91#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93#include <asm/msr.h>
94#include <asm/uaccess.h>
95#include <asm/processor.h>
96
97MODULE_DESCRIPTION("Intel CPU (IA-32) Microcode Update Driver");
Tigran Aivazian69688262006-12-13 00:35:14 -080098MODULE_AUTHOR("Tigran Aivazian <tigran@aivazian.fsnet.co.uk>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070099MODULE_LICENSE("GPL");
100
Jan Beulichba528f22006-06-23 02:04:19 -0700101#define MICROCODE_VERSION "1.14a"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103#define DEFAULT_UCODE_DATASIZE (2000) /* 2000 bytes */
104#define MC_HEADER_SIZE (sizeof (microcode_header_t)) /* 48 bytes */
105#define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE) /* 2048 bytes */
106#define EXT_HEADER_SIZE (sizeof (struct extended_sigtable)) /* 20 bytes */
107#define EXT_SIGNATURE_SIZE (sizeof (struct extended_signature)) /* 12 bytes */
108#define DWSIZE (sizeof (u32))
109#define get_totalsize(mc) \
110 (((microcode_t *)mc)->hdr.totalsize ? \
111 ((microcode_t *)mc)->hdr.totalsize : DEFAULT_UCODE_TOTALSIZE)
112#define get_datasize(mc) \
113 (((microcode_t *)mc)->hdr.datasize ? \
114 ((microcode_t *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE)
115
116#define sigmatch(s1, s2, p1, p2) \
117 (((s1) == (s2)) && (((p1) & (p2)) || (((p1) == 0) && ((p2) == 0))))
118
119#define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE)
120
121/* serialize access to the physical write to MSR 0x79 */
122static DEFINE_SPINLOCK(microcode_update_lock);
123
124/* no concurrent ->write()s are allowed on /dev/cpu/microcode */
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800125static DEFINE_MUTEX(microcode_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static struct ucode_cpu_info {
Shaohua Li9a3110b2006-09-27 01:50:51 -0700128 int valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 unsigned int sig;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700130 unsigned int pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 unsigned int rev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 microcode_t *mc;
133} ucode_cpu_info[NR_CPUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Shaohua Li9a3110b2006-09-27 01:50:51 -0700135static void collect_cpu_info(int cpu_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Mike Travis92cb7612007-10-19 20:35:04 +0200137 struct cpuinfo_x86 *c = &cpu_data(cpu_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
139 unsigned int val[2];
140
Shaohua Li9a3110b2006-09-27 01:50:51 -0700141 /* We should bind the task to the CPU */
142 BUG_ON(raw_smp_processor_id() != cpu_num);
143 uci->pf = uci->rev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 uci->mc = NULL;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700145 uci->valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
148 cpu_has(c, X86_FEATURE_IA64)) {
Shaohua Li9a3110b2006-09-27 01:50:51 -0700149 printk(KERN_ERR "microcode: CPU%d not a capable Intel "
150 "processor\n", cpu_num);
151 uci->valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 return;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Shaohua Li9a3110b2006-09-27 01:50:51 -0700155 uci->sig = cpuid_eax(0x00000001);
156
157 if ((c->x86_model >= 5) || (c->x86 > 6)) {
158 /* get processor flags from MSR 0x17 */
159 rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
160 uci->pf = 1 << ((val[1] >> 18) & 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
162
163 wrmsr(MSR_IA32_UCODE_REV, 0, 0);
Zachary Amsden245067d2005-09-03 15:56:37 -0700164 /* see notes above for revision 1.07. Apparent chip bug */
Andi Kleen487472b2006-01-11 22:45:27 +0100165 sync_core();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 /* get the current revision from MSR 0x8B */
167 rdmsr(MSR_IA32_UCODE_REV, val[0], uci->rev);
168 pr_debug("microcode: collect_cpu_info : sig=0x%x, pf=0x%x, rev=0x%x\n",
169 uci->sig, uci->pf, uci->rev);
170}
171
Shaohua Li9a3110b2006-09-27 01:50:51 -0700172static inline int microcode_update_match(int cpu_num,
173 microcode_header_t *mc_header, int sig, int pf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
176
Shaohua Li9a3110b2006-09-27 01:50:51 -0700177 if (!sigmatch(sig, uci->sig, pf, uci->pf)
178 || mc_header->rev <= uci->rev)
179 return 0;
180 return 1;
181}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Shaohua Li9a3110b2006-09-27 01:50:51 -0700183static int microcode_sanity_check(void *mc)
184{
185 microcode_header_t *mc_header = mc;
186 struct extended_sigtable *ext_header = NULL;
187 struct extended_signature *ext_sig;
188 unsigned long total_size, data_size, ext_table_size;
189 int sum, orig_sum, ext_sigcount = 0, i;
190
191 total_size = get_totalsize(mc_header);
192 data_size = get_datasize(mc_header);
Shaohua Libd8e39f2006-09-27 01:50:54 -0700193 if (data_size + MC_HEADER_SIZE > total_size) {
Shaohua Li9a3110b2006-09-27 01:50:51 -0700194 printk(KERN_ERR "microcode: error! "
195 "Bad data size in microcode data file\n");
196 return -EINVAL;
197 }
198
199 if (mc_header->ldrver != 1 || mc_header->hdrver != 1) {
200 printk(KERN_ERR "microcode: error! "
201 "Unknown microcode update format\n");
202 return -EINVAL;
203 }
204 ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
205 if (ext_table_size) {
206 if ((ext_table_size < EXT_HEADER_SIZE)
207 || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
208 printk(KERN_ERR "microcode: error! "
209 "Small exttable size in microcode data file\n");
210 return -EINVAL;
Jan Beulichba528f22006-06-23 02:04:19 -0700211 }
Shaohua Li9a3110b2006-09-27 01:50:51 -0700212 ext_header = mc + MC_HEADER_SIZE + data_size;
213 if (ext_table_size != exttable_size(ext_header)) {
214 printk(KERN_ERR "microcode: error! "
215 "Bad exttable size in microcode data file\n");
216 return -EFAULT;
Jan Beulichba528f22006-06-23 02:04:19 -0700217 }
Shaohua Li9a3110b2006-09-27 01:50:51 -0700218 ext_sigcount = ext_header->count;
219 }
220
221 /* check extended table checksum */
222 if (ext_table_size) {
223 int ext_table_sum = 0;
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700224 int *ext_tablep = (int *)ext_header;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700225
226 i = ext_table_size / DWSIZE;
227 while (i--)
228 ext_table_sum += ext_tablep[i];
229 if (ext_table_sum) {
230 printk(KERN_WARNING "microcode: aborting, "
231 "bad extended signature table checksum\n");
232 return -EINVAL;
Jan Beulichba528f22006-06-23 02:04:19 -0700233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
Shaohua Li9a3110b2006-09-27 01:50:51 -0700235
236 /* calculate the checksum */
237 orig_sum = 0;
238 i = (MC_HEADER_SIZE + data_size) / DWSIZE;
239 while (i--)
240 orig_sum += ((int *)mc)[i];
241 if (orig_sum) {
242 printk(KERN_ERR "microcode: aborting, bad checksum\n");
243 return -EINVAL;
244 }
245 if (!ext_table_size)
246 return 0;
247 /* check extended signature checksum */
248 for (i = 0; i < ext_sigcount; i++) {
Jan Engelhardtade1af72008-01-30 13:33:23 +0100249 ext_sig = (void *)ext_header + EXT_HEADER_SIZE +
250 EXT_SIGNATURE_SIZE * i;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700251 sum = orig_sum
252 - (mc_header->sig + mc_header->pf + mc_header->cksum)
253 + (ext_sig->sig + ext_sig->pf + ext_sig->cksum);
254 if (sum) {
255 printk(KERN_ERR "microcode: aborting, bad checksum\n");
256 return -EINVAL;
257 }
258 }
259 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
Shaohua Li9a3110b2006-09-27 01:50:51 -0700262/*
263 * return 0 - no update found
264 * return 1 - found update
265 * return < 0 - error
266 */
267static int get_maching_microcode(void *mc, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Shaohua Li9a3110b2006-09-27 01:50:51 -0700269 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
270 microcode_header_t *mc_header = mc;
271 struct extended_sigtable *ext_header;
272 unsigned long total_size = get_totalsize(mc_header);
273 int ext_sigcount, i;
274 struct extended_signature *ext_sig;
275 void *new_mc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Shaohua Li9a3110b2006-09-27 01:50:51 -0700277 if (microcode_update_match(cpu, mc_header,
278 mc_header->sig, mc_header->pf))
279 goto find;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Shaohua Li9a3110b2006-09-27 01:50:51 -0700281 if (total_size <= get_datasize(mc_header) + MC_HEADER_SIZE)
282 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Jan Engelhardtade1af72008-01-30 13:33:23 +0100284 ext_header = mc + get_datasize(mc_header) + MC_HEADER_SIZE;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700285 ext_sigcount = ext_header->count;
Jan Engelhardtade1af72008-01-30 13:33:23 +0100286 ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700287 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:
Ben Castricumfe176de2008-03-27 20:52:35 +0100295 pr_debug("microcode: CPU%d found a matching microcode update with"
Shaohua Li9a3110b2006-09-27 01:50:51 -0700296 " 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 */
Ben Castricumbc4e0f92008-06-10 13:15:12 +0200325 spin_lock_irqsave(&microcode_update_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 /* write microcode via MSR 0x79 */
328 wrmsr(MSR_IA32_UCODE_WRITE,
Ben Castricumbc4e0f92008-06-10 13:15:12 +0200329 (unsigned long) uci->mc->bits,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 (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) {
Ben Castricumfe176de2008-03-27 20:52:35 +0100341 printk(KERN_ERR "microcode: CPU%d update from revision "
Shaohua Li9a3110b2006-09-27 01:50:51 -0700342 "0x%x to 0x%x failed\n", cpu_num, uci->rev, val[1]);
343 return;
344 }
Ben Castricumfe176de2008-03-27 20:52:35 +0100345 printk(KERN_INFO "microcode: CPU%d updated from revision "
Ben Castricumbc4e0f92008-06-10 13:15:12 +0200346 "0x%x to 0x%x, date = %08x \n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 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);
Shaohua Libd8e39f2006-09-27 01:50:54 -0700368 if (offset + total_size > user_buffer_size) {
Shaohua Li9a3110b2006-09-27 01:50:51 -0700369 printk(KERN_ERR "microcode: error! Bad total size in microcode "
370 "data file\n");
371 return -EINVAL;
372 }
373 *mc = vmalloc(total_size);
374 if (!*mc)
375 return -ENOMEM;
376 if (copy_from_user(*mc, user_buffer + offset, total_size)) {
377 printk(KERN_ERR "microcode: error! Can not read user data\n");
378 vfree(*mc);
379 return -EFAULT;
380 }
381 return offset + total_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
384static int do_microcode_update (void)
385{
Shaohua Li9a3110b2006-09-27 01:50:51 -0700386 long cursor = 0;
387 int error = 0;
Andi Kleen2ba1ff22007-02-13 13:26:25 +0100388 void *new_mc = NULL;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700389 int cpu;
390 cpumask_t old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Shaohua Li9a3110b2006-09-27 01:50:51 -0700392 old = current->cpus_allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Shaohua Li9a3110b2006-09-27 01:50:51 -0700394 while ((cursor = get_next_ucode(&new_mc, cursor)) > 0) {
395 error = microcode_sanity_check(new_mc);
396 if (error)
397 goto out;
398 /*
399 * It's possible the data file has multiple matching ucode,
400 * lets keep searching till the latest version
401 */
402 for_each_online_cpu(cpu) {
403 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Shaohua Li9a3110b2006-09-27 01:50:51 -0700405 if (!uci->valid)
406 continue;
Mike Travis0bc3cc02008-07-24 18:21:31 -0700407 set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
Shaohua Li9a3110b2006-09-27 01:50:51 -0700408 error = get_maching_microcode(new_mc, cpu);
409 if (error < 0)
410 goto out;
411 if (error == 1)
412 apply_microcode(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
Shaohua Li9a3110b2006-09-27 01:50:51 -0700414 vfree(new_mc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416out:
Shaohua Li9a3110b2006-09-27 01:50:51 -0700417 if (cursor > 0)
418 vfree(new_mc);
419 if (cursor < 0)
420 error = cursor;
Mike Travisfc0e4742008-04-04 18:11:05 -0700421 set_cpus_allowed_ptr(current, &old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return error;
423}
424
Shaohua Li9a3110b2006-09-27 01:50:51 -0700425static int microcode_open (struct inode *unused1, struct file *unused2)
426{
Arnd Bergmann77149362008-05-20 19:16:16 +0200427 cycle_kernel_lock();
Shaohua Li9a3110b2006-09-27 01:50:51 -0700428 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if ((len >> PAGE_SHIFT) > num_physpages) {
436 printk(KERN_ERR "microcode: too much data (max %ld pages)\n", num_physpages);
437 return -EINVAL;
438 }
439
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100440 get_online_cpus();
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800441 mutex_lock(&microcode_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 user_buffer = (void __user *) buf;
444 user_buffer_size = (int) len;
445
446 ret = do_microcode_update();
447 if (!ret)
448 ret = (ssize_t)len;
449
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800450 mutex_unlock(&microcode_mutex);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100451 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 return ret;
454}
455
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800456static const struct file_operations microcode_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 .owner = THIS_MODULE,
458 .write = microcode_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 .open = microcode_open,
460};
461
462static struct miscdevice microcode_dev = {
463 .minor = MICROCODE_MINOR,
464 .name = "microcode",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 .fops = &microcode_fops,
466};
467
Shaohua Li9a3110b2006-09-27 01:50:51 -0700468static int __init microcode_dev_init (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 int error;
471
472 error = misc_register(&microcode_dev);
473 if (error) {
474 printk(KERN_ERR
475 "microcode: can't misc_register on minor=%d\n",
476 MICROCODE_MINOR);
477 return error;
478 }
479
Shaohua Li9a3110b2006-09-27 01:50:51 -0700480 return 0;
481}
482
Sam Ravnborgf8281a22007-06-01 00:47:10 -0700483static void microcode_dev_exit (void)
Shaohua Li9a3110b2006-09-27 01:50:51 -0700484{
485 misc_deregister(&microcode_dev);
486}
487
488MODULE_ALIAS_MISCDEV(MICROCODE_MINOR);
489#else
490#define microcode_dev_init() 0
491#define microcode_dev_exit() do { } while(0)
492#endif
493
Greg Kroah-Hartmana13b04a2008-05-29 10:05:08 -0700494static long get_next_ucode_from_buffer(void **mc, const u8 *buf,
Shaohua Lia30a6a22006-09-27 01:50:52 -0700495 unsigned long size, long offset)
496{
497 microcode_header_t *mc_header;
498 unsigned long total_size;
499
500 /* No more data */
501 if (offset >= size)
502 return 0;
503 mc_header = (microcode_header_t *)(buf + offset);
504 total_size = get_totalsize(mc_header);
505
Shaohua Libd8e39f2006-09-27 01:50:54 -0700506 if (offset + total_size > size) {
Shaohua Lia30a6a22006-09-27 01:50:52 -0700507 printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
508 return -EINVAL;
509 }
510
511 *mc = vmalloc(total_size);
512 if (!*mc) {
513 printk(KERN_ERR "microcode: error! Can not allocate memory\n");
514 return -ENOMEM;
515 }
516 memcpy(*mc, buf + offset, total_size);
517 return offset + total_size;
518}
519
520/* fake device for request_firmware */
521static struct platform_device *microcode_pdev;
522
523static int cpu_request_microcode(int cpu)
524{
525 char name[30];
Mike Travis92cb7612007-10-19 20:35:04 +0200526 struct cpuinfo_x86 *c = &cpu_data(cpu);
Shaohua Lia30a6a22006-09-27 01:50:52 -0700527 const struct firmware *firmware;
Greg Kroah-Hartmana13b04a2008-05-29 10:05:08 -0700528 const u8 *buf;
Shaohua Lia30a6a22006-09-27 01:50:52 -0700529 unsigned long size;
530 long offset = 0;
531 int error;
532 void *mc;
533
534 /* We should bind the task to the CPU */
535 BUG_ON(cpu != raw_smp_processor_id());
536 sprintf(name,"intel-ucode/%02x-%02x-%02x",
537 c->x86, c->x86_model, c->x86_mask);
538 error = request_firmware(&firmware, name, &microcode_pdev->dev);
539 if (error) {
Ben Castricumbc4e0f92008-06-10 13:15:12 +0200540 pr_debug("microcode: data file %s load failed\n", name);
Shaohua Lia30a6a22006-09-27 01:50:52 -0700541 return error;
542 }
Jan Engelhardtade1af72008-01-30 13:33:23 +0100543 buf = firmware->data;
Shaohua Lia30a6a22006-09-27 01:50:52 -0700544 size = firmware->size;
545 while ((offset = get_next_ucode_from_buffer(&mc, buf, size, offset))
546 > 0) {
547 error = microcode_sanity_check(mc);
548 if (error)
549 break;
550 error = get_maching_microcode(mc, cpu);
551 if (error < 0)
552 break;
553 /*
554 * It's possible the data file has multiple matching ucode,
555 * lets keep searching till the latest version
556 */
557 if (error == 1) {
558 apply_microcode(cpu);
559 error = 0;
560 }
561 vfree(mc);
562 }
563 if (offset > 0)
564 vfree(mc);
565 if (offset < 0)
566 error = offset;
567 release_firmware(firmware);
568
569 return error;
570}
571
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700572static int apply_microcode_check_cpu(int cpu)
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700573{
Mike Travis92cb7612007-10-19 20:35:04 +0200574 struct cpuinfo_x86 *c = &cpu_data(cpu);
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700575 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
576 cpumask_t old;
577 unsigned int val[2];
578 int err = 0;
579
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700580 /* Check if the microcode is available */
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700581 if (!uci->mc)
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700582 return 0;
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700583
584 old = current->cpus_allowed;
Mike Travis0bc3cc02008-07-24 18:21:31 -0700585 set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700586
587 /* Check if the microcode we have in memory matches the CPU */
588 if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
589 cpu_has(c, X86_FEATURE_IA64) || uci->sig != cpuid_eax(0x00000001))
590 err = -EINVAL;
591
592 if (!err && ((c->x86_model >= 5) || (c->x86 > 6))) {
593 /* get processor flags from MSR 0x17 */
594 rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
595 if (uci->pf != (1 << ((val[1] >> 18) & 7)))
596 err = -EINVAL;
597 }
598
599 if (!err) {
600 wrmsr(MSR_IA32_UCODE_REV, 0, 0);
601 /* see notes above for revision 1.07. Apparent chip bug */
602 sync_core();
603 /* get the current revision from MSR 0x8B */
604 rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
605 if (uci->rev != val[1])
606 err = -EINVAL;
607 }
608
609 if (!err)
610 apply_microcode(cpu);
611 else
612 printk(KERN_ERR "microcode: Could not apply microcode to CPU%d:"
613 " sig=0x%x, pf=0x%x, rev=0x%x\n",
614 cpu, uci->sig, uci->pf, uci->rev);
615
Mike Travisfc0e4742008-04-04 18:11:05 -0700616 set_cpus_allowed_ptr(current, &old);
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700617 return err;
618}
619
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700620static void microcode_init_cpu(int cpu, int resume)
Shaohua Lia30a6a22006-09-27 01:50:52 -0700621{
622 cpumask_t old;
623 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
624
625 old = current->cpus_allowed;
626
Mike Travis0bc3cc02008-07-24 18:21:31 -0700627 set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
Shaohua Lia30a6a22006-09-27 01:50:52 -0700628 mutex_lock(&microcode_mutex);
629 collect_cpu_info(cpu);
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700630 if (uci->valid && system_state == SYSTEM_RUNNING && !resume)
Shaohua Lia30a6a22006-09-27 01:50:52 -0700631 cpu_request_microcode(cpu);
632 mutex_unlock(&microcode_mutex);
Mike Travisfc0e4742008-04-04 18:11:05 -0700633 set_cpus_allowed_ptr(current, &old);
Shaohua Lia30a6a22006-09-27 01:50:52 -0700634}
635
636static void microcode_fini_cpu(int cpu)
637{
638 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
639
640 mutex_lock(&microcode_mutex);
641 uci->valid = 0;
642 vfree(uci->mc);
643 uci->mc = NULL;
644 mutex_unlock(&microcode_mutex);
645}
646
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200647static ssize_t reload_store(struct sys_device *dev,
648 struct sysdev_attribute *attr,
649 const char *buf, size_t sz)
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700650{
651 struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
652 char *end;
653 unsigned long val = simple_strtoul(buf, &end, 0);
654 int err = 0;
655 int cpu = dev->id;
656
657 if (end == buf)
658 return -EINVAL;
659 if (val == 1) {
Mike Travis0bc3cc02008-07-24 18:21:31 -0700660 cpumask_t old = current->cpus_allowed;
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700661
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100662 get_online_cpus();
Mike Travis0bc3cc02008-07-24 18:21:31 -0700663 set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700664
665 mutex_lock(&microcode_mutex);
666 if (uci->valid)
667 err = cpu_request_microcode(cpu);
668 mutex_unlock(&microcode_mutex);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100669 put_online_cpus();
Mike Travisfc0e4742008-04-04 18:11:05 -0700670 set_cpus_allowed_ptr(current, &old);
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700671 }
672 if (err)
673 return err;
674 return sz;
675}
676
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200677static ssize_t version_show(struct sys_device *dev,
678 struct sysdev_attribute *attr, char *buf)
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700679{
680 struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
681
682 return sprintf(buf, "0x%x\n", uci->rev);
683}
684
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200685static ssize_t pf_show(struct sys_device *dev,
686 struct sysdev_attribute *attr, char *buf)
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700687{
688 struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
689
690 return sprintf(buf, "0x%x\n", uci->pf);
691}
692
693static SYSDEV_ATTR(reload, 0200, NULL, reload_store);
694static SYSDEV_ATTR(version, 0400, version_show, NULL);
695static SYSDEV_ATTR(processor_flags, 0400, pf_show, NULL);
696
697static struct attribute *mc_default_attrs[] = {
698 &attr_reload.attr,
699 &attr_version.attr,
700 &attr_processor_flags.attr,
701 NULL
702};
703
704static struct attribute_group mc_attr_group = {
705 .attrs = mc_default_attrs,
706 .name = "microcode",
707};
708
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700709static int __mc_sysdev_add(struct sys_device *sys_dev, int resume)
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700710{
Jeff Garzik2e3ad8a2006-10-11 01:21:53 -0700711 int err, cpu = sys_dev->id;
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700712 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
713
714 if (!cpu_online(cpu))
715 return 0;
Jeff Garzik2e3ad8a2006-10-11 01:21:53 -0700716
Ben Castricumfe176de2008-03-27 20:52:35 +0100717 pr_debug("microcode: CPU%d added\n", cpu);
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700718 memset(uci, 0, sizeof(*uci));
Jeff Garzik2e3ad8a2006-10-11 01:21:53 -0700719
720 err = sysfs_create_group(&sys_dev->kobj, &mc_attr_group);
721 if (err)
722 return err;
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700723
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700724 microcode_init_cpu(cpu, resume);
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700725
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700726 return 0;
727}
728
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700729static int mc_sysdev_add(struct sys_device *sys_dev)
730{
731 return __mc_sysdev_add(sys_dev, 0);
732}
733
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700734static int mc_sysdev_remove(struct sys_device *sys_dev)
735{
736 int cpu = sys_dev->id;
737
738 if (!cpu_online(cpu))
739 return 0;
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700740
Ben Castricumfe176de2008-03-27 20:52:35 +0100741 pr_debug("microcode: CPU%d removed\n", cpu);
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700742 microcode_fini_cpu(cpu);
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700743 sysfs_remove_group(&sys_dev->kobj, &mc_attr_group);
744 return 0;
745}
746
747static int mc_sysdev_resume(struct sys_device *dev)
748{
749 int cpu = dev->id;
750
751 if (!cpu_online(cpu))
752 return 0;
Ben Castricumfe176de2008-03-27 20:52:35 +0100753 pr_debug("microcode: CPU%d resumed\n", cpu);
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700754 /* only CPU 0 will apply ucode here */
755 apply_microcode(0);
756 return 0;
757}
758
759static struct sysdev_driver mc_sysdev_driver = {
760 .add = mc_sysdev_add,
761 .remove = mc_sysdev_remove,
762 .resume = mc_sysdev_resume,
763};
764
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700765static __cpuinit int
766mc_cpu_callback(struct notifier_block *nb, unsigned long action, void *hcpu)
767{
768 unsigned int cpu = (unsigned long)hcpu;
769 struct sys_device *sys_dev;
770
771 sys_dev = get_cpu_sysdev(cpu);
772 switch (action) {
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700773 case CPU_UP_CANCELED_FROZEN:
774 /* The CPU refused to come up during a system resume */
775 microcode_fini_cpu(cpu);
776 break;
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700777 case CPU_ONLINE:
778 case CPU_DOWN_FAILED:
779 mc_sysdev_add(sys_dev);
780 break;
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700781 case CPU_ONLINE_FROZEN:
782 /* System-wide resume is in progress, try to apply microcode */
783 if (apply_microcode_check_cpu(cpu)) {
784 /* The application of microcode failed */
785 microcode_fini_cpu(cpu);
786 __mc_sysdev_add(sys_dev, 1);
787 break;
788 }
789 case CPU_DOWN_FAILED_FROZEN:
790 if (sysfs_create_group(&sys_dev->kobj, &mc_attr_group))
Ben Castricumfe176de2008-03-27 20:52:35 +0100791 printk(KERN_ERR "microcode: Failed to create the sysfs "
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700792 "group for CPU%d\n", cpu);
793 break;
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700794 case CPU_DOWN_PREPARE:
795 mc_sysdev_remove(sys_dev);
796 break;
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700797 case CPU_DOWN_PREPARE_FROZEN:
798 /* Suspend is in progress, only remove the interface */
799 sysfs_remove_group(&sys_dev->kobj, &mc_attr_group);
800 break;
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700801 }
802 return NOTIFY_OK;
803}
804
Sam Ravnborgc72258c2008-02-01 17:49:42 +0100805static struct notifier_block __refdata mc_cpu_notifier = {
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700806 .notifier_call = mc_cpu_callback,
807};
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700808
Shaohua Li9a3110b2006-09-27 01:50:51 -0700809static int __init microcode_init (void)
810{
811 int error;
812
Ben Castricumbc4e0f92008-06-10 13:15:12 +0200813 printk(KERN_INFO
814 "IA-32 Microcode Update Driver: v" MICROCODE_VERSION " <tigran@aivazian.fsnet.co.uk>\n");
815
Shaohua Li9a3110b2006-09-27 01:50:51 -0700816 error = microcode_dev_init();
817 if (error)
818 return error;
Shaohua Lia30a6a22006-09-27 01:50:52 -0700819 microcode_pdev = platform_device_register_simple("microcode", -1,
820 NULL, 0);
821 if (IS_ERR(microcode_pdev)) {
822 microcode_dev_exit();
823 return PTR_ERR(microcode_pdev);
824 }
Shaohua Li9a3110b2006-09-27 01:50:51 -0700825
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100826 get_online_cpus();
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700827 error = sysdev_driver_register(&cpu_sysdev_class, &mc_sysdev_driver);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100828 put_online_cpus();
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700829 if (error) {
830 microcode_dev_exit();
831 platform_device_unregister(microcode_pdev);
832 return error;
833 }
834
835 register_hotcpu_notifier(&mc_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return 0;
837}
838
839static void __exit microcode_exit (void)
840{
Shaohua Li9a3110b2006-09-27 01:50:51 -0700841 microcode_dev_exit();
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700842
843 unregister_hotcpu_notifier(&mc_cpu_notifier);
844
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100845 get_online_cpus();
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700846 sysdev_driver_unregister(&cpu_sysdev_class, &mc_sysdev_driver);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100847 put_online_cpus();
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700848
Shaohua Lia30a6a22006-09-27 01:50:52 -0700849 platform_device_unregister(microcode_pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
852module_init(microcode_init)
853module_exit(microcode_exit)