microcode_core.c 14 KB

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