microcode.c 15 KB

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