2 * Intel CPU Microcode Update Driver for Linux
4 * Copyright (C) 2000-2006 Tigran Aivazian <tigran@aivazian.fsnet.co.uk>
5 * 2006 Shaohua Li <shaohua.li@intel.com>
7 * This driver allows to upgrade microcode on Intel processors
8 * belonging to IA-32 family - PentiumPro, Pentium II,
9 * Pentium III, Xeon, Pentium 4, etc.
11 * Reference: Section 8.11 of Volume 3a, IA-32 Intel? Architecture
12 * Software Developer's Manual
13 * Order Number 253668 or free download from:
15 * http://developer.intel.com/design/pentium4/manuals/253668.htm
17 * For more information, go to http://www.urbanmyth.org/microcode
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.
24 * 1.0 16 Feb 2000, Tigran Aivazian <tigran@sco.com>
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
59 * speculative nature of implementation.
60 * 1.11 22 Mar 2002 Tigran Aivazian <tigran@veritas.com>
61 * Fix the panic when writing zero-length microcode chunk.
62 * 1.12 29 Sep 2003 Nitin Kamble <nitin.a.kamble@intel.com>,
63 * 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
67 * because we no longer hold a copy of applied microcode
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.
74 /* #define DEBUG pr_debug */
75 #include <linux/capability.h>
76 #include <linux/kernel.h>
77 #include <linux/init.h>
78 #include <linux/sched.h>
79 #include <linux/smp_lock.h>
80 #include <linux/cpumask.h>
81 #include <linux/module.h>
82 #include <linux/slab.h>
83 #include <linux/vmalloc.h>
84 #include <linux/miscdevice.h>
85 #include <linux/spinlock.h>
88 #include <linux/mutex.h>
89 #include <linux/cpu.h>
90 #include <linux/firmware.h>
91 #include <linux/platform_device.h>
94 #include <asm/uaccess.h>
95 #include <asm/processor.h>
96 #include <asm/microcode.h>
98 MODULE_DESCRIPTION("Microcode Update Driver");
99 MODULE_AUTHOR("Tigran Aivazian <tigran@aivazian.fsnet.co.uk>");
100 MODULE_LICENSE("GPL");
102 #define MICROCODE_VERSION "2.00"
104 struct microcode_ops *microcode_ops;
106 /* no concurrent ->write()s are allowed on /dev/cpu/microcode */
107 static DEFINE_MUTEX(microcode_mutex);
109 struct ucode_cpu_info ucode_cpu_info[NR_CPUS];
110 EXPORT_SYMBOL_GPL(ucode_cpu_info);
112 #ifdef CONFIG_MICROCODE_OLD_INTERFACE
113 void __user *user_buffer; /* user area microcode data buffer */
114 EXPORT_SYMBOL_GPL(user_buffer);
115 unsigned int user_buffer_size; /* it's size */
116 EXPORT_SYMBOL_GPL(user_buffer_size);
118 static int do_microcode_update(void)
126 old = current->cpus_allowed;
128 while ((cursor = microcode_ops->get_next_ucode(&new_mc, cursor)) > 0) {
129 if (microcode_ops->microcode_sanity_check != NULL)
130 error = microcode_ops->microcode_sanity_check(new_mc);
134 * It's possible the data file has multiple matching ucode,
135 * lets keep searching till the latest version
137 for_each_online_cpu(cpu) {
138 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
142 set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
143 error = microcode_ops->get_matching_microcode(new_mc,
148 microcode_ops->apply_microcode(cpu);
157 set_cpus_allowed_ptr(current, &old);
161 static int microcode_open(struct inode *unused1, struct file *unused2)
164 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
167 static ssize_t microcode_write(struct file *file, const char __user *buf,
168 size_t len, loff_t *ppos)
172 if ((len >> PAGE_SHIFT) > num_physpages) {
173 printk(KERN_ERR "microcode: too much data (max %ld pages)\n",
179 mutex_lock(µcode_mutex);
181 user_buffer = (void __user *) buf;
182 user_buffer_size = (int) len;
184 ret = do_microcode_update();
188 mutex_unlock(µcode_mutex);
194 static const struct file_operations microcode_fops = {
195 .owner = THIS_MODULE,
196 .write = microcode_write,
197 .open = microcode_open,
200 static struct miscdevice microcode_dev = {
201 .minor = MICROCODE_MINOR,
203 .fops = µcode_fops,
206 static int __init microcode_dev_init(void)
210 error = misc_register(µcode_dev);
213 "microcode: can't misc_register on minor=%d\n",
221 static void microcode_dev_exit(void)
223 misc_deregister(µcode_dev);
226 MODULE_ALIAS_MISCDEV(MICROCODE_MINOR);
228 #define microcode_dev_init() 0
229 #define microcode_dev_exit() do { } while (0)
232 /* fake device for request_firmware */
233 struct platform_device *microcode_pdev;
234 EXPORT_SYMBOL_GPL(microcode_pdev);
236 static ssize_t reload_store(struct sys_device *dev,
237 struct sysdev_attribute *attr,
238 const char *buf, size_t sz)
240 struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
242 unsigned long val = simple_strtoul(buf, &end, 0);
249 cpumask_t old = current->cpus_allowed;
252 if (cpu_online(cpu)) {
253 set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
254 mutex_lock(µcode_mutex);
256 err = microcode_ops->cpu_request_microcode(cpu);
257 mutex_unlock(µcode_mutex);
258 set_cpus_allowed_ptr(current, &old);
267 static ssize_t version_show(struct sys_device *dev,
268 struct sysdev_attribute *attr, char *buf)
270 struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
272 return sprintf(buf, "0x%x\n", uci->cpu_sig.rev);
275 static ssize_t pf_show(struct sys_device *dev,
276 struct sysdev_attribute *attr, char *buf)
278 struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
280 return sprintf(buf, "0x%x\n", uci->cpu_sig.pf);
283 static SYSDEV_ATTR(reload, 0200, NULL, reload_store);
284 static SYSDEV_ATTR(version, 0400, version_show, NULL);
285 static SYSDEV_ATTR(processor_flags, 0400, pf_show, NULL);
287 static struct attribute *mc_default_attrs[] = {
290 &attr_processor_flags.attr,
294 static struct attribute_group mc_attr_group = {
295 .attrs = mc_default_attrs,
299 static void microcode_fini_cpu(int cpu)
301 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
303 mutex_lock(µcode_mutex);
304 microcode_ops->microcode_fini_cpu(cpu);
306 mutex_unlock(µcode_mutex);
309 static void collect_cpu_info(int cpu)
311 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
313 memset(uci, 0, sizeof(*uci));
314 if (!microcode_ops->collect_cpu_info(cpu, &uci->cpu_sig))
318 static void microcode_resume_cpu(int cpu)
320 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
321 struct cpu_signature nsig;
323 pr_debug("microcode: CPU%d resumed\n", cpu);
325 if (!uci->mc.valid_mc)
329 * Let's verify that the 'cached' ucode does belong
330 * to this cpu (a bit of paranoia):
332 if (microcode_ops->collect_cpu_info(cpu, &nsig)) {
333 microcode_fini_cpu(cpu);
337 if (memcmp(&nsig, &uci->cpu_sig, sizeof(nsig))) {
338 microcode_fini_cpu(cpu);
339 /* Should we look for a new ucode here? */
343 microcode_ops->apply_microcode(cpu);
346 void microcode_update_cpu(int cpu)
348 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
350 /* We should bind the task to the CPU */
351 BUG_ON(raw_smp_processor_id() != cpu);
353 mutex_lock(µcode_mutex);
355 * Check if the system resume is in progress (uci->valid != NULL),
356 * otherwise just request a firmware:
359 microcode_resume_cpu(cpu);
361 collect_cpu_info(cpu);
362 if (uci->valid && system_state == SYSTEM_RUNNING)
363 microcode_ops->cpu_request_microcode(cpu);
365 mutex_unlock(µcode_mutex);
368 static void microcode_init_cpu(int cpu)
370 cpumask_t old = current->cpus_allowed;
372 set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
373 microcode_update_cpu(cpu);
374 set_cpus_allowed_ptr(current, &old);
377 static int mc_sysdev_add(struct sys_device *sys_dev)
379 int err, cpu = sys_dev->id;
380 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
382 if (!cpu_online(cpu))
385 pr_debug("microcode: CPU%d added\n", cpu);
386 memset(uci, 0, sizeof(*uci));
388 err = sysfs_create_group(&sys_dev->kobj, &mc_attr_group);
392 microcode_init_cpu(cpu);
396 static int mc_sysdev_remove(struct sys_device *sys_dev)
398 int cpu = sys_dev->id;
400 if (!cpu_online(cpu))
403 pr_debug("microcode: CPU%d removed\n", cpu);
404 microcode_fini_cpu(cpu);
405 sysfs_remove_group(&sys_dev->kobj, &mc_attr_group);
409 static int mc_sysdev_resume(struct sys_device *dev)
413 if (!cpu_online(cpu))
415 pr_debug("microcode: CPU%d resumed\n", cpu);
416 /* only CPU 0 will apply ucode here */
417 microcode_ops->apply_microcode(0);
421 static struct sysdev_driver mc_sysdev_driver = {
422 .add = mc_sysdev_add,
423 .remove = mc_sysdev_remove,
424 .resume = mc_sysdev_resume,
428 mc_cpu_callback(struct notifier_block *nb, unsigned long action, void *hcpu)
430 unsigned int cpu = (unsigned long)hcpu;
431 struct sys_device *sys_dev;
433 sys_dev = get_cpu_sysdev(cpu);
436 case CPU_ONLINE_FROZEN:
437 microcode_init_cpu(cpu);
438 case CPU_DOWN_FAILED:
439 case CPU_DOWN_FAILED_FROZEN:
440 pr_debug("microcode: CPU%d added\n", cpu);
441 if (sysfs_create_group(&sys_dev->kobj, &mc_attr_group))
442 printk(KERN_ERR "microcode: Failed to create the sysfs "
443 "group for CPU%d\n", cpu);
445 case CPU_DOWN_PREPARE:
446 case CPU_DOWN_PREPARE_FROZEN:
447 /* Suspend is in progress, only remove the interface */
448 sysfs_remove_group(&sys_dev->kobj, &mc_attr_group);
449 pr_debug("microcode: CPU%d removed\n", cpu);
452 case CPU_UP_CANCELED_FROZEN:
453 /* The CPU refused to come up during a system resume */
454 microcode_fini_cpu(cpu);
460 static struct notifier_block __refdata mc_cpu_notifier = {
461 .notifier_call = mc_cpu_callback,
464 int microcode_init(void *opaque, struct module *module)
466 struct microcode_ops *ops = (struct microcode_ops *)opaque;
470 printk(KERN_ERR "microcode: already loaded the other module\n");
476 error = microcode_dev_init();
479 microcode_pdev = platform_device_register_simple("microcode", -1,
481 if (IS_ERR(microcode_pdev)) {
482 microcode_dev_exit();
483 return PTR_ERR(microcode_pdev);
487 error = sysdev_driver_register(&cpu_sysdev_class, &mc_sysdev_driver);
490 microcode_dev_exit();
491 platform_device_unregister(microcode_pdev);
495 register_hotcpu_notifier(&mc_cpu_notifier);
498 "Microcode Update Driver: v" MICROCODE_VERSION
499 " <tigran@aivazian.fsnet.co.uk>"
500 " <peter.oruba@amd.com>\n");
504 EXPORT_SYMBOL_GPL(microcode_init);
506 void __exit microcode_exit(void)
508 microcode_dev_exit();
510 unregister_hotcpu_notifier(&mc_cpu_notifier);
513 sysdev_driver_unregister(&cpu_sysdev_class, &mc_sysdev_driver);
516 platform_device_unregister(microcode_pdev);
518 microcode_ops = NULL;
521 "Microcode Update Driver: v" MICROCODE_VERSION " removed.\n");
523 EXPORT_SYMBOL_GPL(microcode_exit);