microcode_core.c 13 KB

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