microcode.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. static int verbose;
  91. module_param(verbose, int, 0644);
  92. #define MICROCODE_VERSION "1.14a"
  93. #define DEFAULT_UCODE_DATASIZE (2000) /* 2000 bytes */
  94. #define MC_HEADER_SIZE (sizeof (microcode_header_t)) /* 48 bytes */
  95. #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE) /* 2048 bytes */
  96. #define EXT_HEADER_SIZE (sizeof (struct extended_sigtable)) /* 20 bytes */
  97. #define EXT_SIGNATURE_SIZE (sizeof (struct extended_signature)) /* 12 bytes */
  98. #define DWSIZE (sizeof (u32))
  99. #define get_totalsize(mc) \
  100. (((microcode_t *)mc)->hdr.totalsize ? \
  101. ((microcode_t *)mc)->hdr.totalsize : DEFAULT_UCODE_TOTALSIZE)
  102. #define get_datasize(mc) \
  103. (((microcode_t *)mc)->hdr.datasize ? \
  104. ((microcode_t *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE)
  105. #define sigmatch(s1, s2, p1, p2) \
  106. (((s1) == (s2)) && (((p1) & (p2)) || (((p1) == 0) && ((p2) == 0))))
  107. #define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE)
  108. /* serialize access to the physical write to MSR 0x79 */
  109. static DEFINE_SPINLOCK(microcode_update_lock);
  110. /* no concurrent ->write()s are allowed on /dev/cpu/microcode */
  111. static DEFINE_MUTEX(microcode_mutex);
  112. static void __user *user_buffer; /* user area microcode data buffer */
  113. static unsigned int user_buffer_size; /* it's size */
  114. typedef enum mc_error_code {
  115. MC_SUCCESS = 0,
  116. MC_IGNORED = 1,
  117. MC_NOTFOUND = 2,
  118. MC_MARKED = 3,
  119. MC_ALLOCATED = 4,
  120. } mc_error_code_t;
  121. static struct ucode_cpu_info {
  122. unsigned int sig;
  123. unsigned int pf, orig_pf;
  124. unsigned int rev;
  125. unsigned int cksum;
  126. mc_error_code_t err;
  127. microcode_t *mc;
  128. } ucode_cpu_info[NR_CPUS];
  129. static int microcode_open (struct inode *unused1, struct file *unused2)
  130. {
  131. return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
  132. }
  133. static void collect_cpu_info (void *unused)
  134. {
  135. int cpu_num = smp_processor_id();
  136. struct cpuinfo_x86 *c = cpu_data + cpu_num;
  137. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  138. unsigned int val[2];
  139. uci->sig = uci->pf = uci->rev = uci->cksum = 0;
  140. uci->err = MC_NOTFOUND;
  141. uci->mc = NULL;
  142. if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
  143. cpu_has(c, X86_FEATURE_IA64)) {
  144. printk(KERN_ERR "microcode: CPU%d not a capable Intel processor\n", cpu_num);
  145. return;
  146. } else {
  147. uci->sig = cpuid_eax(0x00000001);
  148. if ((c->x86_model >= 5) || (c->x86 > 6)) {
  149. /* get processor flags from MSR 0x17 */
  150. rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  151. uci->pf = 1 << ((val[1] >> 18) & 7);
  152. }
  153. uci->orig_pf = uci->pf;
  154. }
  155. wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  156. /* see notes above for revision 1.07. Apparent chip bug */
  157. sync_core();
  158. /* get the current revision from MSR 0x8B */
  159. rdmsr(MSR_IA32_UCODE_REV, val[0], uci->rev);
  160. pr_debug("microcode: collect_cpu_info : sig=0x%x, pf=0x%x, rev=0x%x\n",
  161. uci->sig, uci->pf, uci->rev);
  162. }
  163. static inline void mark_microcode_update (int cpu_num, microcode_header_t *mc_header, int sig, int pf, int cksum)
  164. {
  165. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  166. pr_debug("Microcode Found.\n");
  167. pr_debug(" Header Revision 0x%x\n", mc_header->hdrver);
  168. pr_debug(" Loader Revision 0x%x\n", mc_header->ldrver);
  169. pr_debug(" Revision 0x%x \n", mc_header->rev);
  170. pr_debug(" Date %x/%x/%x\n",
  171. ((mc_header->date >> 24 ) & 0xff),
  172. ((mc_header->date >> 16 ) & 0xff),
  173. (mc_header->date & 0xFFFF));
  174. pr_debug(" Signature 0x%x\n", sig);
  175. pr_debug(" Type 0x%x Family 0x%x Model 0x%x Stepping 0x%x\n",
  176. ((sig >> 12) & 0x3),
  177. ((sig >> 8) & 0xf),
  178. ((sig >> 4) & 0xf),
  179. ((sig & 0xf)));
  180. pr_debug(" Processor Flags 0x%x\n", pf);
  181. pr_debug(" Checksum 0x%x\n", cksum);
  182. if (mc_header->rev < uci->rev) {
  183. if (uci->err == MC_NOTFOUND) {
  184. uci->err = MC_IGNORED;
  185. uci->cksum = mc_header->rev;
  186. } else if (uci->err == MC_IGNORED && uci->cksum < mc_header->rev)
  187. uci->cksum = mc_header->rev;
  188. } else if (mc_header->rev == uci->rev) {
  189. if (uci->err < MC_MARKED) {
  190. /* notify the caller of success on this cpu */
  191. uci->err = MC_SUCCESS;
  192. }
  193. } else if (uci->err != MC_ALLOCATED || mc_header->rev > uci->mc->hdr.rev) {
  194. pr_debug("microcode: CPU%d found a matching microcode update with "
  195. " revision 0x%x (current=0x%x)\n", cpu_num, mc_header->rev, uci->rev);
  196. uci->cksum = cksum;
  197. uci->pf = pf; /* keep the original mc pf for cksum calculation */
  198. uci->err = MC_MARKED; /* found the match */
  199. for_each_online_cpu(cpu_num) {
  200. if (ucode_cpu_info + cpu_num != uci
  201. && ucode_cpu_info[cpu_num].mc == uci->mc) {
  202. uci->mc = NULL;
  203. break;
  204. }
  205. }
  206. if (uci->mc != NULL) {
  207. vfree(uci->mc);
  208. uci->mc = NULL;
  209. }
  210. }
  211. return;
  212. }
  213. static int find_matching_ucodes (void)
  214. {
  215. int cursor = 0;
  216. int error = 0;
  217. while (cursor + MC_HEADER_SIZE < user_buffer_size) {
  218. microcode_header_t mc_header;
  219. void *newmc = NULL;
  220. int i, sum, cpu_num, allocated_flag, total_size, data_size, ext_table_size;
  221. if (copy_from_user(&mc_header, user_buffer + cursor, MC_HEADER_SIZE)) {
  222. printk(KERN_ERR "microcode: error! Can not read user data\n");
  223. error = -EFAULT;
  224. goto out;
  225. }
  226. total_size = get_totalsize(&mc_header);
  227. if ((cursor + total_size > user_buffer_size) || (total_size < DEFAULT_UCODE_TOTALSIZE)) {
  228. printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
  229. error = -EINVAL;
  230. goto out;
  231. }
  232. data_size = get_datasize(&mc_header);
  233. if ((data_size + MC_HEADER_SIZE > total_size) || (data_size < DEFAULT_UCODE_DATASIZE)) {
  234. printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
  235. error = -EINVAL;
  236. goto out;
  237. }
  238. if (mc_header.ldrver != 1 || mc_header.hdrver != 1) {
  239. printk(KERN_ERR "microcode: error! Unknown microcode update format\n");
  240. error = -EINVAL;
  241. goto out;
  242. }
  243. for_each_online_cpu(cpu_num) {
  244. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  245. if (sigmatch(mc_header.sig, uci->sig, mc_header.pf, uci->orig_pf))
  246. mark_microcode_update(cpu_num, &mc_header, mc_header.sig, mc_header.pf, mc_header.cksum);
  247. }
  248. ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
  249. if (ext_table_size) {
  250. struct extended_sigtable ext_header;
  251. struct extended_signature ext_sig;
  252. int ext_sigcount;
  253. if ((ext_table_size < EXT_HEADER_SIZE)
  254. || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
  255. printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
  256. error = -EINVAL;
  257. goto out;
  258. }
  259. if (copy_from_user(&ext_header, user_buffer + cursor
  260. + MC_HEADER_SIZE + data_size, EXT_HEADER_SIZE)) {
  261. printk(KERN_ERR "microcode: error! Can not read user data\n");
  262. error = -EFAULT;
  263. goto out;
  264. }
  265. if (ext_table_size != exttable_size(&ext_header)) {
  266. printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
  267. error = -EFAULT;
  268. goto out;
  269. }
  270. ext_sigcount = ext_header.count;
  271. for (i = 0; i < ext_sigcount; i++) {
  272. if (copy_from_user(&ext_sig, user_buffer + cursor + MC_HEADER_SIZE + data_size + EXT_HEADER_SIZE
  273. + EXT_SIGNATURE_SIZE * i, EXT_SIGNATURE_SIZE)) {
  274. printk(KERN_ERR "microcode: error! Can not read user data\n");
  275. error = -EFAULT;
  276. goto out;
  277. }
  278. for_each_online_cpu(cpu_num) {
  279. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  280. if (sigmatch(ext_sig.sig, uci->sig, ext_sig.pf, uci->orig_pf)) {
  281. mark_microcode_update(cpu_num, &mc_header, ext_sig.sig, ext_sig.pf, ext_sig.cksum);
  282. }
  283. }
  284. }
  285. }
  286. /* now check if any cpu has matched */
  287. allocated_flag = 0;
  288. sum = 0;
  289. for_each_online_cpu(cpu_num) {
  290. if (ucode_cpu_info[cpu_num].err == MC_MARKED) {
  291. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  292. if (!allocated_flag) {
  293. allocated_flag = 1;
  294. newmc = vmalloc(total_size);
  295. if (!newmc) {
  296. printk(KERN_ERR "microcode: error! Can not allocate memory\n");
  297. error = -ENOMEM;
  298. goto out;
  299. }
  300. if (copy_from_user(newmc + MC_HEADER_SIZE,
  301. user_buffer + cursor + MC_HEADER_SIZE,
  302. total_size - MC_HEADER_SIZE)) {
  303. printk(KERN_ERR "microcode: error! Can not read user data\n");
  304. vfree(newmc);
  305. error = -EFAULT;
  306. goto out;
  307. }
  308. memcpy(newmc, &mc_header, MC_HEADER_SIZE);
  309. /* check extended table checksum */
  310. if (ext_table_size) {
  311. int ext_table_sum = 0;
  312. int * ext_tablep = (((void *) newmc) + MC_HEADER_SIZE + data_size);
  313. i = ext_table_size / DWSIZE;
  314. while (i--) ext_table_sum += ext_tablep[i];
  315. if (ext_table_sum) {
  316. printk(KERN_WARNING "microcode: aborting, bad extended signature table checksum\n");
  317. vfree(newmc);
  318. error = -EINVAL;
  319. goto out;
  320. }
  321. }
  322. /* calculate the checksum */
  323. i = (MC_HEADER_SIZE + data_size) / DWSIZE;
  324. while (i--) sum += ((int *)newmc)[i];
  325. sum -= (mc_header.sig + mc_header.pf + mc_header.cksum);
  326. }
  327. ucode_cpu_info[cpu_num].mc = newmc;
  328. ucode_cpu_info[cpu_num].err = MC_ALLOCATED; /* mc updated */
  329. if (sum + uci->sig + uci->pf + uci->cksum != 0) {
  330. printk(KERN_ERR "microcode: CPU%d aborting, bad checksum\n", cpu_num);
  331. error = -EINVAL;
  332. goto out;
  333. }
  334. }
  335. }
  336. cursor += total_size; /* goto the next update patch */
  337. } /* end of while */
  338. out:
  339. return error;
  340. }
  341. static void do_update_one (void * unused)
  342. {
  343. unsigned long flags;
  344. unsigned int val[2];
  345. int cpu_num = smp_processor_id();
  346. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  347. if (uci->mc == NULL) {
  348. if (verbose) {
  349. if (uci->err == MC_SUCCESS)
  350. printk(KERN_INFO "microcode: CPU%d already at revision 0x%x\n",
  351. cpu_num, uci->rev);
  352. else
  353. printk(KERN_INFO "microcode: No new microcode data for CPU%d\n", cpu_num);
  354. }
  355. return;
  356. }
  357. /* serialize access to the physical write to MSR 0x79 */
  358. spin_lock_irqsave(&microcode_update_lock, flags);
  359. /* write microcode via MSR 0x79 */
  360. wrmsr(MSR_IA32_UCODE_WRITE,
  361. (unsigned long) uci->mc->bits,
  362. (unsigned long) uci->mc->bits >> 16 >> 16);
  363. wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  364. /* see notes above for revision 1.07. Apparent chip bug */
  365. sync_core();
  366. /* get the current revision from MSR 0x8B */
  367. rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  368. /* notify the caller of success on this cpu */
  369. uci->err = MC_SUCCESS;
  370. spin_unlock_irqrestore(&microcode_update_lock, flags);
  371. printk(KERN_INFO "microcode: CPU%d updated from revision "
  372. "0x%x to 0x%x, date = %08x \n",
  373. cpu_num, uci->rev, val[1], uci->mc->hdr.date);
  374. return;
  375. }
  376. static int do_microcode_update (void)
  377. {
  378. int i, error;
  379. if (on_each_cpu(collect_cpu_info, NULL, 1, 1) != 0) {
  380. printk(KERN_ERR "microcode: Error! Could not run on all processors\n");
  381. error = -EIO;
  382. goto out;
  383. }
  384. if ((error = find_matching_ucodes())) {
  385. printk(KERN_ERR "microcode: Error in the microcode data\n");
  386. goto out_free;
  387. }
  388. if (on_each_cpu(do_update_one, NULL, 1, 1) != 0) {
  389. printk(KERN_ERR "microcode: Error! Could not run on all processors\n");
  390. error = -EIO;
  391. }
  392. out_free:
  393. for_each_online_cpu(i) {
  394. if (ucode_cpu_info[i].mc) {
  395. int j;
  396. void *tmp = ucode_cpu_info[i].mc;
  397. vfree(tmp);
  398. for_each_online_cpu(j) {
  399. if (ucode_cpu_info[j].mc == tmp)
  400. ucode_cpu_info[j].mc = NULL;
  401. }
  402. }
  403. if (ucode_cpu_info[i].err == MC_IGNORED && verbose)
  404. printk(KERN_WARNING "microcode: CPU%d not 'upgrading' to earlier revision"
  405. " 0x%x (current=0x%x)\n", i, ucode_cpu_info[i].cksum, ucode_cpu_info[i].rev);
  406. }
  407. out:
  408. return error;
  409. }
  410. static ssize_t microcode_write (struct file *file, const char __user *buf, size_t len, loff_t *ppos)
  411. {
  412. ssize_t ret;
  413. if (len < DEFAULT_UCODE_TOTALSIZE) {
  414. printk(KERN_ERR "microcode: not enough data\n");
  415. return -EINVAL;
  416. }
  417. if ((len >> PAGE_SHIFT) > num_physpages) {
  418. printk(KERN_ERR "microcode: too much data (max %ld pages)\n", num_physpages);
  419. return -EINVAL;
  420. }
  421. mutex_lock(&microcode_mutex);
  422. user_buffer = (void __user *) buf;
  423. user_buffer_size = (int) len;
  424. ret = do_microcode_update();
  425. if (!ret)
  426. ret = (ssize_t)len;
  427. mutex_unlock(&microcode_mutex);
  428. return ret;
  429. }
  430. static struct file_operations microcode_fops = {
  431. .owner = THIS_MODULE,
  432. .write = microcode_write,
  433. .open = microcode_open,
  434. };
  435. static struct miscdevice microcode_dev = {
  436. .minor = MICROCODE_MINOR,
  437. .name = "microcode",
  438. .devfs_name = "cpu/microcode",
  439. .fops = &microcode_fops,
  440. };
  441. static int __init microcode_init (void)
  442. {
  443. int error;
  444. error = misc_register(&microcode_dev);
  445. if (error) {
  446. printk(KERN_ERR
  447. "microcode: can't misc_register on minor=%d\n",
  448. MICROCODE_MINOR);
  449. return error;
  450. }
  451. printk(KERN_INFO
  452. "IA-32 Microcode Update Driver: v" MICROCODE_VERSION " <tigran@veritas.com>\n");
  453. return 0;
  454. }
  455. static void __exit microcode_exit (void)
  456. {
  457. misc_deregister(&microcode_dev);
  458. }
  459. module_init(microcode_init)
  460. module_exit(microcode_exit)
  461. MODULE_ALIAS_MISCDEV(MICROCODE_MINOR);