microcode_core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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/Assets/PDF/manual/253668.pdf
  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 *inode, struct file *file)
  174. {
  175. return capable(CAP_SYS_RAWIO) ? nonseekable_open(inode, file) : -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. .llseek = no_llseek,
  198. };
  199. static struct miscdevice microcode_dev = {
  200. .minor = MICROCODE_MINOR,
  201. .name = "microcode",
  202. .nodename = "cpu/microcode",
  203. .fops = &microcode_fops,
  204. };
  205. static int __init microcode_dev_init(void)
  206. {
  207. int error;
  208. error = misc_register(&microcode_dev);
  209. if (error) {
  210. pr_err("can't misc_register on minor=%d\n", MICROCODE_MINOR);
  211. return error;
  212. }
  213. return 0;
  214. }
  215. static void microcode_dev_exit(void)
  216. {
  217. misc_deregister(&microcode_dev);
  218. }
  219. MODULE_ALIAS_MISCDEV(MICROCODE_MINOR);
  220. MODULE_ALIAS("devname:cpu/microcode");
  221. #else
  222. #define microcode_dev_init() 0
  223. #define microcode_dev_exit() do { } while (0)
  224. #endif
  225. /* fake device for request_firmware */
  226. static struct platform_device *microcode_pdev;
  227. static int reload_for_cpu(int cpu)
  228. {
  229. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  230. int err = 0;
  231. mutex_lock(&microcode_mutex);
  232. if (uci->valid) {
  233. enum ucode_state ustate;
  234. ustate = microcode_ops->request_microcode_fw(cpu, &microcode_pdev->dev);
  235. if (ustate == UCODE_OK)
  236. apply_microcode_on_target(cpu);
  237. else
  238. if (ustate == UCODE_ERROR)
  239. err = -EINVAL;
  240. }
  241. mutex_unlock(&microcode_mutex);
  242. return err;
  243. }
  244. static ssize_t reload_store(struct sys_device *dev,
  245. struct sysdev_attribute *attr,
  246. const char *buf, size_t size)
  247. {
  248. unsigned long val;
  249. int cpu = dev->id;
  250. int ret = 0;
  251. char *end;
  252. val = simple_strtoul(buf, &end, 0);
  253. if (end == buf)
  254. return -EINVAL;
  255. if (val == 1) {
  256. get_online_cpus();
  257. if (cpu_online(cpu))
  258. ret = reload_for_cpu(cpu);
  259. put_online_cpus();
  260. }
  261. if (!ret)
  262. ret = size;
  263. return ret;
  264. }
  265. static ssize_t version_show(struct sys_device *dev,
  266. struct sysdev_attribute *attr, char *buf)
  267. {
  268. struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
  269. return sprintf(buf, "0x%x\n", uci->cpu_sig.rev);
  270. }
  271. static ssize_t pf_show(struct sys_device *dev,
  272. struct sysdev_attribute *attr, char *buf)
  273. {
  274. struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
  275. return sprintf(buf, "0x%x\n", uci->cpu_sig.pf);
  276. }
  277. static SYSDEV_ATTR(reload, 0200, NULL, reload_store);
  278. static SYSDEV_ATTR(version, 0400, version_show, NULL);
  279. static SYSDEV_ATTR(processor_flags, 0400, pf_show, NULL);
  280. static struct attribute *mc_default_attrs[] = {
  281. &attr_reload.attr,
  282. &attr_version.attr,
  283. &attr_processor_flags.attr,
  284. NULL
  285. };
  286. static struct attribute_group mc_attr_group = {
  287. .attrs = mc_default_attrs,
  288. .name = "microcode",
  289. };
  290. static void microcode_fini_cpu(int cpu)
  291. {
  292. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  293. microcode_ops->microcode_fini_cpu(cpu);
  294. uci->valid = 0;
  295. }
  296. static enum ucode_state microcode_resume_cpu(int cpu)
  297. {
  298. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  299. if (!uci->mc)
  300. return UCODE_NFOUND;
  301. pr_debug("CPU%d updated upon resume\n", cpu);
  302. apply_microcode_on_target(cpu);
  303. return UCODE_OK;
  304. }
  305. static enum ucode_state microcode_init_cpu(int cpu)
  306. {
  307. enum ucode_state ustate;
  308. if (collect_cpu_info(cpu))
  309. return UCODE_ERROR;
  310. /* --dimm. Trigger a delayed update? */
  311. if (system_state != SYSTEM_RUNNING)
  312. return UCODE_NFOUND;
  313. ustate = microcode_ops->request_microcode_fw(cpu, &microcode_pdev->dev);
  314. if (ustate == UCODE_OK) {
  315. pr_debug("CPU%d updated upon init\n", cpu);
  316. apply_microcode_on_target(cpu);
  317. }
  318. return ustate;
  319. }
  320. static enum ucode_state microcode_update_cpu(int cpu)
  321. {
  322. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  323. enum ucode_state ustate;
  324. if (uci->valid)
  325. ustate = microcode_resume_cpu(cpu);
  326. else
  327. ustate = microcode_init_cpu(cpu);
  328. return ustate;
  329. }
  330. static int mc_sysdev_add(struct sys_device *sys_dev)
  331. {
  332. int err, cpu = sys_dev->id;
  333. if (!cpu_online(cpu))
  334. return 0;
  335. pr_debug("CPU%d added\n", cpu);
  336. err = sysfs_create_group(&sys_dev->kobj, &mc_attr_group);
  337. if (err)
  338. return err;
  339. if (microcode_init_cpu(cpu) == UCODE_ERROR)
  340. err = -EINVAL;
  341. return err;
  342. }
  343. static int mc_sysdev_remove(struct sys_device *sys_dev)
  344. {
  345. int cpu = sys_dev->id;
  346. if (!cpu_online(cpu))
  347. return 0;
  348. pr_debug("CPU%d removed\n", cpu);
  349. microcode_fini_cpu(cpu);
  350. sysfs_remove_group(&sys_dev->kobj, &mc_attr_group);
  351. return 0;
  352. }
  353. static int mc_sysdev_resume(struct sys_device *dev)
  354. {
  355. int cpu = dev->id;
  356. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  357. if (!cpu_online(cpu))
  358. return 0;
  359. /*
  360. * All non-bootup cpus are still disabled,
  361. * so only CPU 0 will apply ucode here.
  362. *
  363. * Moreover, there can be no concurrent
  364. * updates from any other places at this point.
  365. */
  366. WARN_ON(cpu != 0);
  367. if (uci->valid && uci->mc)
  368. microcode_ops->apply_microcode(cpu);
  369. return 0;
  370. }
  371. static struct sysdev_driver mc_sysdev_driver = {
  372. .add = mc_sysdev_add,
  373. .remove = mc_sysdev_remove,
  374. .resume = mc_sysdev_resume,
  375. };
  376. static __cpuinit int
  377. mc_cpu_callback(struct notifier_block *nb, unsigned long action, void *hcpu)
  378. {
  379. unsigned int cpu = (unsigned long)hcpu;
  380. struct sys_device *sys_dev;
  381. sys_dev = get_cpu_sysdev(cpu);
  382. switch (action) {
  383. case CPU_ONLINE:
  384. case CPU_ONLINE_FROZEN:
  385. microcode_update_cpu(cpu);
  386. case CPU_DOWN_FAILED:
  387. case CPU_DOWN_FAILED_FROZEN:
  388. pr_debug("CPU%d added\n", cpu);
  389. if (sysfs_create_group(&sys_dev->kobj, &mc_attr_group))
  390. pr_err("Failed to create group for CPU%d\n", cpu);
  391. break;
  392. case CPU_DOWN_PREPARE:
  393. case CPU_DOWN_PREPARE_FROZEN:
  394. /* Suspend is in progress, only remove the interface */
  395. sysfs_remove_group(&sys_dev->kobj, &mc_attr_group);
  396. pr_debug("CPU%d removed\n", cpu);
  397. break;
  398. case CPU_DEAD:
  399. case CPU_UP_CANCELED_FROZEN:
  400. /* The CPU refused to come up during a system resume */
  401. microcode_fini_cpu(cpu);
  402. break;
  403. }
  404. return NOTIFY_OK;
  405. }
  406. static struct notifier_block __refdata mc_cpu_notifier = {
  407. .notifier_call = mc_cpu_callback,
  408. };
  409. static int __init microcode_init(void)
  410. {
  411. struct cpuinfo_x86 *c = &cpu_data(0);
  412. int error;
  413. if (c->x86_vendor == X86_VENDOR_INTEL)
  414. microcode_ops = init_intel_microcode();
  415. else if (c->x86_vendor == X86_VENDOR_AMD)
  416. microcode_ops = init_amd_microcode();
  417. if (!microcode_ops) {
  418. pr_err("no support for this CPU vendor\n");
  419. return -ENODEV;
  420. }
  421. microcode_pdev = platform_device_register_simple("microcode", -1,
  422. NULL, 0);
  423. if (IS_ERR(microcode_pdev)) {
  424. microcode_dev_exit();
  425. return PTR_ERR(microcode_pdev);
  426. }
  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. microcode_ops = NULL;
  456. pr_info("Microcode Update Driver: v" MICROCODE_VERSION " removed.\n");
  457. }
  458. module_exit(microcode_exit);