microcode_core.c 14 KB

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