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