microcode.c 13 KB

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