microcode.c 13 KB

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