microcode_core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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/platform_device.h>
  74. #include <linux/capability.h>
  75. #include <linux/miscdevice.h>
  76. #include <linux/firmware.h>
  77. #include <linux/smp_lock.h>
  78. #include <linux/spinlock.h>
  79. #include <linux/cpumask.h>
  80. #include <linux/uaccess.h>
  81. #include <linux/vmalloc.h>
  82. #include <linux/kernel.h>
  83. #include <linux/module.h>
  84. #include <linux/mutex.h>
  85. #include <linux/sched.h>
  86. #include <linux/init.h>
  87. #include <linux/slab.h>
  88. #include <linux/cpu.h>
  89. #include <linux/fs.h>
  90. #include <linux/mm.h>
  91. #include <asm/microcode.h>
  92. #include <asm/processor.h>
  93. #include <asm/msr.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. static 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. static struct platform_device *microcode_pdev;
  181. static long reload_for_cpu(void *unused)
  182. {
  183. struct ucode_cpu_info *uci = ucode_cpu_info + smp_processor_id();
  184. int err = 0;
  185. mutex_lock(&microcode_mutex);
  186. if (uci->valid) {
  187. err = microcode_ops->request_microcode_fw(smp_processor_id(),
  188. &microcode_pdev->dev);
  189. if (!err)
  190. microcode_ops->apply_microcode(smp_processor_id());
  191. }
  192. mutex_unlock(&microcode_mutex);
  193. return err;
  194. }
  195. static ssize_t reload_store(struct sys_device *dev,
  196. struct sysdev_attribute *attr,
  197. const char *buf, size_t sz)
  198. {
  199. char *end;
  200. unsigned long val = simple_strtoul(buf, &end, 0);
  201. int err = 0;
  202. int cpu = dev->id;
  203. if (end == buf)
  204. return -EINVAL;
  205. if (val == 1) {
  206. get_online_cpus();
  207. if (cpu_online(cpu))
  208. err = work_on_cpu(cpu, reload_for_cpu, NULL);
  209. put_online_cpus();
  210. }
  211. if (err)
  212. return err;
  213. return sz;
  214. }
  215. static ssize_t version_show(struct sys_device *dev,
  216. struct sysdev_attribute *attr, char *buf)
  217. {
  218. struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
  219. return sprintf(buf, "0x%x\n", uci->cpu_sig.rev);
  220. }
  221. static ssize_t pf_show(struct sys_device *dev,
  222. struct sysdev_attribute *attr, char *buf)
  223. {
  224. struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
  225. return sprintf(buf, "0x%x\n", uci->cpu_sig.pf);
  226. }
  227. static SYSDEV_ATTR(reload, 0200, NULL, reload_store);
  228. static SYSDEV_ATTR(version, 0400, version_show, NULL);
  229. static SYSDEV_ATTR(processor_flags, 0400, pf_show, NULL);
  230. static struct attribute *mc_default_attrs[] = {
  231. &attr_reload.attr,
  232. &attr_version.attr,
  233. &attr_processor_flags.attr,
  234. NULL
  235. };
  236. static struct attribute_group mc_attr_group = {
  237. .attrs = mc_default_attrs,
  238. .name = "microcode",
  239. };
  240. static void __microcode_fini_cpu(int cpu)
  241. {
  242. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  243. microcode_ops->microcode_fini_cpu(cpu);
  244. uci->valid = 0;
  245. }
  246. static void microcode_fini_cpu(int cpu)
  247. {
  248. mutex_lock(&microcode_mutex);
  249. __microcode_fini_cpu(cpu);
  250. mutex_unlock(&microcode_mutex);
  251. }
  252. static void collect_cpu_info(int cpu)
  253. {
  254. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  255. memset(uci, 0, sizeof(*uci));
  256. if (!microcode_ops->collect_cpu_info(cpu, &uci->cpu_sig))
  257. uci->valid = 1;
  258. }
  259. static int microcode_resume_cpu(int cpu)
  260. {
  261. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  262. struct cpu_signature nsig;
  263. pr_debug("microcode: CPU%d resumed\n", cpu);
  264. if (!uci->mc)
  265. return 1;
  266. /*
  267. * Let's verify that the 'cached' ucode does belong
  268. * to this cpu (a bit of paranoia):
  269. */
  270. if (microcode_ops->collect_cpu_info(cpu, &nsig)) {
  271. __microcode_fini_cpu(cpu);
  272. printk(KERN_ERR "failed to collect_cpu_info for resuming cpu #%d\n",
  273. cpu);
  274. return -1;
  275. }
  276. if ((nsig.sig != uci->cpu_sig.sig) || (nsig.pf != uci->cpu_sig.pf)) {
  277. __microcode_fini_cpu(cpu);
  278. printk(KERN_ERR "cached ucode doesn't match the resuming cpu #%d\n",
  279. cpu);
  280. /* Should we look for a new ucode here? */
  281. return 1;
  282. }
  283. return 0;
  284. }
  285. static long microcode_update_cpu(void *unused)
  286. {
  287. struct ucode_cpu_info *uci = ucode_cpu_info + smp_processor_id();
  288. int err = 0;
  289. /*
  290. * Check if the system resume is in progress (uci->valid != NULL),
  291. * otherwise just request a firmware:
  292. */
  293. if (uci->valid) {
  294. err = microcode_resume_cpu(smp_processor_id());
  295. } else {
  296. collect_cpu_info(smp_processor_id());
  297. if (uci->valid && system_state == SYSTEM_RUNNING)
  298. err = microcode_ops->request_microcode_fw(
  299. smp_processor_id(),
  300. &microcode_pdev->dev);
  301. }
  302. if (!err)
  303. microcode_ops->apply_microcode(smp_processor_id());
  304. return err;
  305. }
  306. static int microcode_init_cpu(int cpu)
  307. {
  308. int err;
  309. mutex_lock(&microcode_mutex);
  310. err = work_on_cpu(cpu, microcode_update_cpu, NULL);
  311. mutex_unlock(&microcode_mutex);
  312. return err;
  313. }
  314. static int mc_sysdev_add(struct sys_device *sys_dev)
  315. {
  316. int err, cpu = sys_dev->id;
  317. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  318. if (!cpu_online(cpu))
  319. return 0;
  320. pr_debug("microcode: CPU%d added\n", cpu);
  321. memset(uci, 0, sizeof(*uci));
  322. err = sysfs_create_group(&sys_dev->kobj, &mc_attr_group);
  323. if (err)
  324. return err;
  325. err = microcode_init_cpu(cpu);
  326. return err;
  327. }
  328. static int mc_sysdev_remove(struct sys_device *sys_dev)
  329. {
  330. int cpu = sys_dev->id;
  331. if (!cpu_online(cpu))
  332. return 0;
  333. pr_debug("microcode: CPU%d removed\n", cpu);
  334. microcode_fini_cpu(cpu);
  335. sysfs_remove_group(&sys_dev->kobj, &mc_attr_group);
  336. return 0;
  337. }
  338. static int mc_sysdev_resume(struct sys_device *dev)
  339. {
  340. int cpu = dev->id;
  341. if (!cpu_online(cpu))
  342. return 0;
  343. /* only CPU 0 will apply ucode here */
  344. microcode_update_cpu(NULL);
  345. return 0;
  346. }
  347. static struct sysdev_driver mc_sysdev_driver = {
  348. .add = mc_sysdev_add,
  349. .remove = mc_sysdev_remove,
  350. .resume = mc_sysdev_resume,
  351. };
  352. static __cpuinit int
  353. mc_cpu_callback(struct notifier_block *nb, unsigned long action, void *hcpu)
  354. {
  355. unsigned int cpu = (unsigned long)hcpu;
  356. struct sys_device *sys_dev;
  357. sys_dev = get_cpu_sysdev(cpu);
  358. switch (action) {
  359. case CPU_ONLINE:
  360. case CPU_ONLINE_FROZEN:
  361. if (microcode_init_cpu(cpu))
  362. printk(KERN_ERR "microcode: failed to init CPU%d\n",
  363. cpu);
  364. case CPU_DOWN_FAILED:
  365. case CPU_DOWN_FAILED_FROZEN:
  366. pr_debug("microcode: CPU%d added\n", cpu);
  367. if (sysfs_create_group(&sys_dev->kobj, &mc_attr_group))
  368. printk(KERN_ERR "microcode: Failed to create the sysfs "
  369. "group for CPU%d\n", cpu);
  370. break;
  371. case CPU_DOWN_PREPARE:
  372. case CPU_DOWN_PREPARE_FROZEN:
  373. /* Suspend is in progress, only remove the interface */
  374. sysfs_remove_group(&sys_dev->kobj, &mc_attr_group);
  375. pr_debug("microcode: CPU%d removed\n", cpu);
  376. break;
  377. case CPU_DEAD:
  378. case CPU_UP_CANCELED_FROZEN:
  379. /* The CPU refused to come up during a system resume */
  380. microcode_fini_cpu(cpu);
  381. break;
  382. }
  383. return NOTIFY_OK;
  384. }
  385. static struct notifier_block __refdata mc_cpu_notifier = {
  386. .notifier_call = mc_cpu_callback,
  387. };
  388. static int __init microcode_init(void)
  389. {
  390. struct cpuinfo_x86 *c = &cpu_data(0);
  391. int error;
  392. if (c->x86_vendor == X86_VENDOR_INTEL)
  393. microcode_ops = init_intel_microcode();
  394. else if (c->x86_vendor == X86_VENDOR_AMD)
  395. microcode_ops = init_amd_microcode();
  396. if (!microcode_ops) {
  397. printk(KERN_ERR "microcode: no support for this CPU vendor\n");
  398. return -ENODEV;
  399. }
  400. error = microcode_dev_init();
  401. if (error)
  402. return error;
  403. microcode_pdev = platform_device_register_simple("microcode", -1,
  404. NULL, 0);
  405. if (IS_ERR(microcode_pdev)) {
  406. microcode_dev_exit();
  407. return PTR_ERR(microcode_pdev);
  408. }
  409. get_online_cpus();
  410. error = sysdev_driver_register(&cpu_sysdev_class, &mc_sysdev_driver);
  411. put_online_cpus();
  412. if (error) {
  413. microcode_dev_exit();
  414. platform_device_unregister(microcode_pdev);
  415. return error;
  416. }
  417. register_hotcpu_notifier(&mc_cpu_notifier);
  418. printk(KERN_INFO
  419. "Microcode Update Driver: v" MICROCODE_VERSION
  420. " <tigran@aivazian.fsnet.co.uk>,"
  421. " Peter Oruba\n");
  422. return 0;
  423. }
  424. static void __exit microcode_exit(void)
  425. {
  426. microcode_dev_exit();
  427. unregister_hotcpu_notifier(&mc_cpu_notifier);
  428. get_online_cpus();
  429. sysdev_driver_unregister(&cpu_sysdev_class, &mc_sysdev_driver);
  430. put_online_cpus();
  431. platform_device_unregister(microcode_pdev);
  432. microcode_ops = NULL;
  433. printk(KERN_INFO
  434. "Microcode Update Driver: v" MICROCODE_VERSION " removed.\n");
  435. }
  436. module_init(microcode_init);
  437. module_exit(microcode_exit);