microcode_core.c 14 KB

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