microcode.c 12 KB

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