microcode.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /*
  2. * Intel CPU Microcode Update Driver for Linux
  3. *
  4. * Copyright (C) 2000-2004 Tigran Aivazian
  5. *
  6. * This driver allows to upgrade microcode on Intel processors
  7. * belonging to IA-32 family - PentiumPro, Pentium II,
  8. * Pentium III, Xeon, Pentium 4, etc.
  9. *
  10. * Reference: Section 8.10 of Volume III, Intel Pentium 4 Manual,
  11. * Order Number 245472 or free download from:
  12. *
  13. * http://developer.intel.com/design/pentium4/manuals/245472.htm
  14. *
  15. * For more information, go to http://www.urbanmyth.org/microcode
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * as published by the Free Software Foundation; either version
  20. * 2 of the License, or (at your option) any later version.
  21. *
  22. * 1.0 16 Feb 2000, Tigran Aivazian <tigran@sco.com>
  23. * Initial release.
  24. * 1.01 18 Feb 2000, Tigran Aivazian <tigran@sco.com>
  25. * Added read() support + cleanups.
  26. * 1.02 21 Feb 2000, Tigran Aivazian <tigran@sco.com>
  27. * Added 'device trimming' support. open(O_WRONLY) zeroes
  28. * and frees the saved copy of applied microcode.
  29. * 1.03 29 Feb 2000, Tigran Aivazian <tigran@sco.com>
  30. * Made to use devfs (/dev/cpu/microcode) + cleanups.
  31. * 1.04 06 Jun 2000, Simon Trimmer <simon@veritas.com>
  32. * Added misc device support (now uses both devfs and misc).
  33. * Added MICROCODE_IOCFREE ioctl to clear memory.
  34. * 1.05 09 Jun 2000, Simon Trimmer <simon@veritas.com>
  35. * Messages for error cases (non Intel & no suitable microcode).
  36. * 1.06 03 Aug 2000, Tigran Aivazian <tigran@veritas.com>
  37. * Removed ->release(). Removed exclusive open and status bitmap.
  38. * Added microcode_rwsem to serialize read()/write()/ioctl().
  39. * Removed global kernel lock usage.
  40. * 1.07 07 Sep 2000, Tigran Aivazian <tigran@veritas.com>
  41. * Write 0 to 0x8B msr and then cpuid before reading revision,
  42. * so that it works even if there were no update done by the
  43. * BIOS. Otherwise, reading from 0x8B gives junk (which happened
  44. * to be 0 on my machine which is why it worked even when I
  45. * disabled update by the BIOS)
  46. * Thanks to Eric W. Biederman <ebiederman@lnxi.com> for the fix.
  47. * 1.08 11 Dec 2000, Richard Schaal <richard.schaal@intel.com> and
  48. * Tigran Aivazian <tigran@veritas.com>
  49. * Intel Pentium 4 processor support and bugfixes.
  50. * 1.09 30 Oct 2001, Tigran Aivazian <tigran@veritas.com>
  51. * Bugfix for HT (Hyper-Threading) enabled processors
  52. * whereby processor resources are shared by all logical processors
  53. * in a single CPU package.
  54. * 1.10 28 Feb 2002 Asit K Mallick <asit.k.mallick@intel.com> and
  55. * Tigran Aivazian <tigran@veritas.com>,
  56. * Serialize updates as required on HT processors due to speculative
  57. * nature of implementation.
  58. * 1.11 22 Mar 2002 Tigran Aivazian <tigran@veritas.com>
  59. * Fix the panic when writing zero-length microcode chunk.
  60. * 1.12 29 Sep 2003 Nitin Kamble <nitin.a.kamble@intel.com>,
  61. * Jun Nakajima <jun.nakajima@intel.com>
  62. * Support for the microcode updates in the new format.
  63. * 1.13 10 Oct 2003 Tigran Aivazian <tigran@veritas.com>
  64. * Removed ->read() method and obsoleted MICROCODE_IOCFREE ioctl
  65. * because we no longer hold a copy of applied microcode
  66. * in kernel memory.
  67. * 1.14 25 Jun 2004 Tigran Aivazian <tigran@veritas.com>
  68. * Fix sigmatch() macro to handle old CPUs with pf == 0.
  69. * Thanks to Stuart Swales for pointing out this bug.
  70. */
  71. //#define DEBUG /* pr_debug */
  72. #include <linux/capability.h>
  73. #include <linux/kernel.h>
  74. #include <linux/init.h>
  75. #include <linux/sched.h>
  76. #include <linux/module.h>
  77. #include <linux/slab.h>
  78. #include <linux/vmalloc.h>
  79. #include <linux/miscdevice.h>
  80. #include <linux/spinlock.h>
  81. #include <linux/mm.h>
  82. #include <asm/msr.h>
  83. #include <asm/uaccess.h>
  84. #include <asm/processor.h>
  85. MODULE_DESCRIPTION("Intel CPU (IA-32) Microcode Update Driver");
  86. MODULE_AUTHOR("Tigran Aivazian <tigran@veritas.com>");
  87. MODULE_LICENSE("GPL");
  88. #define MICROCODE_VERSION "1.14"
  89. #define DEFAULT_UCODE_DATASIZE (2000) /* 2000 bytes */
  90. #define MC_HEADER_SIZE (sizeof (microcode_header_t)) /* 48 bytes */
  91. #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE) /* 2048 bytes */
  92. #define EXT_HEADER_SIZE (sizeof (struct extended_sigtable)) /* 20 bytes */
  93. #define EXT_SIGNATURE_SIZE (sizeof (struct extended_signature)) /* 12 bytes */
  94. #define DWSIZE (sizeof (u32))
  95. #define get_totalsize(mc) \
  96. (((microcode_t *)mc)->hdr.totalsize ? \
  97. ((microcode_t *)mc)->hdr.totalsize : DEFAULT_UCODE_TOTALSIZE)
  98. #define get_datasize(mc) \
  99. (((microcode_t *)mc)->hdr.datasize ? \
  100. ((microcode_t *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE)
  101. #define sigmatch(s1, s2, p1, p2) \
  102. (((s1) == (s2)) && (((p1) & (p2)) || (((p1) == 0) && ((p2) == 0))))
  103. #define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE)
  104. /* serialize access to the physical write to MSR 0x79 */
  105. static DEFINE_SPINLOCK(microcode_update_lock);
  106. /* no concurrent ->write()s are allowed on /dev/cpu/microcode */
  107. static DECLARE_MUTEX(microcode_sem);
  108. static void __user *user_buffer; /* user area microcode data buffer */
  109. static unsigned int user_buffer_size; /* it's size */
  110. typedef enum mc_error_code {
  111. MC_SUCCESS = 0,
  112. MC_NOTFOUND = 1,
  113. MC_MARKED = 2,
  114. MC_ALLOCATED = 3,
  115. } mc_error_code_t;
  116. static struct ucode_cpu_info {
  117. unsigned int sig;
  118. unsigned int pf;
  119. unsigned int rev;
  120. unsigned int cksum;
  121. mc_error_code_t err;
  122. microcode_t *mc;
  123. } ucode_cpu_info[NR_CPUS];
  124. static int microcode_open (struct inode *unused1, struct file *unused2)
  125. {
  126. return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
  127. }
  128. static void collect_cpu_info (void *unused)
  129. {
  130. int cpu_num = smp_processor_id();
  131. struct cpuinfo_x86 *c = cpu_data + cpu_num;
  132. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  133. unsigned int val[2];
  134. uci->sig = uci->pf = uci->rev = uci->cksum = 0;
  135. uci->err = MC_NOTFOUND;
  136. uci->mc = NULL;
  137. if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
  138. cpu_has(c, X86_FEATURE_IA64)) {
  139. printk(KERN_ERR "microcode: CPU%d not a capable Intel processor\n", cpu_num);
  140. return;
  141. } else {
  142. uci->sig = cpuid_eax(0x00000001);
  143. if ((c->x86_model >= 5) || (c->x86 > 6)) {
  144. /* get processor flags from MSR 0x17 */
  145. rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  146. uci->pf = 1 << ((val[1] >> 18) & 7);
  147. }
  148. }
  149. wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  150. /* see notes above for revision 1.07. Apparent chip bug */
  151. sync_core();
  152. /* get the current revision from MSR 0x8B */
  153. rdmsr(MSR_IA32_UCODE_REV, val[0], uci->rev);
  154. pr_debug("microcode: collect_cpu_info : sig=0x%x, pf=0x%x, rev=0x%x\n",
  155. uci->sig, uci->pf, uci->rev);
  156. }
  157. static inline void mark_microcode_update (int cpu_num, microcode_header_t *mc_header, int sig, int pf, int cksum)
  158. {
  159. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  160. pr_debug("Microcode Found.\n");
  161. pr_debug(" Header Revision 0x%x\n", mc_header->hdrver);
  162. pr_debug(" Loader Revision 0x%x\n", mc_header->ldrver);
  163. pr_debug(" Revision 0x%x \n", mc_header->rev);
  164. pr_debug(" Date %x/%x/%x\n",
  165. ((mc_header->date >> 24 ) & 0xff),
  166. ((mc_header->date >> 16 ) & 0xff),
  167. (mc_header->date & 0xFFFF));
  168. pr_debug(" Signature 0x%x\n", sig);
  169. pr_debug(" Type 0x%x Family 0x%x Model 0x%x Stepping 0x%x\n",
  170. ((sig >> 12) & 0x3),
  171. ((sig >> 8) & 0xf),
  172. ((sig >> 4) & 0xf),
  173. ((sig & 0xf)));
  174. pr_debug(" Processor Flags 0x%x\n", pf);
  175. pr_debug(" Checksum 0x%x\n", cksum);
  176. if (mc_header->rev < uci->rev) {
  177. printk(KERN_ERR "microcode: CPU%d not 'upgrading' to earlier revision"
  178. " 0x%x (current=0x%x)\n", cpu_num, mc_header->rev, uci->rev);
  179. goto out;
  180. } else if (mc_header->rev == uci->rev) {
  181. /* notify the caller of success on this cpu */
  182. uci->err = MC_SUCCESS;
  183. printk(KERN_ERR "microcode: CPU%d already at revision"
  184. " 0x%x (current=0x%x)\n", cpu_num, mc_header->rev, uci->rev);
  185. goto out;
  186. }
  187. pr_debug("microcode: CPU%d found a matching microcode update with "
  188. " revision 0x%x (current=0x%x)\n", cpu_num, mc_header->rev, uci->rev);
  189. uci->cksum = cksum;
  190. uci->pf = pf; /* keep the original mc pf for cksum calculation */
  191. uci->err = MC_MARKED; /* found the match */
  192. out:
  193. return;
  194. }
  195. static int find_matching_ucodes (void)
  196. {
  197. int cursor = 0;
  198. int error = 0;
  199. while (cursor + MC_HEADER_SIZE < user_buffer_size) {
  200. microcode_header_t mc_header;
  201. void *newmc = NULL;
  202. int i, sum, cpu_num, allocated_flag, total_size, data_size, ext_table_size;
  203. if (copy_from_user(&mc_header, user_buffer + cursor, MC_HEADER_SIZE)) {
  204. printk(KERN_ERR "microcode: error! Can not read user data\n");
  205. error = -EFAULT;
  206. goto out;
  207. }
  208. total_size = get_totalsize(&mc_header);
  209. if ((cursor + total_size > user_buffer_size) || (total_size < DEFAULT_UCODE_TOTALSIZE)) {
  210. printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
  211. error = -EINVAL;
  212. goto out;
  213. }
  214. data_size = get_datasize(&mc_header);
  215. if ((data_size + MC_HEADER_SIZE > total_size) || (data_size < DEFAULT_UCODE_DATASIZE)) {
  216. printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
  217. error = -EINVAL;
  218. goto out;
  219. }
  220. if (mc_header.ldrver != 1 || mc_header.hdrver != 1) {
  221. printk(KERN_ERR "microcode: error! Unknown microcode update format\n");
  222. error = -EINVAL;
  223. goto out;
  224. }
  225. for (cpu_num = 0; cpu_num < num_online_cpus(); cpu_num++) {
  226. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  227. if (uci->err != MC_NOTFOUND) /* already found a match or not an online cpu*/
  228. continue;
  229. if (sigmatch(mc_header.sig, uci->sig, mc_header.pf, uci->pf))
  230. mark_microcode_update(cpu_num, &mc_header, mc_header.sig, mc_header.pf, mc_header.cksum);
  231. }
  232. ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
  233. if (ext_table_size) {
  234. struct extended_sigtable ext_header;
  235. struct extended_signature ext_sig;
  236. int ext_sigcount;
  237. if ((ext_table_size < EXT_HEADER_SIZE)
  238. || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
  239. printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
  240. error = -EINVAL;
  241. goto out;
  242. }
  243. if (copy_from_user(&ext_header, user_buffer + cursor
  244. + MC_HEADER_SIZE + data_size, EXT_HEADER_SIZE)) {
  245. printk(KERN_ERR "microcode: error! Can not read user data\n");
  246. error = -EFAULT;
  247. goto out;
  248. }
  249. if (ext_table_size != exttable_size(&ext_header)) {
  250. printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
  251. error = -EFAULT;
  252. goto out;
  253. }
  254. ext_sigcount = ext_header.count;
  255. for (i = 0; i < ext_sigcount; i++) {
  256. if (copy_from_user(&ext_sig, user_buffer + cursor + MC_HEADER_SIZE + data_size + EXT_HEADER_SIZE
  257. + EXT_SIGNATURE_SIZE * i, EXT_SIGNATURE_SIZE)) {
  258. printk(KERN_ERR "microcode: error! Can not read user data\n");
  259. error = -EFAULT;
  260. goto out;
  261. }
  262. for (cpu_num = 0; cpu_num < num_online_cpus(); cpu_num++) {
  263. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  264. if (uci->err != MC_NOTFOUND) /* already found a match or not an online cpu*/
  265. continue;
  266. if (sigmatch(ext_sig.sig, uci->sig, ext_sig.pf, uci->pf)) {
  267. mark_microcode_update(cpu_num, &mc_header, ext_sig.sig, ext_sig.pf, ext_sig.cksum);
  268. }
  269. }
  270. }
  271. }
  272. /* now check if any cpu has matched */
  273. for (cpu_num = 0, allocated_flag = 0, sum = 0; cpu_num < num_online_cpus(); cpu_num++) {
  274. if (ucode_cpu_info[cpu_num].err == MC_MARKED) {
  275. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  276. if (!allocated_flag) {
  277. allocated_flag = 1;
  278. newmc = vmalloc(total_size);
  279. if (!newmc) {
  280. printk(KERN_ERR "microcode: error! Can not allocate memory\n");
  281. error = -ENOMEM;
  282. goto out;
  283. }
  284. if (copy_from_user(newmc + MC_HEADER_SIZE,
  285. user_buffer + cursor + MC_HEADER_SIZE,
  286. total_size - MC_HEADER_SIZE)) {
  287. printk(KERN_ERR "microcode: error! Can not read user data\n");
  288. vfree(newmc);
  289. error = -EFAULT;
  290. goto out;
  291. }
  292. memcpy(newmc, &mc_header, MC_HEADER_SIZE);
  293. /* check extended table checksum */
  294. if (ext_table_size) {
  295. int ext_table_sum = 0;
  296. int * ext_tablep = (((void *) newmc) + MC_HEADER_SIZE + data_size);
  297. i = ext_table_size / DWSIZE;
  298. while (i--) ext_table_sum += ext_tablep[i];
  299. if (ext_table_sum) {
  300. printk(KERN_WARNING "microcode: aborting, bad extended signature table checksum\n");
  301. vfree(newmc);
  302. error = -EINVAL;
  303. goto out;
  304. }
  305. }
  306. /* calculate the checksum */
  307. i = (MC_HEADER_SIZE + data_size) / DWSIZE;
  308. while (i--) sum += ((int *)newmc)[i];
  309. sum -= (mc_header.sig + mc_header.pf + mc_header.cksum);
  310. }
  311. ucode_cpu_info[cpu_num].mc = newmc;
  312. ucode_cpu_info[cpu_num].err = MC_ALLOCATED; /* mc updated */
  313. if (sum + uci->sig + uci->pf + uci->cksum != 0) {
  314. printk(KERN_ERR "microcode: CPU%d aborting, bad checksum\n", cpu_num);
  315. error = -EINVAL;
  316. goto out;
  317. }
  318. }
  319. }
  320. cursor += total_size; /* goto the next update patch */
  321. } /* end of while */
  322. out:
  323. return error;
  324. }
  325. static void do_update_one (void * unused)
  326. {
  327. unsigned long flags;
  328. unsigned int val[2];
  329. int cpu_num = smp_processor_id();
  330. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  331. if (uci->mc == NULL) {
  332. printk(KERN_INFO "microcode: No new microcode data for CPU%d\n", cpu_num);
  333. return;
  334. }
  335. /* serialize access to the physical write to MSR 0x79 */
  336. spin_lock_irqsave(&microcode_update_lock, flags);
  337. /* write microcode via MSR 0x79 */
  338. wrmsr(MSR_IA32_UCODE_WRITE,
  339. (unsigned long) uci->mc->bits,
  340. (unsigned long) uci->mc->bits >> 16 >> 16);
  341. wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  342. /* see notes above for revision 1.07. Apparent chip bug */
  343. sync_core();
  344. /* get the current revision from MSR 0x8B */
  345. rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  346. /* notify the caller of success on this cpu */
  347. uci->err = MC_SUCCESS;
  348. spin_unlock_irqrestore(&microcode_update_lock, flags);
  349. printk(KERN_INFO "microcode: CPU%d updated from revision "
  350. "0x%x to 0x%x, date = %08x \n",
  351. cpu_num, uci->rev, val[1], uci->mc->hdr.date);
  352. return;
  353. }
  354. static int do_microcode_update (void)
  355. {
  356. int i, error;
  357. if (on_each_cpu(collect_cpu_info, NULL, 1, 1) != 0) {
  358. printk(KERN_ERR "microcode: Error! Could not run on all processors\n");
  359. error = -EIO;
  360. goto out;
  361. }
  362. if ((error = find_matching_ucodes())) {
  363. printk(KERN_ERR "microcode: Error in the microcode data\n");
  364. goto out_free;
  365. }
  366. if (on_each_cpu(do_update_one, NULL, 1, 1) != 0) {
  367. printk(KERN_ERR "microcode: Error! Could not run on all processors\n");
  368. error = -EIO;
  369. }
  370. out_free:
  371. for (i = 0; i < num_online_cpus(); i++) {
  372. if (ucode_cpu_info[i].mc) {
  373. int j;
  374. void *tmp = ucode_cpu_info[i].mc;
  375. vfree(tmp);
  376. for (j = i; j < num_online_cpus(); j++) {
  377. if (ucode_cpu_info[j].mc == tmp)
  378. ucode_cpu_info[j].mc = NULL;
  379. }
  380. }
  381. }
  382. out:
  383. return error;
  384. }
  385. static ssize_t microcode_write (struct file *file, const char __user *buf, size_t len, loff_t *ppos)
  386. {
  387. ssize_t ret;
  388. if (len < DEFAULT_UCODE_TOTALSIZE) {
  389. printk(KERN_ERR "microcode: not enough data\n");
  390. return -EINVAL;
  391. }
  392. if ((len >> PAGE_SHIFT) > num_physpages) {
  393. printk(KERN_ERR "microcode: too much data (max %ld pages)\n", num_physpages);
  394. return -EINVAL;
  395. }
  396. down(&microcode_sem);
  397. user_buffer = (void __user *) buf;
  398. user_buffer_size = (int) len;
  399. ret = do_microcode_update();
  400. if (!ret)
  401. ret = (ssize_t)len;
  402. up(&microcode_sem);
  403. return ret;
  404. }
  405. static int microcode_ioctl (struct inode *inode, struct file *file,
  406. unsigned int cmd, unsigned long arg)
  407. {
  408. switch (cmd) {
  409. /*
  410. * XXX: will be removed after microcode_ctl
  411. * is updated to ignore failure of this ioctl()
  412. */
  413. case MICROCODE_IOCFREE:
  414. return 0;
  415. default:
  416. return -EINVAL;
  417. }
  418. return -EINVAL;
  419. }
  420. static struct file_operations microcode_fops = {
  421. .owner = THIS_MODULE,
  422. .write = microcode_write,
  423. .ioctl = microcode_ioctl,
  424. .open = microcode_open,
  425. };
  426. static struct miscdevice microcode_dev = {
  427. .minor = MICROCODE_MINOR,
  428. .name = "microcode",
  429. .devfs_name = "cpu/microcode",
  430. .fops = &microcode_fops,
  431. };
  432. static int __init microcode_init (void)
  433. {
  434. int error;
  435. error = misc_register(&microcode_dev);
  436. if (error) {
  437. printk(KERN_ERR
  438. "microcode: can't misc_register on minor=%d\n",
  439. MICROCODE_MINOR);
  440. return error;
  441. }
  442. printk(KERN_INFO
  443. "IA-32 Microcode Update Driver: v" MICROCODE_VERSION " <tigran@veritas.com>\n");
  444. return 0;
  445. }
  446. static void __exit microcode_exit (void)
  447. {
  448. misc_deregister(&microcode_dev);
  449. printk(KERN_INFO "IA-32 Microcode Update Driver v" MICROCODE_VERSION " unregistered\n");
  450. }
  451. module_init(microcode_init)
  452. module_exit(microcode_exit)
  453. MODULE_ALIAS_MISCDEV(MICROCODE_MINOR);