microcode.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * Intel CPU Microcode Update Driver for Linux
  3. *
  4. * Copyright (C) 2000-2006 Tigran Aivazian <tigran@aivazian.fsnet.co.uk>
  5. * 2006 Shaohua Li <shaohua.li@intel.com>
  6. *
  7. * This driver allows to upgrade microcode on Intel processors
  8. * belonging to IA-32 family - PentiumPro, Pentium II,
  9. * Pentium III, Xeon, Pentium 4, etc.
  10. *
  11. * Reference: Section 8.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
  16. *
  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.
  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
  68. * 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. //#define DEBUG /* pr_debug */
  74. #include <linux/capability.h>
  75. #include <linux/kernel.h>
  76. #include <linux/init.h>
  77. #include <linux/sched.h>
  78. #include <linux/smp_lock.h>
  79. #include <linux/cpumask.h>
  80. #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>
  86. #include <linux/fs.h>
  87. #include <linux/mutex.h>
  88. #include <linux/cpu.h>
  89. #include <linux/firmware.h>
  90. #include <linux/platform_device.h>
  91. #include <asm/msr.h>
  92. #include <asm/uaccess.h>
  93. #include <asm/processor.h>
  94. #include <asm/microcode.h>
  95. MODULE_DESCRIPTION("Microcode Update Driver");
  96. MODULE_AUTHOR("Tigran Aivazian <tigran@aivazian.fsnet.co.uk>");
  97. MODULE_LICENSE("GPL");
  98. #define MICROCODE_VERSION "2.00"
  99. struct microcode_ops *microcode_ops;
  100. /* no concurrent ->write()s are allowed on /dev/cpu/microcode */
  101. static DEFINE_MUTEX(microcode_mutex);
  102. EXPORT_SYMBOL_GPL(microcode_mutex);
  103. static struct ucode_cpu_info ucode_cpu_info[NR_CPUS];
  104. EXPORT_SYMBOL_GPL(ucode_cpu_info);
  105. #ifdef CONFIG_MICROCODE_OLD_INTERFACE
  106. static void __user *user_buffer; /* user area microcode data buffer */
  107. EXPORT_SYMBOL_GPL(user_buffer);
  108. static unsigned int user_buffer_size; /* it's size */
  109. EXPORT_SYMBOL_GPL(user_buffer_size);
  110. static int do_microcode_update (void)
  111. {
  112. long cursor = 0;
  113. int error = 0;
  114. void *new_mc = NULL;
  115. int cpu;
  116. cpumask_t old;
  117. cpumask_of_cpu_ptr_declare(newmask);
  118. old = current->cpus_allowed;
  119. while ((cursor = microcode_ops->get_next_ucode(&new_mc, cursor)) > 0) {
  120. error = microcode_ops->microcode_sanity_check(new_mc);
  121. if (error)
  122. goto out;
  123. /*
  124. * It's possible the data file has multiple matching ucode,
  125. * lets keep searching till the latest version
  126. */
  127. for_each_online_cpu(cpu) {
  128. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  129. if (!uci->valid)
  130. continue;
  131. cpumask_of_cpu_ptr_next(newmask, cpu);
  132. set_cpus_allowed_ptr(current, newmask);
  133. error = microcode_ops->get_matching_microcode(new_mc,
  134. cpu);
  135. if (error < 0)
  136. goto out;
  137. if (error == 1)
  138. microcode_ops->apply_microcode(cpu);
  139. }
  140. vfree(new_mc);
  141. }
  142. out:
  143. if (cursor > 0)
  144. vfree(new_mc);
  145. if (cursor < 0)
  146. error = cursor;
  147. set_cpus_allowed_ptr(current, &old);
  148. return error;
  149. }
  150. static int microcode_open (struct inode *unused1, struct file *unused2)
  151. {
  152. cycle_kernel_lock();
  153. return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
  154. }
  155. static ssize_t microcode_write (struct file *file, const char __user *buf, size_t len, loff_t *ppos)
  156. {
  157. ssize_t ret;
  158. if ((len >> PAGE_SHIFT) > num_physpages) {
  159. printk(KERN_ERR "microcode: too much data (max %ld pages)\n", num_physpages);
  160. return -EINVAL;
  161. }
  162. get_online_cpus();
  163. mutex_lock(&microcode_mutex);
  164. user_buffer = (void __user *) buf;
  165. user_buffer_size = (int) len;
  166. ret = do_microcode_update();
  167. if (!ret)
  168. ret = (ssize_t)len;
  169. mutex_unlock(&microcode_mutex);
  170. put_online_cpus();
  171. return ret;
  172. }
  173. static const struct file_operations microcode_fops = {
  174. .owner = THIS_MODULE,
  175. .write = microcode_write,
  176. .open = microcode_open,
  177. };
  178. static struct miscdevice microcode_dev = {
  179. .minor = MICROCODE_MINOR,
  180. .name = "microcode",
  181. .fops = &microcode_fops,
  182. };
  183. static int __init microcode_dev_init (void)
  184. {
  185. int error;
  186. error = misc_register(&microcode_dev);
  187. if (error) {
  188. printk(KERN_ERR
  189. "microcode: can't misc_register on minor=%d\n",
  190. MICROCODE_MINOR);
  191. return error;
  192. }
  193. return 0;
  194. }
  195. static void microcode_dev_exit (void)
  196. {
  197. misc_deregister(&microcode_dev);
  198. }
  199. MODULE_ALIAS_MISCDEV(MICROCODE_MINOR);
  200. #else
  201. #define microcode_dev_init() 0
  202. #define microcode_dev_exit() do { } while(0)
  203. #endif
  204. /* fake device for request_firmware */
  205. static struct platform_device *microcode_pdev;
  206. EXPORT_SYMBOL_GPL(microcode_pdev);
  207. static void microcode_init_cpu(int cpu, int resume)
  208. {
  209. cpumask_t old;
  210. cpumask_of_cpu_ptr(newmask, cpu);
  211. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  212. old = current->cpus_allowed;
  213. set_cpus_allowed_ptr(current, newmask);
  214. mutex_lock(&microcode_mutex);
  215. microcode_ops->collect_cpu_info(cpu);
  216. if (uci->valid && system_state == SYSTEM_RUNNING && !resume)
  217. microcode_ops->cpu_request_microcode(cpu);
  218. mutex_unlock(&microcode_mutex);
  219. set_cpus_allowed_ptr(current, &old);
  220. }
  221. static ssize_t reload_store(struct sys_device *dev,
  222. struct sysdev_attribute *attr,
  223. const char *buf, size_t sz)
  224. {
  225. struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
  226. char *end;
  227. unsigned long val = simple_strtoul(buf, &end, 0);
  228. int err = 0;
  229. int cpu = dev->id;
  230. if (end == buf)
  231. return -EINVAL;
  232. if (val == 1) {
  233. cpumask_t old;
  234. cpumask_of_cpu_ptr(newmask, cpu);
  235. old = current->cpus_allowed;
  236. get_online_cpus();
  237. set_cpus_allowed_ptr(current, newmask);
  238. mutex_lock(&microcode_mutex);
  239. if (uci->valid)
  240. err = microcode_ops->cpu_request_microcode(cpu);
  241. mutex_unlock(&microcode_mutex);
  242. put_online_cpus();
  243. set_cpus_allowed_ptr(current, &old);
  244. }
  245. if (err)
  246. return err;
  247. return sz;
  248. }
  249. static ssize_t version_show(struct sys_device *dev,
  250. struct sysdev_attribute *attr, char *buf)
  251. {
  252. struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
  253. return sprintf(buf, "0x%x\n", uci->rev);
  254. }
  255. static ssize_t pf_show(struct sys_device *dev,
  256. struct sysdev_attribute *attr, char *buf)
  257. {
  258. struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
  259. return sprintf(buf, "0x%x\n", uci->pf);
  260. }
  261. static SYSDEV_ATTR(reload, 0200, NULL, reload_store);
  262. static SYSDEV_ATTR(version, 0400, version_show, NULL);
  263. static SYSDEV_ATTR(processor_flags, 0400, pf_show, NULL);
  264. static struct attribute *mc_default_attrs[] = {
  265. &attr_reload.attr,
  266. &attr_version.attr,
  267. &attr_processor_flags.attr,
  268. NULL
  269. };
  270. static struct attribute_group mc_attr_group = {
  271. .attrs = mc_default_attrs,
  272. .name = "microcode",
  273. };
  274. static int __mc_sysdev_add(struct sys_device *sys_dev, int resume)
  275. {
  276. int err, cpu = sys_dev->id;
  277. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  278. if (!cpu_online(cpu))
  279. return 0;
  280. pr_debug("microcode: CPU%d added\n", cpu);
  281. memset(uci, 0, sizeof(*uci));
  282. err = sysfs_create_group(&sys_dev->kobj, &mc_attr_group);
  283. if (err)
  284. return err;
  285. microcode_init_cpu(cpu, resume);
  286. return 0;
  287. }
  288. static int mc_sysdev_add(struct sys_device *sys_dev)
  289. {
  290. return __mc_sysdev_add(sys_dev, 0);
  291. }
  292. static int mc_sysdev_remove(struct sys_device *sys_dev)
  293. {
  294. int cpu = sys_dev->id;
  295. if (!cpu_online(cpu))
  296. return 0;
  297. pr_debug("microcode: CPU%d removed\n", cpu);
  298. microcode_ops->microcode_fini_cpu(cpu);
  299. sysfs_remove_group(&sys_dev->kobj, &mc_attr_group);
  300. return 0;
  301. }
  302. static int mc_sysdev_resume(struct sys_device *dev)
  303. {
  304. int cpu = dev->id;
  305. if (!cpu_online(cpu))
  306. return 0;
  307. pr_debug("microcode: CPU%d resumed\n", cpu);
  308. /* only CPU 0 will apply ucode here */
  309. microcode_ops->apply_microcode(0);
  310. return 0;
  311. }
  312. static struct sysdev_driver mc_sysdev_driver = {
  313. .add = mc_sysdev_add,
  314. .remove = mc_sysdev_remove,
  315. .resume = mc_sysdev_resume,
  316. };
  317. static __cpuinit int
  318. mc_cpu_callback(struct notifier_block *nb, unsigned long action, void *hcpu)
  319. {
  320. unsigned int cpu = (unsigned long)hcpu;
  321. struct sys_device *sys_dev;
  322. sys_dev = get_cpu_sysdev(cpu);
  323. switch (action) {
  324. case CPU_UP_CANCELED_FROZEN:
  325. /* The CPU refused to come up during a system resume */
  326. microcode_ops->microcode_fini_cpu(cpu);
  327. break;
  328. case CPU_ONLINE:
  329. case CPU_DOWN_FAILED:
  330. mc_sysdev_add(sys_dev);
  331. break;
  332. case CPU_ONLINE_FROZEN:
  333. /* System-wide resume is in progress, try to apply microcode */
  334. if (microcode_ops->apply_microcode_check_cpu(cpu)) {
  335. /* The application of microcode failed */
  336. microcode_ops->microcode_fini_cpu(cpu);
  337. __mc_sysdev_add(sys_dev, 1);
  338. break;
  339. }
  340. case CPU_DOWN_FAILED_FROZEN:
  341. if (sysfs_create_group(&sys_dev->kobj, &mc_attr_group))
  342. printk(KERN_ERR "microcode: Failed to create the sysfs "
  343. "group for CPU%d\n", cpu);
  344. break;
  345. case CPU_DOWN_PREPARE:
  346. mc_sysdev_remove(sys_dev);
  347. break;
  348. case CPU_DOWN_PREPARE_FROZEN:
  349. /* Suspend is in progress, only remove the interface */
  350. sysfs_remove_group(&sys_dev->kobj, &mc_attr_group);
  351. break;
  352. }
  353. return NOTIFY_OK;
  354. }
  355. static struct notifier_block __refdata mc_cpu_notifier = {
  356. .notifier_call = mc_cpu_callback,
  357. };
  358. int microcode_init(void *opaque, struct module *module)
  359. {
  360. struct microcode_ops *ops = (struct microcode_ops *)opaque;
  361. int error;
  362. if (microcode_ops) {
  363. printk(KERN_ERR "microcode: already loaded the other module\n");
  364. return -EEXIST;
  365. }
  366. microcode_ops = ops;
  367. error = microcode_dev_init();
  368. if (error)
  369. return error;
  370. microcode_pdev = platform_device_register_simple("microcode", -1,
  371. NULL, 0);
  372. if (IS_ERR(microcode_pdev)) {
  373. microcode_dev_exit();
  374. return PTR_ERR(microcode_pdev);
  375. }
  376. get_online_cpus();
  377. error = sysdev_driver_register(&cpu_sysdev_class, &mc_sysdev_driver);
  378. put_online_cpus();
  379. if (error) {
  380. microcode_dev_exit();
  381. platform_device_unregister(microcode_pdev);
  382. return error;
  383. }
  384. register_hotcpu_notifier(&mc_cpu_notifier);
  385. printk(KERN_INFO
  386. "Microcode Update Driver: v" MICROCODE_VERSION
  387. " <tigran@aivazian.fsnet.co.uk>"
  388. " <peter.oruba@amd.com>\n");
  389. return 0;
  390. }
  391. EXPORT_SYMBOL_GPL(microcode_init);
  392. void __exit microcode_exit (void)
  393. {
  394. microcode_dev_exit();
  395. unregister_hotcpu_notifier(&mc_cpu_notifier);
  396. get_online_cpus();
  397. sysdev_driver_unregister(&cpu_sysdev_class, &mc_sysdev_driver);
  398. put_online_cpus();
  399. platform_device_unregister(microcode_pdev);
  400. microcode_ops = NULL;
  401. printk(KERN_INFO
  402. "Microcode Update Driver: v" MICROCODE_VERSION " removed.\n");
  403. }
  404. EXPORT_SYMBOL_GPL(microcode_exit);