microcode.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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/cpumask.h>
  77. #include <linux/module.h>
  78. #include <linux/slab.h>
  79. #include <linux/vmalloc.h>
  80. #include <linux/miscdevice.h>
  81. #include <linux/spinlock.h>
  82. #include <linux/mm.h>
  83. #include <linux/mutex.h>
  84. #include <asm/msr.h>
  85. #include <asm/uaccess.h>
  86. #include <asm/processor.h>
  87. MODULE_DESCRIPTION("Intel CPU (IA-32) Microcode Update Driver");
  88. MODULE_AUTHOR("Tigran Aivazian <tigran@veritas.com>");
  89. MODULE_LICENSE("GPL");
  90. #define MICROCODE_VERSION "1.14"
  91. #define DEFAULT_UCODE_DATASIZE (2000) /* 2000 bytes */
  92. #define MC_HEADER_SIZE (sizeof (microcode_header_t)) /* 48 bytes */
  93. #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE) /* 2048 bytes */
  94. #define EXT_HEADER_SIZE (sizeof (struct extended_sigtable)) /* 20 bytes */
  95. #define EXT_SIGNATURE_SIZE (sizeof (struct extended_signature)) /* 12 bytes */
  96. #define DWSIZE (sizeof (u32))
  97. #define get_totalsize(mc) \
  98. (((microcode_t *)mc)->hdr.totalsize ? \
  99. ((microcode_t *)mc)->hdr.totalsize : DEFAULT_UCODE_TOTALSIZE)
  100. #define get_datasize(mc) \
  101. (((microcode_t *)mc)->hdr.datasize ? \
  102. ((microcode_t *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE)
  103. #define sigmatch(s1, s2, p1, p2) \
  104. (((s1) == (s2)) && (((p1) & (p2)) || (((p1) == 0) && ((p2) == 0))))
  105. #define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE)
  106. /* serialize access to the physical write to MSR 0x79 */
  107. static DEFINE_SPINLOCK(microcode_update_lock);
  108. /* no concurrent ->write()s are allowed on /dev/cpu/microcode */
  109. static DEFINE_MUTEX(microcode_mutex);
  110. static void __user *user_buffer; /* user area microcode data buffer */
  111. static unsigned int user_buffer_size; /* it's size */
  112. typedef enum mc_error_code {
  113. MC_SUCCESS = 0,
  114. MC_NOTFOUND = 1,
  115. MC_MARKED = 2,
  116. MC_ALLOCATED = 3,
  117. } mc_error_code_t;
  118. static struct ucode_cpu_info {
  119. unsigned int sig;
  120. unsigned int pf;
  121. unsigned int rev;
  122. unsigned int cksum;
  123. mc_error_code_t err;
  124. microcode_t *mc;
  125. } ucode_cpu_info[NR_CPUS];
  126. static int microcode_open (struct inode *unused1, struct file *unused2)
  127. {
  128. return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
  129. }
  130. static void collect_cpu_info (void *unused)
  131. {
  132. int cpu_num = smp_processor_id();
  133. struct cpuinfo_x86 *c = cpu_data + cpu_num;
  134. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  135. unsigned int val[2];
  136. uci->sig = uci->pf = uci->rev = uci->cksum = 0;
  137. uci->err = MC_NOTFOUND;
  138. uci->mc = NULL;
  139. if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
  140. cpu_has(c, X86_FEATURE_IA64)) {
  141. printk(KERN_ERR "microcode: CPU%d not a capable Intel processor\n", cpu_num);
  142. return;
  143. } else {
  144. uci->sig = cpuid_eax(0x00000001);
  145. if ((c->x86_model >= 5) || (c->x86 > 6)) {
  146. /* get processor flags from MSR 0x17 */
  147. rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  148. uci->pf = 1 << ((val[1] >> 18) & 7);
  149. }
  150. }
  151. wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  152. /* see notes above for revision 1.07. Apparent chip bug */
  153. sync_core();
  154. /* get the current revision from MSR 0x8B */
  155. rdmsr(MSR_IA32_UCODE_REV, val[0], uci->rev);
  156. pr_debug("microcode: collect_cpu_info : sig=0x%x, pf=0x%x, rev=0x%x\n",
  157. uci->sig, uci->pf, uci->rev);
  158. }
  159. static inline void mark_microcode_update (int cpu_num, microcode_header_t *mc_header, int sig, int pf, int cksum)
  160. {
  161. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  162. pr_debug("Microcode Found.\n");
  163. pr_debug(" Header Revision 0x%x\n", mc_header->hdrver);
  164. pr_debug(" Loader Revision 0x%x\n", mc_header->ldrver);
  165. pr_debug(" Revision 0x%x \n", mc_header->rev);
  166. pr_debug(" Date %x/%x/%x\n",
  167. ((mc_header->date >> 24 ) & 0xff),
  168. ((mc_header->date >> 16 ) & 0xff),
  169. (mc_header->date & 0xFFFF));
  170. pr_debug(" Signature 0x%x\n", sig);
  171. pr_debug(" Type 0x%x Family 0x%x Model 0x%x Stepping 0x%x\n",
  172. ((sig >> 12) & 0x3),
  173. ((sig >> 8) & 0xf),
  174. ((sig >> 4) & 0xf),
  175. ((sig & 0xf)));
  176. pr_debug(" Processor Flags 0x%x\n", pf);
  177. pr_debug(" Checksum 0x%x\n", cksum);
  178. if (mc_header->rev < uci->rev) {
  179. printk(KERN_ERR "microcode: CPU%d not 'upgrading' to earlier revision"
  180. " 0x%x (current=0x%x)\n", cpu_num, mc_header->rev, uci->rev);
  181. goto out;
  182. } else if (mc_header->rev == uci->rev) {
  183. /* notify the caller of success on this cpu */
  184. uci->err = MC_SUCCESS;
  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_each_online_cpu(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_each_online_cpu(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. allocated_flag = 0;
  274. sum = 0;
  275. for_each_online_cpu(cpu_num) {
  276. if (ucode_cpu_info[cpu_num].err == MC_MARKED) {
  277. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  278. if (!allocated_flag) {
  279. allocated_flag = 1;
  280. newmc = vmalloc(total_size);
  281. if (!newmc) {
  282. printk(KERN_ERR "microcode: error! Can not allocate memory\n");
  283. error = -ENOMEM;
  284. goto out;
  285. }
  286. if (copy_from_user(newmc + MC_HEADER_SIZE,
  287. user_buffer + cursor + MC_HEADER_SIZE,
  288. total_size - MC_HEADER_SIZE)) {
  289. printk(KERN_ERR "microcode: error! Can not read user data\n");
  290. vfree(newmc);
  291. error = -EFAULT;
  292. goto out;
  293. }
  294. memcpy(newmc, &mc_header, MC_HEADER_SIZE);
  295. /* check extended table checksum */
  296. if (ext_table_size) {
  297. int ext_table_sum = 0;
  298. int * ext_tablep = (((void *) newmc) + MC_HEADER_SIZE + data_size);
  299. i = ext_table_size / DWSIZE;
  300. while (i--) ext_table_sum += ext_tablep[i];
  301. if (ext_table_sum) {
  302. printk(KERN_WARNING "microcode: aborting, bad extended signature table checksum\n");
  303. vfree(newmc);
  304. error = -EINVAL;
  305. goto out;
  306. }
  307. }
  308. /* calculate the checksum */
  309. i = (MC_HEADER_SIZE + data_size) / DWSIZE;
  310. while (i--) sum += ((int *)newmc)[i];
  311. sum -= (mc_header.sig + mc_header.pf + mc_header.cksum);
  312. }
  313. ucode_cpu_info[cpu_num].mc = newmc;
  314. ucode_cpu_info[cpu_num].err = MC_ALLOCATED; /* mc updated */
  315. if (sum + uci->sig + uci->pf + uci->cksum != 0) {
  316. printk(KERN_ERR "microcode: CPU%d aborting, bad checksum\n", cpu_num);
  317. error = -EINVAL;
  318. goto out;
  319. }
  320. }
  321. }
  322. cursor += total_size; /* goto the next update patch */
  323. } /* end of while */
  324. out:
  325. return error;
  326. }
  327. static void do_update_one (void * unused)
  328. {
  329. unsigned long flags;
  330. unsigned int val[2];
  331. int cpu_num = smp_processor_id();
  332. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  333. if (uci->mc == NULL) {
  334. return;
  335. }
  336. /* serialize access to the physical write to MSR 0x79 */
  337. spin_lock_irqsave(&microcode_update_lock, flags);
  338. /* write microcode via MSR 0x79 */
  339. wrmsr(MSR_IA32_UCODE_WRITE,
  340. (unsigned long) uci->mc->bits,
  341. (unsigned long) uci->mc->bits >> 16 >> 16);
  342. wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  343. /* see notes above for revision 1.07. Apparent chip bug */
  344. sync_core();
  345. /* get the current revision from MSR 0x8B */
  346. rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  347. /* notify the caller of success on this cpu */
  348. uci->err = MC_SUCCESS;
  349. spin_unlock_irqrestore(&microcode_update_lock, flags);
  350. printk(KERN_INFO "microcode: CPU%d updated from revision "
  351. "0x%x to 0x%x, date = %08x \n",
  352. cpu_num, uci->rev, val[1], uci->mc->hdr.date);
  353. return;
  354. }
  355. static int do_microcode_update (void)
  356. {
  357. int i, error;
  358. if (on_each_cpu(collect_cpu_info, NULL, 1, 1) != 0) {
  359. printk(KERN_ERR "microcode: Error! Could not run on all processors\n");
  360. error = -EIO;
  361. goto out;
  362. }
  363. if ((error = find_matching_ucodes())) {
  364. printk(KERN_ERR "microcode: Error in the microcode data\n");
  365. goto out_free;
  366. }
  367. if (on_each_cpu(do_update_one, NULL, 1, 1) != 0) {
  368. printk(KERN_ERR "microcode: Error! Could not run on all processors\n");
  369. error = -EIO;
  370. }
  371. out_free:
  372. for_each_online_cpu(i) {
  373. if (ucode_cpu_info[i].mc) {
  374. int j;
  375. void *tmp = ucode_cpu_info[i].mc;
  376. vfree(tmp);
  377. for_each_online_cpu(j) {
  378. if (ucode_cpu_info[j].mc == tmp)
  379. ucode_cpu_info[j].mc = NULL;
  380. }
  381. }
  382. }
  383. out:
  384. return error;
  385. }
  386. static ssize_t microcode_write (struct file *file, const char __user *buf, size_t len, loff_t *ppos)
  387. {
  388. ssize_t ret;
  389. if (len < DEFAULT_UCODE_TOTALSIZE) {
  390. printk(KERN_ERR "microcode: not enough data\n");
  391. return -EINVAL;
  392. }
  393. if ((len >> PAGE_SHIFT) > num_physpages) {
  394. printk(KERN_ERR "microcode: too much data (max %ld pages)\n", num_physpages);
  395. return -EINVAL;
  396. }
  397. mutex_lock(&microcode_mutex);
  398. user_buffer = (void __user *) buf;
  399. user_buffer_size = (int) len;
  400. ret = do_microcode_update();
  401. if (!ret)
  402. ret = (ssize_t)len;
  403. mutex_unlock(&microcode_mutex);
  404. return ret;
  405. }
  406. static struct file_operations microcode_fops = {
  407. .owner = THIS_MODULE,
  408. .write = microcode_write,
  409. .open = microcode_open,
  410. };
  411. static struct miscdevice microcode_dev = {
  412. .minor = MICROCODE_MINOR,
  413. .name = "microcode",
  414. .devfs_name = "cpu/microcode",
  415. .fops = &microcode_fops,
  416. };
  417. static int __init microcode_init (void)
  418. {
  419. int error;
  420. error = misc_register(&microcode_dev);
  421. if (error) {
  422. printk(KERN_ERR
  423. "microcode: can't misc_register on minor=%d\n",
  424. MICROCODE_MINOR);
  425. return error;
  426. }
  427. printk(KERN_INFO
  428. "IA-32 Microcode Update Driver: v" MICROCODE_VERSION " <tigran@veritas.com>\n");
  429. return 0;
  430. }
  431. static void __exit microcode_exit (void)
  432. {
  433. misc_deregister(&microcode_dev);
  434. }
  435. module_init(microcode_init)
  436. module_exit(microcode_exit)
  437. MODULE_ALIAS_MISCDEV(MICROCODE_MINOR);