microcode_core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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/miscdevice.h>
  75. #include <linux/capability.h>
  76. #include <linux/kernel.h>
  77. #include <linux/module.h>
  78. #include <linux/mutex.h>
  79. #include <linux/cpu.h>
  80. #include <linux/fs.h>
  81. #include <linux/mm.h>
  82. #include <asm/microcode.h>
  83. #include <asm/processor.h>
  84. MODULE_DESCRIPTION("Microcode Update Driver");
  85. MODULE_AUTHOR("Tigran Aivazian <tigran@aivazian.fsnet.co.uk>");
  86. MODULE_LICENSE("GPL");
  87. #define MICROCODE_VERSION "2.00"
  88. static struct microcode_ops *microcode_ops;
  89. /*
  90. * Synchronization.
  91. *
  92. * All non cpu-hotplug-callback call sites use:
  93. *
  94. * - microcode_mutex to synchronize with each other;
  95. * - get/put_online_cpus() to synchronize with
  96. * the cpu-hotplug-callback call sites.
  97. *
  98. * We guarantee that only a single cpu is being
  99. * updated at any particular moment of time.
  100. */
  101. static DEFINE_MUTEX(microcode_mutex);
  102. struct ucode_cpu_info ucode_cpu_info[NR_CPUS];
  103. EXPORT_SYMBOL_GPL(ucode_cpu_info);
  104. /*
  105. * Operations that are run on a target cpu:
  106. */
  107. struct cpu_info_ctx {
  108. struct cpu_signature *cpu_sig;
  109. int err;
  110. };
  111. static void collect_cpu_info_local(void *arg)
  112. {
  113. struct cpu_info_ctx *ctx = arg;
  114. ctx->err = microcode_ops->collect_cpu_info(smp_processor_id(),
  115. ctx->cpu_sig);
  116. }
  117. static int collect_cpu_info_on_target(int cpu, struct cpu_signature *cpu_sig)
  118. {
  119. struct cpu_info_ctx ctx = { .cpu_sig = cpu_sig, .err = 0 };
  120. int ret;
  121. ret = smp_call_function_single(cpu, collect_cpu_info_local, &ctx, 1);
  122. if (!ret)
  123. ret = ctx.err;
  124. return ret;
  125. }
  126. static int collect_cpu_info(int cpu)
  127. {
  128. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  129. int ret;
  130. memset(uci, 0, sizeof(*uci));
  131. ret = collect_cpu_info_on_target(cpu, &uci->cpu_sig);
  132. if (!ret)
  133. uci->valid = 1;
  134. return ret;
  135. }
  136. struct apply_microcode_ctx {
  137. int err;
  138. };
  139. static void apply_microcode_local(void *arg)
  140. {
  141. struct apply_microcode_ctx *ctx = arg;
  142. ctx->err = microcode_ops->apply_microcode(smp_processor_id());
  143. }
  144. static int apply_microcode_on_target(int cpu)
  145. {
  146. struct apply_microcode_ctx ctx = { .err = 0 };
  147. int ret;
  148. ret = smp_call_function_single(cpu, apply_microcode_local, &ctx, 1);
  149. if (!ret)
  150. ret = ctx.err;
  151. return ret;
  152. }
  153. #ifdef CONFIG_MICROCODE_OLD_INTERFACE
  154. static int do_microcode_update(const void __user *buf, size_t size)
  155. {
  156. int error = 0;
  157. int cpu;
  158. for_each_online_cpu(cpu) {
  159. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  160. enum ucode_state ustate;
  161. if (!uci->valid)
  162. continue;
  163. ustate = microcode_ops->request_microcode_user(cpu, buf, size);
  164. if (ustate == UCODE_ERROR) {
  165. error = -1;
  166. break;
  167. } else if (ustate == UCODE_OK)
  168. apply_microcode_on_target(cpu);
  169. }
  170. return error;
  171. }
  172. static int microcode_open(struct inode *unused1, struct file *unused2)
  173. {
  174. return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
  175. }
  176. static ssize_t microcode_write(struct file *file, const char __user *buf,
  177. size_t len, loff_t *ppos)
  178. {
  179. ssize_t ret = -EINVAL;
  180. if ((len >> PAGE_SHIFT) > totalram_pages) {
  181. pr_err("microcode: too much data (max %ld pages)\n", totalram_pages);
  182. return ret;
  183. }
  184. get_online_cpus();
  185. mutex_lock(&microcode_mutex);
  186. if (do_microcode_update(buf, len) == 0)
  187. ret = (ssize_t)len;
  188. mutex_unlock(&microcode_mutex);
  189. put_online_cpus();
  190. return ret;
  191. }
  192. static const struct file_operations microcode_fops = {
  193. .owner = THIS_MODULE,
  194. .write = microcode_write,
  195. .open = microcode_open,
  196. };
  197. static struct miscdevice microcode_dev = {
  198. .minor = MICROCODE_MINOR,
  199. .name = "microcode",
  200. .nodename = "cpu/microcode",
  201. .fops = &microcode_fops,
  202. };
  203. static int __init microcode_dev_init(void)
  204. {
  205. int error;
  206. error = misc_register(&microcode_dev);
  207. if (error) {
  208. pr_err("microcode: can't misc_register on minor=%d\n", MICROCODE_MINOR);
  209. return error;
  210. }
  211. return 0;
  212. }
  213. static void microcode_dev_exit(void)
  214. {
  215. misc_deregister(&microcode_dev);
  216. }
  217. MODULE_ALIAS_MISCDEV(MICROCODE_MINOR);
  218. #else
  219. #define microcode_dev_init() 0
  220. #define microcode_dev_exit() do { } while (0)
  221. #endif
  222. /* fake device for request_firmware */
  223. static struct platform_device *microcode_pdev;
  224. static int reload_for_cpu(int cpu)
  225. {
  226. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  227. int err = 0;
  228. mutex_lock(&microcode_mutex);
  229. if (uci->valid) {
  230. enum ucode_state ustate;
  231. ustate = microcode_ops->request_microcode_fw(cpu, &microcode_pdev->dev);
  232. if (ustate == UCODE_OK)
  233. apply_microcode_on_target(cpu);
  234. else
  235. if (ustate == UCODE_ERROR)
  236. err = -EINVAL;
  237. }
  238. mutex_unlock(&microcode_mutex);
  239. return err;
  240. }
  241. static ssize_t reload_store(struct sys_device *dev,
  242. struct sysdev_attribute *attr,
  243. const char *buf, size_t size)
  244. {
  245. unsigned long val;
  246. int cpu = dev->id;
  247. int ret = 0;
  248. char *end;
  249. val = simple_strtoul(buf, &end, 0);
  250. if (end == buf)
  251. return -EINVAL;
  252. if (val == 1) {
  253. get_online_cpus();
  254. if (cpu_online(cpu))
  255. ret = reload_for_cpu(cpu);
  256. put_online_cpus();
  257. }
  258. if (!ret)
  259. ret = size;
  260. return ret;
  261. }
  262. static ssize_t version_show(struct sys_device *dev,
  263. struct sysdev_attribute *attr, char *buf)
  264. {
  265. struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
  266. return sprintf(buf, "0x%x\n", uci->cpu_sig.rev);
  267. }
  268. static ssize_t pf_show(struct sys_device *dev,
  269. struct sysdev_attribute *attr, char *buf)
  270. {
  271. struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
  272. return sprintf(buf, "0x%x\n", uci->cpu_sig.pf);
  273. }
  274. static SYSDEV_ATTR(reload, 0200, NULL, reload_store);
  275. static SYSDEV_ATTR(version, 0400, version_show, NULL);
  276. static SYSDEV_ATTR(processor_flags, 0400, pf_show, NULL);
  277. static struct attribute *mc_default_attrs[] = {
  278. &attr_reload.attr,
  279. &attr_version.attr,
  280. &attr_processor_flags.attr,
  281. NULL
  282. };
  283. static struct attribute_group mc_attr_group = {
  284. .attrs = mc_default_attrs,
  285. .name = "microcode",
  286. };
  287. static void microcode_fini_cpu(int cpu)
  288. {
  289. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  290. microcode_ops->microcode_fini_cpu(cpu);
  291. uci->valid = 0;
  292. }
  293. static enum ucode_state microcode_resume_cpu(int cpu)
  294. {
  295. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  296. if (!uci->mc)
  297. return UCODE_NFOUND;
  298. pr_debug("microcode: CPU%d updated upon resume\n", cpu);
  299. apply_microcode_on_target(cpu);
  300. return UCODE_OK;
  301. }
  302. static enum ucode_state microcode_init_cpu(int cpu)
  303. {
  304. enum ucode_state ustate;
  305. if (collect_cpu_info(cpu))
  306. return UCODE_ERROR;
  307. /* --dimm. Trigger a delayed update? */
  308. if (system_state != SYSTEM_RUNNING)
  309. return UCODE_NFOUND;
  310. ustate = microcode_ops->request_microcode_fw(cpu, &microcode_pdev->dev);
  311. if (ustate == UCODE_OK) {
  312. pr_debug("microcode: CPU%d updated upon init\n", cpu);
  313. apply_microcode_on_target(cpu);
  314. }
  315. return ustate;
  316. }
  317. static enum ucode_state microcode_update_cpu(int cpu)
  318. {
  319. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  320. enum ucode_state ustate;
  321. if (uci->valid)
  322. ustate = microcode_resume_cpu(cpu);
  323. else
  324. ustate = microcode_init_cpu(cpu);
  325. return ustate;
  326. }
  327. static int mc_sysdev_add(struct sys_device *sys_dev)
  328. {
  329. int err, cpu = sys_dev->id;
  330. if (!cpu_online(cpu))
  331. return 0;
  332. pr_debug("microcode: CPU%d added\n", cpu);
  333. err = sysfs_create_group(&sys_dev->kobj, &mc_attr_group);
  334. if (err)
  335. return err;
  336. if (microcode_init_cpu(cpu) == UCODE_ERROR)
  337. err = -EINVAL;
  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. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  354. if (!cpu_online(cpu))
  355. return 0;
  356. /*
  357. * All non-bootup cpus are still disabled,
  358. * so only CPU 0 will apply ucode here.
  359. *
  360. * Moreover, there can be no concurrent
  361. * updates from any other places at this point.
  362. */
  363. WARN_ON(cpu != 0);
  364. if (uci->valid && uci->mc)
  365. microcode_ops->apply_microcode(cpu);
  366. return 0;
  367. }
  368. static struct sysdev_driver mc_sysdev_driver = {
  369. .add = mc_sysdev_add,
  370. .remove = mc_sysdev_remove,
  371. .resume = mc_sysdev_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("microcode: CPU%d added\n", cpu);
  386. if (sysfs_create_group(&sys_dev->kobj, &mc_attr_group))
  387. pr_err("microcode: 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("microcode: 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("microcode: 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_hotcpu_notifier(&mc_cpu_notifier);
  437. pr_info("Microcode Update Driver: v" MICROCODE_VERSION
  438. " <tigran@aivazian.fsnet.co.uk>,"
  439. " 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. get_online_cpus();
  448. mutex_lock(&microcode_mutex);
  449. sysdev_driver_unregister(&cpu_sysdev_class, &mc_sysdev_driver);
  450. mutex_unlock(&microcode_mutex);
  451. put_online_cpus();
  452. platform_device_unregister(microcode_pdev);
  453. microcode_ops = NULL;
  454. pr_info("Microcode Update Driver: v" MICROCODE_VERSION " removed.\n");
  455. }
  456. module_exit(microcode_exit);