blob: 0d654bd32928adb6dc2b5c13b2c91f9ddb2c5e5d [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>
Peter Oruba9a56a0f2008-07-28 18:44:13 +020096#include <asm/microcode.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98MODULE_DESCRIPTION("Intel CPU (IA-32) Microcode Update Driver");
Tigran Aivazian69688262006-12-13 00:35:14 -080099MODULE_AUTHOR("Tigran Aivazian <tigran@aivazian.fsnet.co.uk>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100MODULE_LICENSE("GPL");
101
Jan Beulichba528f22006-06-23 02:04:19 -0700102#define MICROCODE_VERSION "1.14a"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104#define DEFAULT_UCODE_DATASIZE (2000) /* 2000 bytes */
105#define MC_HEADER_SIZE (sizeof (microcode_header_t)) /* 48 bytes */
106#define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE) /* 2048 bytes */
107#define EXT_HEADER_SIZE (sizeof (struct extended_sigtable)) /* 20 bytes */
108#define EXT_SIGNATURE_SIZE (sizeof (struct extended_signature)) /* 12 bytes */
109#define DWSIZE (sizeof (u32))
110#define get_totalsize(mc) \
111 (((microcode_t *)mc)->hdr.totalsize ? \
112 ((microcode_t *)mc)->hdr.totalsize : DEFAULT_UCODE_TOTALSIZE)
113#define get_datasize(mc) \
114 (((microcode_t *)mc)->hdr.datasize ? \
115 ((microcode_t *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE)
116
117#define sigmatch(s1, s2, p1, p2) \
118 (((s1) == (s2)) && (((p1) & (p2)) || (((p1) == 0) && ((p2) == 0))))
119
120#define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE)
121
122/* serialize access to the physical write to MSR 0x79 */
123static DEFINE_SPINLOCK(microcode_update_lock);
124
125/* no concurrent ->write()s are allowed on /dev/cpu/microcode */
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800126static DEFINE_MUTEX(microcode_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128static struct ucode_cpu_info {
Shaohua Li9a3110b2006-09-27 01:50:51 -0700129 int valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 unsigned int sig;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700131 unsigned int pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 unsigned int rev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 microcode_t *mc;
134} ucode_cpu_info[NR_CPUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Shaohua Li9a3110b2006-09-27 01:50:51 -0700136static void collect_cpu_info(int cpu_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Mike Travis92cb7612007-10-19 20:35:04 +0200138 struct cpuinfo_x86 *c = &cpu_data(cpu_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
140 unsigned int val[2];
141
Shaohua Li9a3110b2006-09-27 01:50:51 -0700142 /* We should bind the task to the CPU */
143 BUG_ON(raw_smp_processor_id() != cpu_num);
144 uci->pf = uci->rev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 uci->mc = NULL;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700146 uci->valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
149 cpu_has(c, X86_FEATURE_IA64)) {
Shaohua Li9a3110b2006-09-27 01:50:51 -0700150 printk(KERN_ERR "microcode: CPU%d not a capable Intel "
151 "processor\n", cpu_num);
152 uci->valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Shaohua Li9a3110b2006-09-27 01:50:51 -0700156 uci->sig = cpuid_eax(0x00000001);
157
158 if ((c->x86_model >= 5) || (c->x86 > 6)) {
159 /* get processor flags from MSR 0x17 */
160 rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
161 uci->pf = 1 << ((val[1] >> 18) & 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
163
164 wrmsr(MSR_IA32_UCODE_REV, 0, 0);
Zachary Amsden245067d2005-09-03 15:56:37 -0700165 /* see notes above for revision 1.07. Apparent chip bug */
Andi Kleen487472b2006-01-11 22:45:27 +0100166 sync_core();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 /* get the current revision from MSR 0x8B */
168 rdmsr(MSR_IA32_UCODE_REV, val[0], uci->rev);
169 pr_debug("microcode: collect_cpu_info : sig=0x%x, pf=0x%x, rev=0x%x\n",
170 uci->sig, uci->pf, uci->rev);
171}
172
Shaohua Li9a3110b2006-09-27 01:50:51 -0700173static inline int microcode_update_match(int cpu_num,
174 microcode_header_t *mc_header, int sig, int pf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
177
Shaohua Li9a3110b2006-09-27 01:50:51 -0700178 if (!sigmatch(sig, uci->sig, pf, uci->pf)
179 || mc_header->rev <= uci->rev)
180 return 0;
181 return 1;
182}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Shaohua Li9a3110b2006-09-27 01:50:51 -0700184static int microcode_sanity_check(void *mc)
185{
186 microcode_header_t *mc_header = mc;
187 struct extended_sigtable *ext_header = NULL;
188 struct extended_signature *ext_sig;
189 unsigned long total_size, data_size, ext_table_size;
190 int sum, orig_sum, ext_sigcount = 0, i;
191
192 total_size = get_totalsize(mc_header);
193 data_size = get_datasize(mc_header);
Shaohua Libd8e39f2006-09-27 01:50:54 -0700194 if (data_size + MC_HEADER_SIZE > total_size) {
Shaohua Li9a3110b2006-09-27 01:50:51 -0700195 printk(KERN_ERR "microcode: error! "
196 "Bad data size in microcode data file\n");
197 return -EINVAL;
198 }
199
200 if (mc_header->ldrver != 1 || mc_header->hdrver != 1) {
201 printk(KERN_ERR "microcode: error! "
202 "Unknown microcode update format\n");
203 return -EINVAL;
204 }
205 ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
206 if (ext_table_size) {
207 if ((ext_table_size < EXT_HEADER_SIZE)
208 || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
209 printk(KERN_ERR "microcode: error! "
210 "Small exttable size in microcode data file\n");
211 return -EINVAL;
Jan Beulichba528f22006-06-23 02:04:19 -0700212 }
Shaohua Li9a3110b2006-09-27 01:50:51 -0700213 ext_header = mc + MC_HEADER_SIZE + data_size;
214 if (ext_table_size != exttable_size(ext_header)) {
215 printk(KERN_ERR "microcode: error! "
216 "Bad exttable size in microcode data file\n");
217 return -EFAULT;
Jan Beulichba528f22006-06-23 02:04:19 -0700218 }
Shaohua Li9a3110b2006-09-27 01:50:51 -0700219 ext_sigcount = ext_header->count;
220 }
221
222 /* check extended table checksum */
223 if (ext_table_size) {
224 int ext_table_sum = 0;
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700225 int *ext_tablep = (int *)ext_header;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700226
227 i = ext_table_size / DWSIZE;
228 while (i--)
229 ext_table_sum += ext_tablep[i];
230 if (ext_table_sum) {
231 printk(KERN_WARNING "microcode: aborting, "
232 "bad extended signature table checksum\n");
233 return -EINVAL;
Jan Beulichba528f22006-06-23 02:04:19 -0700234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
Shaohua Li9a3110b2006-09-27 01:50:51 -0700236
237 /* calculate the checksum */
238 orig_sum = 0;
239 i = (MC_HEADER_SIZE + data_size) / DWSIZE;
240 while (i--)
241 orig_sum += ((int *)mc)[i];
242 if (orig_sum) {
243 printk(KERN_ERR "microcode: aborting, bad checksum\n");
244 return -EINVAL;
245 }
246 if (!ext_table_size)
247 return 0;
248 /* check extended signature checksum */
249 for (i = 0; i < ext_sigcount; i++) {
Jan Engelhardtade1af72008-01-30 13:33:23 +0100250 ext_sig = (void *)ext_header + EXT_HEADER_SIZE +
251 EXT_SIGNATURE_SIZE * i;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700252 sum = orig_sum
253 - (mc_header->sig + mc_header->pf + mc_header->cksum)
254 + (ext_sig->sig + ext_sig->pf + ext_sig->cksum);
255 if (sum) {
256 printk(KERN_ERR "microcode: aborting, bad checksum\n");
257 return -EINVAL;
258 }
259 }
260 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Shaohua Li9a3110b2006-09-27 01:50:51 -0700263/*
264 * return 0 - no update found
265 * return 1 - found update
266 * return < 0 - error
267 */
268static int get_maching_microcode(void *mc, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Shaohua Li9a3110b2006-09-27 01:50:51 -0700270 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
271 microcode_header_t *mc_header = mc;
272 struct extended_sigtable *ext_header;
273 unsigned long total_size = get_totalsize(mc_header);
274 int ext_sigcount, i;
275 struct extended_signature *ext_sig;
276 void *new_mc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Shaohua Li9a3110b2006-09-27 01:50:51 -0700278 if (microcode_update_match(cpu, mc_header,
279 mc_header->sig, mc_header->pf))
280 goto find;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Shaohua Li9a3110b2006-09-27 01:50:51 -0700282 if (total_size <= get_datasize(mc_header) + MC_HEADER_SIZE)
283 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Jan Engelhardtade1af72008-01-30 13:33:23 +0100285 ext_header = mc + get_datasize(mc_header) + MC_HEADER_SIZE;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700286 ext_sigcount = ext_header->count;
Jan Engelhardtade1af72008-01-30 13:33:23 +0100287 ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700288 for (i = 0; i < ext_sigcount; i++) {
289 if (microcode_update_match(cpu, mc_header,
290 ext_sig->sig, ext_sig->pf))
291 goto find;
292 ext_sig++;
293 }
294 return 0;
295find:
Ben Castricumfe176de2008-03-27 20:52:35 +0100296 pr_debug("microcode: CPU%d found a matching microcode update with"
Shaohua Li9a3110b2006-09-27 01:50:51 -0700297 " version 0x%x (current=0x%x)\n", cpu, mc_header->rev,uci->rev);
298 new_mc = vmalloc(total_size);
299 if (!new_mc) {
300 printk(KERN_ERR "microcode: error! Can not allocate memory\n");
301 return -ENOMEM;
302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Shaohua Li9a3110b2006-09-27 01:50:51 -0700304 /* free previous update file */
305 vfree(uci->mc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Shaohua Li9a3110b2006-09-27 01:50:51 -0700307 memcpy(new_mc, mc, total_size);
308 uci->mc = new_mc;
309 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
Shaohua Li9a3110b2006-09-27 01:50:51 -0700312static void apply_microcode(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 unsigned long flags;
315 unsigned int val[2];
Shaohua Li9a3110b2006-09-27 01:50:51 -0700316 int cpu_num = raw_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
318
Shaohua Li9a3110b2006-09-27 01:50:51 -0700319 /* We should bind the task to the CPU */
320 BUG_ON(cpu_num != cpu);
321
322 if (uci->mc == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 /* serialize access to the physical write to MSR 0x79 */
Ben Castricumbc4e0f92008-06-10 13:15:12 +0200326 spin_lock_irqsave(&microcode_update_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 /* write microcode via MSR 0x79 */
329 wrmsr(MSR_IA32_UCODE_WRITE,
Ben Castricumbc4e0f92008-06-10 13:15:12 +0200330 (unsigned long) uci->mc->bits,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 (unsigned long) uci->mc->bits >> 16 >> 16);
332 wrmsr(MSR_IA32_UCODE_REV, 0, 0);
333
Zachary Amsden245067d2005-09-03 15:56:37 -0700334 /* see notes above for revision 1.07. Apparent chip bug */
Andi Kleen487472b2006-01-11 22:45:27 +0100335 sync_core();
Zachary Amsden245067d2005-09-03 15:56:37 -0700336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 /* get the current revision from MSR 0x8B */
338 rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 spin_unlock_irqrestore(&microcode_update_lock, flags);
Shaohua Li9a3110b2006-09-27 01:50:51 -0700341 if (val[1] != uci->mc->hdr.rev) {
Ben Castricumfe176de2008-03-27 20:52:35 +0100342 printk(KERN_ERR "microcode: CPU%d update from revision "
Shaohua Li9a3110b2006-09-27 01:50:51 -0700343 "0x%x to 0x%x failed\n", cpu_num, uci->rev, val[1]);
344 return;
345 }
Ben Castricumfe176de2008-03-27 20:52:35 +0100346 printk(KERN_INFO "microcode: CPU%d updated from revision "
Ben Castricumbc4e0f92008-06-10 13:15:12 +0200347 "0x%x to 0x%x, date = %08x \n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 cpu_num, uci->rev, val[1], uci->mc->hdr.date);
Shaohua Li9a3110b2006-09-27 01:50:51 -0700349 uci->rev = val[1];
350}
351
352#ifdef CONFIG_MICROCODE_OLD_INTERFACE
353static void __user *user_buffer; /* user area microcode data buffer */
354static unsigned int user_buffer_size; /* it's size */
355
356static long get_next_ucode(void **mc, long offset)
357{
358 microcode_header_t mc_header;
359 unsigned long total_size;
360
361 /* No more data */
362 if (offset >= user_buffer_size)
363 return 0;
364 if (copy_from_user(&mc_header, user_buffer + offset, MC_HEADER_SIZE)) {
365 printk(KERN_ERR "microcode: error! Can not read user data\n");
366 return -EFAULT;
367 }
368 total_size = get_totalsize(&mc_header);
Shaohua Libd8e39f2006-09-27 01:50:54 -0700369 if (offset + total_size > user_buffer_size) {
Shaohua Li9a3110b2006-09-27 01:50:51 -0700370 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;
Andi Kleen2ba1ff22007-02-13 13:26:25 +0100389 void *new_mc = NULL;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700390 int cpu;
391 cpumask_t old;
Mike Travis65c01182008-07-15 14:14:30 -0700392 cpumask_of_cpu_ptr_declare(newmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Shaohua Li9a3110b2006-09-27 01:50:51 -0700394 old = current->cpus_allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Shaohua Li9a3110b2006-09-27 01:50:51 -0700396 while ((cursor = get_next_ucode(&new_mc, cursor)) > 0) {
397 error = microcode_sanity_check(new_mc);
398 if (error)
399 goto out;
400 /*
401 * It's possible the data file has multiple matching ucode,
402 * lets keep searching till the latest version
403 */
404 for_each_online_cpu(cpu) {
405 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Shaohua Li9a3110b2006-09-27 01:50:51 -0700407 if (!uci->valid)
408 continue;
Mike Travis65c01182008-07-15 14:14:30 -0700409 cpumask_of_cpu_ptr_next(newmask, cpu);
410 set_cpus_allowed_ptr(current, newmask);
Shaohua Li9a3110b2006-09-27 01:50:51 -0700411 error = get_maching_microcode(new_mc, cpu);
412 if (error < 0)
413 goto out;
414 if (error == 1)
415 apply_microcode(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
Shaohua Li9a3110b2006-09-27 01:50:51 -0700417 vfree(new_mc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419out:
Shaohua Li9a3110b2006-09-27 01:50:51 -0700420 if (cursor > 0)
421 vfree(new_mc);
422 if (cursor < 0)
423 error = cursor;
Mike Travisfc0e4742008-04-04 18:11:05 -0700424 set_cpus_allowed_ptr(current, &old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return error;
426}
427
Shaohua Li9a3110b2006-09-27 01:50:51 -0700428static int microcode_open (struct inode *unused1, struct file *unused2)
429{
Arnd Bergmann77149362008-05-20 19:16:16 +0200430 cycle_kernel_lock();
Shaohua Li9a3110b2006-09-27 01:50:51 -0700431 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
432}
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434static ssize_t microcode_write (struct file *file, const char __user *buf, size_t len, loff_t *ppos)
435{
436 ssize_t ret;
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if ((len >> PAGE_SHIFT) > num_physpages) {
439 printk(KERN_ERR "microcode: too much data (max %ld pages)\n", num_physpages);
440 return -EINVAL;
441 }
442
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100443 get_online_cpus();
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800444 mutex_lock(&microcode_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 user_buffer = (void __user *) buf;
447 user_buffer_size = (int) len;
448
449 ret = do_microcode_update();
450 if (!ret)
451 ret = (ssize_t)len;
452
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800453 mutex_unlock(&microcode_mutex);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100454 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 return ret;
457}
458
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800459static const struct file_operations microcode_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 .owner = THIS_MODULE,
461 .write = microcode_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 .open = microcode_open,
463};
464
465static struct miscdevice microcode_dev = {
466 .minor = MICROCODE_MINOR,
467 .name = "microcode",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 .fops = &microcode_fops,
469};
470
Shaohua Li9a3110b2006-09-27 01:50:51 -0700471static int __init microcode_dev_init (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 int error;
474
475 error = misc_register(&microcode_dev);
476 if (error) {
477 printk(KERN_ERR
478 "microcode: can't misc_register on minor=%d\n",
479 MICROCODE_MINOR);
480 return error;
481 }
482
Shaohua Li9a3110b2006-09-27 01:50:51 -0700483 return 0;
484}
485
Sam Ravnborgf8281a22007-06-01 00:47:10 -0700486static void microcode_dev_exit (void)
Shaohua Li9a3110b2006-09-27 01:50:51 -0700487{
488 misc_deregister(&microcode_dev);
489}
490
491MODULE_ALIAS_MISCDEV(MICROCODE_MINOR);
492#else
493#define microcode_dev_init() 0
494#define microcode_dev_exit() do { } while(0)
495#endif
496
Greg Kroah-Hartmana13b04a2008-05-29 10:05:08 -0700497static long get_next_ucode_from_buffer(void **mc, const u8 *buf,
Shaohua Lia30a6a22006-09-27 01:50:52 -0700498 unsigned long size, long offset)
499{
500 microcode_header_t *mc_header;
501 unsigned long total_size;
502
503 /* No more data */
504 if (offset >= size)
505 return 0;
506 mc_header = (microcode_header_t *)(buf + offset);
507 total_size = get_totalsize(mc_header);
508
Shaohua Libd8e39f2006-09-27 01:50:54 -0700509 if (offset + total_size > size) {
Shaohua Lia30a6a22006-09-27 01:50:52 -0700510 printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
511 return -EINVAL;
512 }
513
514 *mc = vmalloc(total_size);
515 if (!*mc) {
516 printk(KERN_ERR "microcode: error! Can not allocate memory\n");
517 return -ENOMEM;
518 }
519 memcpy(*mc, buf + offset, total_size);
520 return offset + total_size;
521}
522
523/* fake device for request_firmware */
524static struct platform_device *microcode_pdev;
525
526static int cpu_request_microcode(int cpu)
527{
528 char name[30];
Mike Travis92cb7612007-10-19 20:35:04 +0200529 struct cpuinfo_x86 *c = &cpu_data(cpu);
Shaohua Lia30a6a22006-09-27 01:50:52 -0700530 const struct firmware *firmware;
Greg Kroah-Hartmana13b04a2008-05-29 10:05:08 -0700531 const u8 *buf;
Shaohua Lia30a6a22006-09-27 01:50:52 -0700532 unsigned long size;
533 long offset = 0;
534 int error;
535 void *mc;
536
537 /* We should bind the task to the CPU */
538 BUG_ON(cpu != raw_smp_processor_id());
539 sprintf(name,"intel-ucode/%02x-%02x-%02x",
540 c->x86, c->x86_model, c->x86_mask);
541 error = request_firmware(&firmware, name, &microcode_pdev->dev);
542 if (error) {
Ben Castricumbc4e0f92008-06-10 13:15:12 +0200543 pr_debug("microcode: data file %s load failed\n", name);
Shaohua Lia30a6a22006-09-27 01:50:52 -0700544 return error;
545 }
Jan Engelhardtade1af72008-01-30 13:33:23 +0100546 buf = firmware->data;
Shaohua Lia30a6a22006-09-27 01:50:52 -0700547 size = firmware->size;
548 while ((offset = get_next_ucode_from_buffer(&mc, buf, size, offset))
549 > 0) {
550 error = microcode_sanity_check(mc);
551 if (error)
552 break;
553 error = get_maching_microcode(mc, cpu);
554 if (error < 0)
555 break;
556 /*
557 * It's possible the data file has multiple matching ucode,
558 * lets keep searching till the latest version
559 */
560 if (error == 1) {
561 apply_microcode(cpu);
562 error = 0;
563 }
564 vfree(mc);
565 }
566 if (offset > 0)
567 vfree(mc);
568 if (offset < 0)
569 error = offset;
570 release_firmware(firmware);
571
572 return error;
573}
574
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700575static int apply_microcode_check_cpu(int cpu)
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700576{
Mike Travis92cb7612007-10-19 20:35:04 +0200577 struct cpuinfo_x86 *c = &cpu_data(cpu);
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700578 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
579 cpumask_t old;
Mike Travis65c01182008-07-15 14:14:30 -0700580 cpumask_of_cpu_ptr(newmask, cpu);
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700581 unsigned int val[2];
582 int err = 0;
583
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700584 /* Check if the microcode is available */
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700585 if (!uci->mc)
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700586 return 0;
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700587
588 old = current->cpus_allowed;
Mike Travis65c01182008-07-15 14:14:30 -0700589 set_cpus_allowed_ptr(current, newmask);
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700590
591 /* Check if the microcode we have in memory matches the CPU */
592 if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
593 cpu_has(c, X86_FEATURE_IA64) || uci->sig != cpuid_eax(0x00000001))
594 err = -EINVAL;
595
596 if (!err && ((c->x86_model >= 5) || (c->x86 > 6))) {
597 /* get processor flags from MSR 0x17 */
598 rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
599 if (uci->pf != (1 << ((val[1] >> 18) & 7)))
600 err = -EINVAL;
601 }
602
603 if (!err) {
604 wrmsr(MSR_IA32_UCODE_REV, 0, 0);
605 /* see notes above for revision 1.07. Apparent chip bug */
606 sync_core();
607 /* get the current revision from MSR 0x8B */
608 rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
609 if (uci->rev != val[1])
610 err = -EINVAL;
611 }
612
613 if (!err)
614 apply_microcode(cpu);
615 else
616 printk(KERN_ERR "microcode: Could not apply microcode to CPU%d:"
617 " sig=0x%x, pf=0x%x, rev=0x%x\n",
618 cpu, uci->sig, uci->pf, uci->rev);
619
Mike Travisfc0e4742008-04-04 18:11:05 -0700620 set_cpus_allowed_ptr(current, &old);
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700621 return err;
622}
623
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700624static void microcode_init_cpu(int cpu, int resume)
Shaohua Lia30a6a22006-09-27 01:50:52 -0700625{
626 cpumask_t old;
Mike Travis65c01182008-07-15 14:14:30 -0700627 cpumask_of_cpu_ptr(newmask, cpu);
Shaohua Lia30a6a22006-09-27 01:50:52 -0700628 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
629
630 old = current->cpus_allowed;
631
Mike Travis65c01182008-07-15 14:14:30 -0700632 set_cpus_allowed_ptr(current, newmask);
Shaohua Lia30a6a22006-09-27 01:50:52 -0700633 mutex_lock(&microcode_mutex);
634 collect_cpu_info(cpu);
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700635 if (uci->valid && system_state == SYSTEM_RUNNING && !resume)
Shaohua Lia30a6a22006-09-27 01:50:52 -0700636 cpu_request_microcode(cpu);
637 mutex_unlock(&microcode_mutex);
Mike Travisfc0e4742008-04-04 18:11:05 -0700638 set_cpus_allowed_ptr(current, &old);
Shaohua Lia30a6a22006-09-27 01:50:52 -0700639}
640
641static void microcode_fini_cpu(int cpu)
642{
643 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
644
645 mutex_lock(&microcode_mutex);
646 uci->valid = 0;
647 vfree(uci->mc);
648 uci->mc = NULL;
649 mutex_unlock(&microcode_mutex);
650}
651
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200652static ssize_t reload_store(struct sys_device *dev,
653 struct sysdev_attribute *attr,
654 const char *buf, size_t sz)
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700655{
656 struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
657 char *end;
658 unsigned long val = simple_strtoul(buf, &end, 0);
659 int err = 0;
660 int cpu = dev->id;
661
662 if (end == buf)
663 return -EINVAL;
664 if (val == 1) {
665 cpumask_t old;
Mike Travis65c01182008-07-15 14:14:30 -0700666 cpumask_of_cpu_ptr(newmask, cpu);
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700667
668 old = current->cpus_allowed;
669
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100670 get_online_cpus();
Mike Travis65c01182008-07-15 14:14:30 -0700671 set_cpus_allowed_ptr(current, newmask);
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700672
673 mutex_lock(&microcode_mutex);
674 if (uci->valid)
675 err = cpu_request_microcode(cpu);
676 mutex_unlock(&microcode_mutex);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100677 put_online_cpus();
Mike Travisfc0e4742008-04-04 18:11:05 -0700678 set_cpus_allowed_ptr(current, &old);
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700679 }
680 if (err)
681 return err;
682 return sz;
683}
684
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200685static ssize_t version_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->rev);
691}
692
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200693static ssize_t pf_show(struct sys_device *dev,
694 struct sysdev_attribute *attr, char *buf)
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700695{
696 struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
697
698 return sprintf(buf, "0x%x\n", uci->pf);
699}
700
701static SYSDEV_ATTR(reload, 0200, NULL, reload_store);
702static SYSDEV_ATTR(version, 0400, version_show, NULL);
703static SYSDEV_ATTR(processor_flags, 0400, pf_show, NULL);
704
705static struct attribute *mc_default_attrs[] = {
706 &attr_reload.attr,
707 &attr_version.attr,
708 &attr_processor_flags.attr,
709 NULL
710};
711
712static struct attribute_group mc_attr_group = {
713 .attrs = mc_default_attrs,
714 .name = "microcode",
715};
716
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700717static int __mc_sysdev_add(struct sys_device *sys_dev, int resume)
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700718{
Jeff Garzik2e3ad8a2006-10-11 01:21:53 -0700719 int err, cpu = sys_dev->id;
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700720 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
721
722 if (!cpu_online(cpu))
723 return 0;
Jeff Garzik2e3ad8a2006-10-11 01:21:53 -0700724
Ben Castricumfe176de2008-03-27 20:52:35 +0100725 pr_debug("microcode: CPU%d added\n", cpu);
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700726 memset(uci, 0, sizeof(*uci));
Jeff Garzik2e3ad8a2006-10-11 01:21:53 -0700727
728 err = sysfs_create_group(&sys_dev->kobj, &mc_attr_group);
729 if (err)
730 return err;
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700731
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700732 microcode_init_cpu(cpu, resume);
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700733
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700734 return 0;
735}
736
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700737static int mc_sysdev_add(struct sys_device *sys_dev)
738{
739 return __mc_sysdev_add(sys_dev, 0);
740}
741
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700742static int mc_sysdev_remove(struct sys_device *sys_dev)
743{
744 int cpu = sys_dev->id;
745
746 if (!cpu_online(cpu))
747 return 0;
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700748
Ben Castricumfe176de2008-03-27 20:52:35 +0100749 pr_debug("microcode: CPU%d removed\n", cpu);
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700750 microcode_fini_cpu(cpu);
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700751 sysfs_remove_group(&sys_dev->kobj, &mc_attr_group);
752 return 0;
753}
754
755static int mc_sysdev_resume(struct sys_device *dev)
756{
757 int cpu = dev->id;
758
759 if (!cpu_online(cpu))
760 return 0;
Ben Castricumfe176de2008-03-27 20:52:35 +0100761 pr_debug("microcode: CPU%d resumed\n", cpu);
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700762 /* only CPU 0 will apply ucode here */
763 apply_microcode(0);
764 return 0;
765}
766
767static struct sysdev_driver mc_sysdev_driver = {
768 .add = mc_sysdev_add,
769 .remove = mc_sysdev_remove,
770 .resume = mc_sysdev_resume,
771};
772
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700773static __cpuinit int
774mc_cpu_callback(struct notifier_block *nb, unsigned long action, void *hcpu)
775{
776 unsigned int cpu = (unsigned long)hcpu;
777 struct sys_device *sys_dev;
778
779 sys_dev = get_cpu_sysdev(cpu);
780 switch (action) {
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700781 case CPU_UP_CANCELED_FROZEN:
782 /* The CPU refused to come up during a system resume */
783 microcode_fini_cpu(cpu);
784 break;
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700785 case CPU_ONLINE:
786 case CPU_DOWN_FAILED:
787 mc_sysdev_add(sys_dev);
788 break;
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700789 case CPU_ONLINE_FROZEN:
790 /* System-wide resume is in progress, try to apply microcode */
791 if (apply_microcode_check_cpu(cpu)) {
792 /* The application of microcode failed */
793 microcode_fini_cpu(cpu);
794 __mc_sysdev_add(sys_dev, 1);
795 break;
796 }
797 case CPU_DOWN_FAILED_FROZEN:
798 if (sysfs_create_group(&sys_dev->kobj, &mc_attr_group))
Ben Castricumfe176de2008-03-27 20:52:35 +0100799 printk(KERN_ERR "microcode: Failed to create the sysfs "
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700800 "group for CPU%d\n", cpu);
801 break;
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700802 case CPU_DOWN_PREPARE:
803 mc_sysdev_remove(sys_dev);
804 break;
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700805 case CPU_DOWN_PREPARE_FROZEN:
806 /* Suspend is in progress, only remove the interface */
807 sysfs_remove_group(&sys_dev->kobj, &mc_attr_group);
808 break;
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700809 }
810 return NOTIFY_OK;
811}
812
Sam Ravnborgc72258c2008-02-01 17:49:42 +0100813static struct notifier_block __refdata mc_cpu_notifier = {
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700814 .notifier_call = mc_cpu_callback,
815};
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700816
Shaohua Li9a3110b2006-09-27 01:50:51 -0700817static int __init microcode_init (void)
818{
819 int error;
820
Ben Castricumbc4e0f92008-06-10 13:15:12 +0200821 printk(KERN_INFO
822 "IA-32 Microcode Update Driver: v" MICROCODE_VERSION " <tigran@aivazian.fsnet.co.uk>\n");
823
Shaohua Li9a3110b2006-09-27 01:50:51 -0700824 error = microcode_dev_init();
825 if (error)
826 return error;
Shaohua Lia30a6a22006-09-27 01:50:52 -0700827 microcode_pdev = platform_device_register_simple("microcode", -1,
828 NULL, 0);
829 if (IS_ERR(microcode_pdev)) {
830 microcode_dev_exit();
831 return PTR_ERR(microcode_pdev);
832 }
Shaohua Li9a3110b2006-09-27 01:50:51 -0700833
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100834 get_online_cpus();
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700835 error = sysdev_driver_register(&cpu_sysdev_class, &mc_sysdev_driver);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100836 put_online_cpus();
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700837 if (error) {
838 microcode_dev_exit();
839 platform_device_unregister(microcode_pdev);
840 return error;
841 }
842
843 register_hotcpu_notifier(&mc_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return 0;
845}
846
847static void __exit microcode_exit (void)
848{
Shaohua Li9a3110b2006-09-27 01:50:51 -0700849 microcode_dev_exit();
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700850
851 unregister_hotcpu_notifier(&mc_cpu_notifier);
852
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100853 get_online_cpus();
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700854 sysdev_driver_unregister(&cpu_sysdev_class, &mc_sysdev_driver);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100855 put_online_cpus();
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700856
Shaohua Lia30a6a22006-09-27 01:50:52 -0700857 platform_device_unregister(microcode_pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
860module_init(microcode_init)
861module_exit(microcode_exit)