blob: 9758fea87c5b4e219d8c28d7dde2260f3a5902e4 [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>
Dave Jones5cf6c542006-02-28 16:58:53 -080079#include <linux/cpumask.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <linux/module.h>
81#include <linux/slab.h>
82#include <linux/vmalloc.h>
83#include <linux/miscdevice.h>
84#include <linux/spinlock.h>
85#include <linux/mm.h>
Alexey Dobriyan4e950f62007-07-30 02:36:13 +040086#include <linux/fs.h>
Ingo Molnar14cc3e22006-03-26 01:37:14 -080087#include <linux/mutex.h>
Shaohua Lia30a6a22006-09-27 01:50:52 -070088#include <linux/cpu.h>
89#include <linux/firmware.h>
90#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92#include <asm/msr.h>
93#include <asm/uaccess.h>
94#include <asm/processor.h>
95
96MODULE_DESCRIPTION("Intel CPU (IA-32) Microcode Update Driver");
Tigran Aivazian69688262006-12-13 00:35:14 -080097MODULE_AUTHOR("Tigran Aivazian <tigran@aivazian.fsnet.co.uk>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070098MODULE_LICENSE("GPL");
99
Jan Beulichba528f22006-06-23 02:04:19 -0700100#define MICROCODE_VERSION "1.14a"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102#define DEFAULT_UCODE_DATASIZE (2000) /* 2000 bytes */
103#define MC_HEADER_SIZE (sizeof (microcode_header_t)) /* 48 bytes */
104#define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE) /* 2048 bytes */
105#define EXT_HEADER_SIZE (sizeof (struct extended_sigtable)) /* 20 bytes */
106#define EXT_SIGNATURE_SIZE (sizeof (struct extended_signature)) /* 12 bytes */
107#define DWSIZE (sizeof (u32))
108#define get_totalsize(mc) \
109 (((microcode_t *)mc)->hdr.totalsize ? \
110 ((microcode_t *)mc)->hdr.totalsize : DEFAULT_UCODE_TOTALSIZE)
111#define get_datasize(mc) \
112 (((microcode_t *)mc)->hdr.datasize ? \
113 ((microcode_t *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE)
114
115#define sigmatch(s1, s2, p1, p2) \
116 (((s1) == (s2)) && (((p1) & (p2)) || (((p1) == 0) && ((p2) == 0))))
117
118#define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE)
119
120/* serialize access to the physical write to MSR 0x79 */
121static DEFINE_SPINLOCK(microcode_update_lock);
122
123/* no concurrent ->write()s are allowed on /dev/cpu/microcode */
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800124static DEFINE_MUTEX(microcode_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126static struct ucode_cpu_info {
Shaohua Li9a3110b2006-09-27 01:50:51 -0700127 int valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 unsigned int sig;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700129 unsigned int pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 unsigned int rev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 microcode_t *mc;
132} ucode_cpu_info[NR_CPUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Shaohua Li9a3110b2006-09-27 01:50:51 -0700134static void collect_cpu_info(int cpu_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Mike Travis92cb7612007-10-19 20:35:04 +0200136 struct cpuinfo_x86 *c = &cpu_data(cpu_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
138 unsigned int val[2];
139
Shaohua Li9a3110b2006-09-27 01:50:51 -0700140 /* We should bind the task to the CPU */
141 BUG_ON(raw_smp_processor_id() != cpu_num);
142 uci->pf = uci->rev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 uci->mc = NULL;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700144 uci->valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
147 cpu_has(c, X86_FEATURE_IA64)) {
Shaohua Li9a3110b2006-09-27 01:50:51 -0700148 printk(KERN_ERR "microcode: CPU%d not a capable Intel "
149 "processor\n", cpu_num);
150 uci->valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Shaohua Li9a3110b2006-09-27 01:50:51 -0700154 uci->sig = cpuid_eax(0x00000001);
155
156 if ((c->x86_model >= 5) || (c->x86 > 6)) {
157 /* get processor flags from MSR 0x17 */
158 rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
159 uci->pf = 1 << ((val[1] >> 18) & 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
161
162 wrmsr(MSR_IA32_UCODE_REV, 0, 0);
Zachary Amsden245067d2005-09-03 15:56:37 -0700163 /* see notes above for revision 1.07. Apparent chip bug */
Andi Kleen487472b2006-01-11 22:45:27 +0100164 sync_core();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 /* get the current revision from MSR 0x8B */
166 rdmsr(MSR_IA32_UCODE_REV, val[0], uci->rev);
167 pr_debug("microcode: collect_cpu_info : sig=0x%x, pf=0x%x, rev=0x%x\n",
168 uci->sig, uci->pf, uci->rev);
169}
170
Shaohua Li9a3110b2006-09-27 01:50:51 -0700171static inline int microcode_update_match(int cpu_num,
172 microcode_header_t *mc_header, int sig, int pf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
175
Shaohua Li9a3110b2006-09-27 01:50:51 -0700176 if (!sigmatch(sig, uci->sig, pf, uci->pf)
177 || mc_header->rev <= uci->rev)
178 return 0;
179 return 1;
180}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Shaohua Li9a3110b2006-09-27 01:50:51 -0700182static int microcode_sanity_check(void *mc)
183{
184 microcode_header_t *mc_header = mc;
185 struct extended_sigtable *ext_header = NULL;
186 struct extended_signature *ext_sig;
187 unsigned long total_size, data_size, ext_table_size;
188 int sum, orig_sum, ext_sigcount = 0, i;
189
190 total_size = get_totalsize(mc_header);
191 data_size = get_datasize(mc_header);
Shaohua Libd8e39f2006-09-27 01:50:54 -0700192 if (data_size + MC_HEADER_SIZE > total_size) {
Shaohua Li9a3110b2006-09-27 01:50:51 -0700193 printk(KERN_ERR "microcode: error! "
194 "Bad data size in microcode data file\n");
195 return -EINVAL;
196 }
197
198 if (mc_header->ldrver != 1 || mc_header->hdrver != 1) {
199 printk(KERN_ERR "microcode: error! "
200 "Unknown microcode update format\n");
201 return -EINVAL;
202 }
203 ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
204 if (ext_table_size) {
205 if ((ext_table_size < EXT_HEADER_SIZE)
206 || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
207 printk(KERN_ERR "microcode: error! "
208 "Small exttable size in microcode data file\n");
209 return -EINVAL;
Jan Beulichba528f22006-06-23 02:04:19 -0700210 }
Shaohua Li9a3110b2006-09-27 01:50:51 -0700211 ext_header = mc + MC_HEADER_SIZE + data_size;
212 if (ext_table_size != exttable_size(ext_header)) {
213 printk(KERN_ERR "microcode: error! "
214 "Bad exttable size in microcode data file\n");
215 return -EFAULT;
Jan Beulichba528f22006-06-23 02:04:19 -0700216 }
Shaohua Li9a3110b2006-09-27 01:50:51 -0700217 ext_sigcount = ext_header->count;
218 }
219
220 /* check extended table checksum */
221 if (ext_table_size) {
222 int ext_table_sum = 0;
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700223 int *ext_tablep = (int *)ext_header;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700224
225 i = ext_table_size / DWSIZE;
226 while (i--)
227 ext_table_sum += ext_tablep[i];
228 if (ext_table_sum) {
229 printk(KERN_WARNING "microcode: aborting, "
230 "bad extended signature table checksum\n");
231 return -EINVAL;
Jan Beulichba528f22006-06-23 02:04:19 -0700232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
Shaohua Li9a3110b2006-09-27 01:50:51 -0700234
235 /* calculate the checksum */
236 orig_sum = 0;
237 i = (MC_HEADER_SIZE + data_size) / DWSIZE;
238 while (i--)
239 orig_sum += ((int *)mc)[i];
240 if (orig_sum) {
241 printk(KERN_ERR "microcode: aborting, bad checksum\n");
242 return -EINVAL;
243 }
244 if (!ext_table_size)
245 return 0;
246 /* check extended signature checksum */
247 for (i = 0; i < ext_sigcount; i++) {
Jan Engelhardtade1af72008-01-30 13:33:23 +0100248 ext_sig = (void *)ext_header + EXT_HEADER_SIZE +
249 EXT_SIGNATURE_SIZE * i;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700250 sum = orig_sum
251 - (mc_header->sig + mc_header->pf + mc_header->cksum)
252 + (ext_sig->sig + ext_sig->pf + ext_sig->cksum);
253 if (sum) {
254 printk(KERN_ERR "microcode: aborting, bad checksum\n");
255 return -EINVAL;
256 }
257 }
258 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Shaohua Li9a3110b2006-09-27 01:50:51 -0700261/*
262 * return 0 - no update found
263 * return 1 - found update
264 * return < 0 - error
265 */
266static int get_maching_microcode(void *mc, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Shaohua Li9a3110b2006-09-27 01:50:51 -0700268 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
269 microcode_header_t *mc_header = mc;
270 struct extended_sigtable *ext_header;
271 unsigned long total_size = get_totalsize(mc_header);
272 int ext_sigcount, i;
273 struct extended_signature *ext_sig;
274 void *new_mc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Shaohua Li9a3110b2006-09-27 01:50:51 -0700276 if (microcode_update_match(cpu, mc_header,
277 mc_header->sig, mc_header->pf))
278 goto find;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Shaohua Li9a3110b2006-09-27 01:50:51 -0700280 if (total_size <= get_datasize(mc_header) + MC_HEADER_SIZE)
281 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Jan Engelhardtade1af72008-01-30 13:33:23 +0100283 ext_header = mc + get_datasize(mc_header) + MC_HEADER_SIZE;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700284 ext_sigcount = ext_header->count;
Jan Engelhardtade1af72008-01-30 13:33:23 +0100285 ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700286 for (i = 0; i < ext_sigcount; i++) {
287 if (microcode_update_match(cpu, mc_header,
288 ext_sig->sig, ext_sig->pf))
289 goto find;
290 ext_sig++;
291 }
292 return 0;
293find:
Ben Castricumfe176de2008-03-27 20:52:35 +0100294 pr_debug("microcode: CPU%d found a matching microcode update with"
Shaohua Li9a3110b2006-09-27 01:50:51 -0700295 " version 0x%x (current=0x%x)\n", cpu, mc_header->rev,uci->rev);
296 new_mc = vmalloc(total_size);
297 if (!new_mc) {
298 printk(KERN_ERR "microcode: error! Can not allocate memory\n");
299 return -ENOMEM;
300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Shaohua Li9a3110b2006-09-27 01:50:51 -0700302 /* free previous update file */
303 vfree(uci->mc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Shaohua Li9a3110b2006-09-27 01:50:51 -0700305 memcpy(new_mc, mc, total_size);
306 uci->mc = new_mc;
307 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
Shaohua Li9a3110b2006-09-27 01:50:51 -0700310static void apply_microcode(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 unsigned long flags;
313 unsigned int val[2];
Shaohua Li9a3110b2006-09-27 01:50:51 -0700314 int cpu_num = raw_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
316
Shaohua Li9a3110b2006-09-27 01:50:51 -0700317 /* We should bind the task to the CPU */
318 BUG_ON(cpu_num != cpu);
319
320 if (uci->mc == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 /* serialize access to the physical write to MSR 0x79 */
Ben Castricumbc4e0f92008-06-10 13:15:12 +0200324 spin_lock_irqsave(&microcode_update_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 /* write microcode via MSR 0x79 */
327 wrmsr(MSR_IA32_UCODE_WRITE,
Ben Castricumbc4e0f92008-06-10 13:15:12 +0200328 (unsigned long) uci->mc->bits,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 (unsigned long) uci->mc->bits >> 16 >> 16);
330 wrmsr(MSR_IA32_UCODE_REV, 0, 0);
331
Zachary Amsden245067d2005-09-03 15:56:37 -0700332 /* see notes above for revision 1.07. Apparent chip bug */
Andi Kleen487472b2006-01-11 22:45:27 +0100333 sync_core();
Zachary Amsden245067d2005-09-03 15:56:37 -0700334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 /* get the current revision from MSR 0x8B */
336 rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 spin_unlock_irqrestore(&microcode_update_lock, flags);
Shaohua Li9a3110b2006-09-27 01:50:51 -0700339 if (val[1] != uci->mc->hdr.rev) {
Ben Castricumfe176de2008-03-27 20:52:35 +0100340 printk(KERN_ERR "microcode: CPU%d update from revision "
Shaohua Li9a3110b2006-09-27 01:50:51 -0700341 "0x%x to 0x%x failed\n", cpu_num, uci->rev, val[1]);
342 return;
343 }
Ben Castricumfe176de2008-03-27 20:52:35 +0100344 printk(KERN_INFO "microcode: CPU%d updated from revision "
Ben Castricumbc4e0f92008-06-10 13:15:12 +0200345 "0x%x to 0x%x, date = %08x \n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 cpu_num, uci->rev, val[1], uci->mc->hdr.date);
Shaohua Li9a3110b2006-09-27 01:50:51 -0700347 uci->rev = val[1];
348}
349
350#ifdef CONFIG_MICROCODE_OLD_INTERFACE
351static void __user *user_buffer; /* user area microcode data buffer */
352static unsigned int user_buffer_size; /* it's size */
353
354static long get_next_ucode(void **mc, long offset)
355{
356 microcode_header_t mc_header;
357 unsigned long total_size;
358
359 /* No more data */
360 if (offset >= user_buffer_size)
361 return 0;
362 if (copy_from_user(&mc_header, user_buffer + offset, MC_HEADER_SIZE)) {
363 printk(KERN_ERR "microcode: error! Can not read user data\n");
364 return -EFAULT;
365 }
366 total_size = get_totalsize(&mc_header);
Shaohua Libd8e39f2006-09-27 01:50:54 -0700367 if (offset + total_size > user_buffer_size) {
Shaohua Li9a3110b2006-09-27 01:50:51 -0700368 printk(KERN_ERR "microcode: error! Bad total size in microcode "
369 "data file\n");
370 return -EINVAL;
371 }
372 *mc = vmalloc(total_size);
373 if (!*mc)
374 return -ENOMEM;
375 if (copy_from_user(*mc, user_buffer + offset, total_size)) {
376 printk(KERN_ERR "microcode: error! Can not read user data\n");
377 vfree(*mc);
378 return -EFAULT;
379 }
380 return offset + total_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
383static int do_microcode_update (void)
384{
Shaohua Li9a3110b2006-09-27 01:50:51 -0700385 long cursor = 0;
386 int error = 0;
Andi Kleen2ba1ff22007-02-13 13:26:25 +0100387 void *new_mc = NULL;
Shaohua Li9a3110b2006-09-27 01:50:51 -0700388 int cpu;
389 cpumask_t old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Shaohua Li9a3110b2006-09-27 01:50:51 -0700391 old = current->cpus_allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Shaohua Li9a3110b2006-09-27 01:50:51 -0700393 while ((cursor = get_next_ucode(&new_mc, cursor)) > 0) {
394 error = microcode_sanity_check(new_mc);
395 if (error)
396 goto out;
397 /*
398 * It's possible the data file has multiple matching ucode,
399 * lets keep searching till the latest version
400 */
401 for_each_online_cpu(cpu) {
402 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Shaohua Li9a3110b2006-09-27 01:50:51 -0700404 if (!uci->valid)
405 continue;
Mike Travisfc0e4742008-04-04 18:11:05 -0700406 set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
Shaohua Li9a3110b2006-09-27 01:50:51 -0700407 error = get_maching_microcode(new_mc, cpu);
408 if (error < 0)
409 goto out;
410 if (error == 1)
411 apply_microcode(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
Shaohua Li9a3110b2006-09-27 01:50:51 -0700413 vfree(new_mc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415out:
Shaohua Li9a3110b2006-09-27 01:50:51 -0700416 if (cursor > 0)
417 vfree(new_mc);
418 if (cursor < 0)
419 error = cursor;
Mike Travisfc0e4742008-04-04 18:11:05 -0700420 set_cpus_allowed_ptr(current, &old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return error;
422}
423
Shaohua Li9a3110b2006-09-27 01:50:51 -0700424static int microcode_open (struct inode *unused1, struct file *unused2)
425{
426 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
427}
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429static ssize_t microcode_write (struct file *file, const char __user *buf, size_t len, loff_t *ppos)
430{
431 ssize_t ret;
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if ((len >> PAGE_SHIFT) > num_physpages) {
434 printk(KERN_ERR "microcode: too much data (max %ld pages)\n", num_physpages);
435 return -EINVAL;
436 }
437
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100438 get_online_cpus();
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800439 mutex_lock(&microcode_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 user_buffer = (void __user *) buf;
442 user_buffer_size = (int) len;
443
444 ret = do_microcode_update();
445 if (!ret)
446 ret = (ssize_t)len;
447
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800448 mutex_unlock(&microcode_mutex);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100449 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 return ret;
452}
453
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800454static const struct file_operations microcode_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 .owner = THIS_MODULE,
456 .write = microcode_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 .open = microcode_open,
458};
459
460static struct miscdevice microcode_dev = {
461 .minor = MICROCODE_MINOR,
462 .name = "microcode",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 .fops = &microcode_fops,
464};
465
Shaohua Li9a3110b2006-09-27 01:50:51 -0700466static int __init microcode_dev_init (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 int error;
469
470 error = misc_register(&microcode_dev);
471 if (error) {
472 printk(KERN_ERR
473 "microcode: can't misc_register on minor=%d\n",
474 MICROCODE_MINOR);
475 return error;
476 }
477
Shaohua Li9a3110b2006-09-27 01:50:51 -0700478 return 0;
479}
480
Sam Ravnborgf8281a22007-06-01 00:47:10 -0700481static void microcode_dev_exit (void)
Shaohua Li9a3110b2006-09-27 01:50:51 -0700482{
483 misc_deregister(&microcode_dev);
484}
485
486MODULE_ALIAS_MISCDEV(MICROCODE_MINOR);
487#else
488#define microcode_dev_init() 0
489#define microcode_dev_exit() do { } while(0)
490#endif
491
Shaohua Lia30a6a22006-09-27 01:50:52 -0700492static long get_next_ucode_from_buffer(void **mc, void *buf,
493 unsigned long size, long offset)
494{
495 microcode_header_t *mc_header;
496 unsigned long total_size;
497
498 /* No more data */
499 if (offset >= size)
500 return 0;
501 mc_header = (microcode_header_t *)(buf + offset);
502 total_size = get_totalsize(mc_header);
503
Shaohua Libd8e39f2006-09-27 01:50:54 -0700504 if (offset + total_size > size) {
Shaohua Lia30a6a22006-09-27 01:50:52 -0700505 printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
506 return -EINVAL;
507 }
508
509 *mc = vmalloc(total_size);
510 if (!*mc) {
511 printk(KERN_ERR "microcode: error! Can not allocate memory\n");
512 return -ENOMEM;
513 }
514 memcpy(*mc, buf + offset, total_size);
515 return offset + total_size;
516}
517
518/* fake device for request_firmware */
519static struct platform_device *microcode_pdev;
520
521static int cpu_request_microcode(int cpu)
522{
523 char name[30];
Mike Travis92cb7612007-10-19 20:35:04 +0200524 struct cpuinfo_x86 *c = &cpu_data(cpu);
Shaohua Lia30a6a22006-09-27 01:50:52 -0700525 const struct firmware *firmware;
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700526 void *buf;
Shaohua Lia30a6a22006-09-27 01:50:52 -0700527 unsigned long size;
528 long offset = 0;
529 int error;
530 void *mc;
531
532 /* We should bind the task to the CPU */
533 BUG_ON(cpu != raw_smp_processor_id());
534 sprintf(name,"intel-ucode/%02x-%02x-%02x",
535 c->x86, c->x86_model, c->x86_mask);
536 error = request_firmware(&firmware, name, &microcode_pdev->dev);
537 if (error) {
Ben Castricumbc4e0f92008-06-10 13:15:12 +0200538 pr_debug("microcode: data file %s load failed\n", name);
Shaohua Lia30a6a22006-09-27 01:50:52 -0700539 return error;
540 }
Jan Engelhardtade1af72008-01-30 13:33:23 +0100541 buf = firmware->data;
Shaohua Lia30a6a22006-09-27 01:50:52 -0700542 size = firmware->size;
543 while ((offset = get_next_ucode_from_buffer(&mc, buf, size, offset))
544 > 0) {
545 error = microcode_sanity_check(mc);
546 if (error)
547 break;
548 error = get_maching_microcode(mc, cpu);
549 if (error < 0)
550 break;
551 /*
552 * It's possible the data file has multiple matching ucode,
553 * lets keep searching till the latest version
554 */
555 if (error == 1) {
556 apply_microcode(cpu);
557 error = 0;
558 }
559 vfree(mc);
560 }
561 if (offset > 0)
562 vfree(mc);
563 if (offset < 0)
564 error = offset;
565 release_firmware(firmware);
566
567 return error;
568}
569
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700570static int apply_microcode_check_cpu(int cpu)
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700571{
Mike Travis92cb7612007-10-19 20:35:04 +0200572 struct cpuinfo_x86 *c = &cpu_data(cpu);
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700573 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
574 cpumask_t old;
575 unsigned int val[2];
576 int err = 0;
577
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700578 /* Check if the microcode is available */
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700579 if (!uci->mc)
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700580 return 0;
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700581
582 old = current->cpus_allowed;
Mike Travisfc0e4742008-04-04 18:11:05 -0700583 set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700584
585 /* Check if the microcode we have in memory matches the CPU */
586 if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
587 cpu_has(c, X86_FEATURE_IA64) || uci->sig != cpuid_eax(0x00000001))
588 err = -EINVAL;
589
590 if (!err && ((c->x86_model >= 5) || (c->x86 > 6))) {
591 /* get processor flags from MSR 0x17 */
592 rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
593 if (uci->pf != (1 << ((val[1] >> 18) & 7)))
594 err = -EINVAL;
595 }
596
597 if (!err) {
598 wrmsr(MSR_IA32_UCODE_REV, 0, 0);
599 /* see notes above for revision 1.07. Apparent chip bug */
600 sync_core();
601 /* get the current revision from MSR 0x8B */
602 rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
603 if (uci->rev != val[1])
604 err = -EINVAL;
605 }
606
607 if (!err)
608 apply_microcode(cpu);
609 else
610 printk(KERN_ERR "microcode: Could not apply microcode to CPU%d:"
611 " sig=0x%x, pf=0x%x, rev=0x%x\n",
612 cpu, uci->sig, uci->pf, uci->rev);
613
Mike Travisfc0e4742008-04-04 18:11:05 -0700614 set_cpus_allowed_ptr(current, &old);
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700615 return err;
616}
617
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700618static void microcode_init_cpu(int cpu, int resume)
Shaohua Lia30a6a22006-09-27 01:50:52 -0700619{
620 cpumask_t old;
621 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
622
623 old = current->cpus_allowed;
624
Mike Travisfc0e4742008-04-04 18:11:05 -0700625 set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
Shaohua Lia30a6a22006-09-27 01:50:52 -0700626 mutex_lock(&microcode_mutex);
627 collect_cpu_info(cpu);
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700628 if (uci->valid && system_state == SYSTEM_RUNNING && !resume)
Shaohua Lia30a6a22006-09-27 01:50:52 -0700629 cpu_request_microcode(cpu);
630 mutex_unlock(&microcode_mutex);
Mike Travisfc0e4742008-04-04 18:11:05 -0700631 set_cpus_allowed_ptr(current, &old);
Shaohua Lia30a6a22006-09-27 01:50:52 -0700632}
633
634static void microcode_fini_cpu(int cpu)
635{
636 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
637
638 mutex_lock(&microcode_mutex);
639 uci->valid = 0;
640 vfree(uci->mc);
641 uci->mc = NULL;
642 mutex_unlock(&microcode_mutex);
643}
644
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700645static ssize_t reload_store(struct sys_device *dev, const char *buf, size_t sz)
646{
647 struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
648 char *end;
649 unsigned long val = simple_strtoul(buf, &end, 0);
650 int err = 0;
651 int cpu = dev->id;
652
653 if (end == buf)
654 return -EINVAL;
655 if (val == 1) {
656 cpumask_t old;
657
658 old = current->cpus_allowed;
659
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100660 get_online_cpus();
Mike Travisfc0e4742008-04-04 18:11:05 -0700661 set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700662
663 mutex_lock(&microcode_mutex);
664 if (uci->valid)
665 err = cpu_request_microcode(cpu);
666 mutex_unlock(&microcode_mutex);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100667 put_online_cpus();
Mike Travisfc0e4742008-04-04 18:11:05 -0700668 set_cpus_allowed_ptr(current, &old);
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700669 }
670 if (err)
671 return err;
672 return sz;
673}
674
675static ssize_t version_show(struct sys_device *dev, char *buf)
676{
677 struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
678
679 return sprintf(buf, "0x%x\n", uci->rev);
680}
681
682static ssize_t pf_show(struct sys_device *dev, char *buf)
683{
684 struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
685
686 return sprintf(buf, "0x%x\n", uci->pf);
687}
688
689static SYSDEV_ATTR(reload, 0200, NULL, reload_store);
690static SYSDEV_ATTR(version, 0400, version_show, NULL);
691static SYSDEV_ATTR(processor_flags, 0400, pf_show, NULL);
692
693static struct attribute *mc_default_attrs[] = {
694 &attr_reload.attr,
695 &attr_version.attr,
696 &attr_processor_flags.attr,
697 NULL
698};
699
700static struct attribute_group mc_attr_group = {
701 .attrs = mc_default_attrs,
702 .name = "microcode",
703};
704
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700705static int __mc_sysdev_add(struct sys_device *sys_dev, int resume)
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700706{
Jeff Garzik2e3ad8a2006-10-11 01:21:53 -0700707 int err, cpu = sys_dev->id;
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700708 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
709
710 if (!cpu_online(cpu))
711 return 0;
Jeff Garzik2e3ad8a2006-10-11 01:21:53 -0700712
Ben Castricumfe176de2008-03-27 20:52:35 +0100713 pr_debug("microcode: CPU%d added\n", cpu);
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700714 memset(uci, 0, sizeof(*uci));
Jeff Garzik2e3ad8a2006-10-11 01:21:53 -0700715
716 err = sysfs_create_group(&sys_dev->kobj, &mc_attr_group);
717 if (err)
718 return err;
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700719
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700720 microcode_init_cpu(cpu, resume);
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700721
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700722 return 0;
723}
724
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700725static int mc_sysdev_add(struct sys_device *sys_dev)
726{
727 return __mc_sysdev_add(sys_dev, 0);
728}
729
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700730static int mc_sysdev_remove(struct sys_device *sys_dev)
731{
732 int cpu = sys_dev->id;
733
734 if (!cpu_online(cpu))
735 return 0;
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700736
Ben Castricumfe176de2008-03-27 20:52:35 +0100737 pr_debug("microcode: CPU%d removed\n", cpu);
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700738 microcode_fini_cpu(cpu);
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700739 sysfs_remove_group(&sys_dev->kobj, &mc_attr_group);
740 return 0;
741}
742
743static int mc_sysdev_resume(struct sys_device *dev)
744{
745 int cpu = dev->id;
746
747 if (!cpu_online(cpu))
748 return 0;
Ben Castricumfe176de2008-03-27 20:52:35 +0100749 pr_debug("microcode: CPU%d resumed\n", cpu);
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700750 /* only CPU 0 will apply ucode here */
751 apply_microcode(0);
752 return 0;
753}
754
755static struct sysdev_driver mc_sysdev_driver = {
756 .add = mc_sysdev_add,
757 .remove = mc_sysdev_remove,
758 .resume = mc_sysdev_resume,
759};
760
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700761static __cpuinit int
762mc_cpu_callback(struct notifier_block *nb, unsigned long action, void *hcpu)
763{
764 unsigned int cpu = (unsigned long)hcpu;
765 struct sys_device *sys_dev;
766
767 sys_dev = get_cpu_sysdev(cpu);
768 switch (action) {
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700769 case CPU_UP_CANCELED_FROZEN:
770 /* The CPU refused to come up during a system resume */
771 microcode_fini_cpu(cpu);
772 break;
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700773 case CPU_ONLINE:
774 case CPU_DOWN_FAILED:
775 mc_sysdev_add(sys_dev);
776 break;
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700777 case CPU_ONLINE_FROZEN:
778 /* System-wide resume is in progress, try to apply microcode */
779 if (apply_microcode_check_cpu(cpu)) {
780 /* The application of microcode failed */
781 microcode_fini_cpu(cpu);
782 __mc_sysdev_add(sys_dev, 1);
783 break;
784 }
785 case CPU_DOWN_FAILED_FROZEN:
786 if (sysfs_create_group(&sys_dev->kobj, &mc_attr_group))
Ben Castricumfe176de2008-03-27 20:52:35 +0100787 printk(KERN_ERR "microcode: Failed to create the sysfs "
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700788 "group for CPU%d\n", cpu);
789 break;
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700790 case CPU_DOWN_PREPARE:
791 mc_sysdev_remove(sys_dev);
792 break;
Rafael J. Wysocki455c0172007-05-09 02:35:11 -0700793 case CPU_DOWN_PREPARE_FROZEN:
794 /* Suspend is in progress, only remove the interface */
795 sysfs_remove_group(&sys_dev->kobj, &mc_attr_group);
796 break;
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700797 }
798 return NOTIFY_OK;
799}
800
Sam Ravnborgc72258c2008-02-01 17:49:42 +0100801static struct notifier_block __refdata mc_cpu_notifier = {
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700802 .notifier_call = mc_cpu_callback,
803};
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700804
Shaohua Li9a3110b2006-09-27 01:50:51 -0700805static int __init microcode_init (void)
806{
807 int error;
808
Ben Castricumbc4e0f92008-06-10 13:15:12 +0200809 printk(KERN_INFO
810 "IA-32 Microcode Update Driver: v" MICROCODE_VERSION " <tigran@aivazian.fsnet.co.uk>\n");
811
Shaohua Li9a3110b2006-09-27 01:50:51 -0700812 error = microcode_dev_init();
813 if (error)
814 return error;
Shaohua Lia30a6a22006-09-27 01:50:52 -0700815 microcode_pdev = platform_device_register_simple("microcode", -1,
816 NULL, 0);
817 if (IS_ERR(microcode_pdev)) {
818 microcode_dev_exit();
819 return PTR_ERR(microcode_pdev);
820 }
Shaohua Li9a3110b2006-09-27 01:50:51 -0700821
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100822 get_online_cpus();
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700823 error = sysdev_driver_register(&cpu_sysdev_class, &mc_sysdev_driver);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100824 put_online_cpus();
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700825 if (error) {
826 microcode_dev_exit();
827 platform_device_unregister(microcode_pdev);
828 return error;
829 }
830
831 register_hotcpu_notifier(&mc_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return 0;
833}
834
835static void __exit microcode_exit (void)
836{
Shaohua Li9a3110b2006-09-27 01:50:51 -0700837 microcode_dev_exit();
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700838
839 unregister_hotcpu_notifier(&mc_cpu_notifier);
840
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100841 get_online_cpus();
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700842 sysdev_driver_unregister(&cpu_sysdev_class, &mc_sysdev_driver);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100843 put_online_cpus();
Shaohua Li9a4b9ef2006-09-27 01:50:53 -0700844
Shaohua Lia30a6a22006-09-27 01:50:52 -0700845 platform_device_unregister(microcode_pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
848module_init(microcode_init)
849module_exit(microcode_exit)