microcode_intel.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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 speculative
  59. * 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. //#define DEBUG /* pr_debug */
  74. #include <linux/capability.h>
  75. #include <linux/kernel.h>
  76. #include <linux/init.h>
  77. #include <linux/sched.h>
  78. #include <linux/smp_lock.h>
  79. #include <linux/cpumask.h>
  80. #include <linux/module.h>
  81. #include <linux/slab.h>
  82. #include <linux/vmalloc.h>
  83. #include <linux/miscdevice.h>
  84. #include <linux/spinlock.h>
  85. #include <linux/mm.h>
  86. #include <linux/fs.h>
  87. #include <linux/mutex.h>
  88. #include <linux/cpu.h>
  89. #include <linux/firmware.h>
  90. #include <linux/platform_device.h>
  91. #include <asm/msr.h>
  92. #include <asm/uaccess.h>
  93. #include <asm/processor.h>
  94. #include <asm/microcode.h>
  95. MODULE_DESCRIPTION("Microcode Update Driver");
  96. MODULE_AUTHOR("Tigran Aivazian <tigran@aivazian.fsnet.co.uk>");
  97. MODULE_LICENSE("GPL");
  98. #define DEFAULT_UCODE_DATASIZE (2000) /* 2000 bytes */
  99. #define MC_HEADER_SIZE (sizeof(struct microcode_header)) /* 48 bytes */
  100. #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE) /* 2048 bytes */
  101. #define EXT_HEADER_SIZE (sizeof(struct extended_sigtable)) /* 20 bytes */
  102. #define EXT_SIGNATURE_SIZE (sizeof(struct extended_signature)) /* 12 bytes */
  103. #define DWSIZE (sizeof(u32))
  104. #define get_totalsize(mc) \
  105. (((struct microcode *)mc)->hdr.totalsize ? \
  106. ((struct microcode *)mc)->hdr.totalsize : DEFAULT_UCODE_TOTALSIZE)
  107. #define get_datasize(mc) \
  108. (((struct microcode *)mc)->hdr.datasize ? \
  109. ((struct microcode *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE)
  110. #define sigmatch(s1, s2, p1, p2) \
  111. (((s1) == (s2)) && (((p1) & (p2)) || (((p1) == 0) && ((p2) == 0))))
  112. #define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE)
  113. /* serialize access to the physical write to MSR 0x79 */
  114. static DEFINE_SPINLOCK(microcode_update_lock);
  115. /* no concurrent ->write()s are allowed on /dev/cpu/microcode */
  116. extern struct mutex microcode_mutex;
  117. extern struct ucode_cpu_info ucode_cpu_info[NR_CPUS];
  118. 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. struct microcode_header *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. int microcode_sanity_check(void *mc)
  159. {
  160. struct microcode_header *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. printk(KERN_ERR "microcode: error! "
  169. "Bad data size in microcode data file\n");
  170. return -EINVAL;
  171. }
  172. if (mc_header->ldrver != 1 || mc_header->hdrver != 1) {
  173. printk(KERN_ERR "microcode: error! "
  174. "Unknown microcode update format\n");
  175. return -EINVAL;
  176. }
  177. ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
  178. if (ext_table_size) {
  179. if ((ext_table_size < EXT_HEADER_SIZE)
  180. || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
  181. printk(KERN_ERR "microcode: error! "
  182. "Small exttable size in microcode data file\n");
  183. return -EINVAL;
  184. }
  185. ext_header = mc + MC_HEADER_SIZE + data_size;
  186. if (ext_table_size != exttable_size(ext_header)) {
  187. printk(KERN_ERR "microcode: error! "
  188. "Bad exttable size in microcode data file\n");
  189. return -EFAULT;
  190. }
  191. ext_sigcount = ext_header->count;
  192. }
  193. /* check extended table checksum */
  194. if (ext_table_size) {
  195. int ext_table_sum = 0;
  196. int *ext_tablep = (int *)ext_header;
  197. i = ext_table_size / DWSIZE;
  198. while (i--)
  199. ext_table_sum += ext_tablep[i];
  200. if (ext_table_sum) {
  201. printk(KERN_WARNING "microcode: aborting, "
  202. "bad extended signature table checksum\n");
  203. return -EINVAL;
  204. }
  205. }
  206. /* calculate the checksum */
  207. orig_sum = 0;
  208. i = (MC_HEADER_SIZE + data_size) / DWSIZE;
  209. while (i--)
  210. orig_sum += ((int *)mc)[i];
  211. if (orig_sum) {
  212. printk(KERN_ERR "microcode: aborting, bad checksum\n");
  213. return -EINVAL;
  214. }
  215. if (!ext_table_size)
  216. return 0;
  217. /* check extended signature checksum */
  218. for (i = 0; i < ext_sigcount; i++) {
  219. ext_sig = (void *)ext_header + EXT_HEADER_SIZE +
  220. EXT_SIGNATURE_SIZE * i;
  221. sum = orig_sum
  222. - (mc_header->sig + mc_header->pf + mc_header->cksum)
  223. + (ext_sig->sig + ext_sig->pf + ext_sig->cksum);
  224. if (sum) {
  225. printk(KERN_ERR "microcode: aborting, bad checksum\n");
  226. return -EINVAL;
  227. }
  228. }
  229. return 0;
  230. }
  231. /*
  232. * return 0 - no update found
  233. * return 1 - found update
  234. * return < 0 - error
  235. */
  236. int get_matching_microcode(void *mc, int cpu)
  237. {
  238. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  239. struct microcode_header *mc_header = mc;
  240. struct extended_sigtable *ext_header;
  241. unsigned long total_size = get_totalsize(mc_header);
  242. int ext_sigcount, i;
  243. struct extended_signature *ext_sig;
  244. void *new_mc;
  245. if (microcode_update_match(cpu, mc_header,
  246. mc_header->sig, mc_header->pf))
  247. goto find;
  248. if (total_size <= get_datasize(mc_header) + MC_HEADER_SIZE)
  249. return 0;
  250. ext_header = mc + get_datasize(mc_header) + MC_HEADER_SIZE;
  251. ext_sigcount = ext_header->count;
  252. ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
  253. for (i = 0; i < ext_sigcount; i++) {
  254. if (microcode_update_match(cpu, mc_header,
  255. ext_sig->sig, ext_sig->pf))
  256. goto find;
  257. ext_sig++;
  258. }
  259. return 0;
  260. find:
  261. pr_debug("microcode: CPU%d found a matching microcode update with"
  262. " version 0x%x (current=0x%x)\n", cpu, mc_header->rev, uci->rev);
  263. new_mc = vmalloc(total_size);
  264. if (!new_mc) {
  265. printk(KERN_ERR "microcode: error! Can not allocate memory\n");
  266. return -ENOMEM;
  267. }
  268. /* free previous update file */
  269. vfree(uci->mc);
  270. memcpy(new_mc, mc, total_size);
  271. uci->mc = new_mc;
  272. return 1;
  273. }
  274. void apply_microcode(int cpu)
  275. {
  276. unsigned long flags;
  277. unsigned int val[2];
  278. int cpu_num = raw_smp_processor_id();
  279. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  280. /* We should bind the task to the CPU */
  281. BUG_ON(cpu_num != cpu);
  282. if (uci->mc == NULL)
  283. return;
  284. /* serialize access to the physical write to MSR 0x79 */
  285. spin_lock_irqsave(&microcode_update_lock, flags);
  286. /* write microcode via MSR 0x79 */
  287. wrmsr(MSR_IA32_UCODE_WRITE,
  288. (unsigned long) uci->mc->bits,
  289. (unsigned long) uci->mc->bits >> 16 >> 16);
  290. wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  291. /* see notes above for revision 1.07. Apparent chip bug */
  292. sync_core();
  293. /* get the current revision from MSR 0x8B */
  294. rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  295. spin_unlock_irqrestore(&microcode_update_lock, flags);
  296. if (val[1] != uci->mc->hdr.rev) {
  297. printk(KERN_ERR "microcode: CPU%d update from revision "
  298. "0x%x to 0x%x failed\n", cpu_num, uci->rev, val[1]);
  299. return;
  300. }
  301. printk(KERN_INFO "microcode: CPU%d updated from revision "
  302. "0x%x to 0x%x, date = %08x \n",
  303. cpu_num, uci->rev, val[1], uci->mc->hdr.date);
  304. uci->rev = val[1];
  305. }
  306. #ifdef CONFIG_MICROCODE_OLD_INTERFACE
  307. extern void __user *user_buffer; /* user area microcode data buffer */
  308. extern unsigned int user_buffer_size; /* it's size */
  309. long get_next_ucode(void **mc, long offset)
  310. {
  311. struct microcode_header mc_header;
  312. unsigned long total_size;
  313. /* No more data */
  314. if (offset >= user_buffer_size)
  315. return 0;
  316. if (copy_from_user(&mc_header, user_buffer + offset, MC_HEADER_SIZE)) {
  317. printk(KERN_ERR "microcode: error! Can not read user data\n");
  318. return -EFAULT;
  319. }
  320. total_size = get_totalsize(&mc_header);
  321. if (offset + total_size > user_buffer_size) {
  322. printk(KERN_ERR "microcode: error! Bad total size in microcode "
  323. "data file\n");
  324. return -EINVAL;
  325. }
  326. *mc = vmalloc(total_size);
  327. if (!*mc)
  328. return -ENOMEM;
  329. if (copy_from_user(*mc, user_buffer + offset, total_size)) {
  330. printk(KERN_ERR "microcode: error! Can not read user data\n");
  331. vfree(*mc);
  332. return -EFAULT;
  333. }
  334. return offset + total_size;
  335. }
  336. #endif
  337. static long get_next_ucode_from_buffer(void **mc, const u8 *buf,
  338. unsigned long size, long offset)
  339. {
  340. struct microcode_header *mc_header;
  341. unsigned long total_size;
  342. /* No more data */
  343. if (offset >= size)
  344. return 0;
  345. mc_header = (struct microcode_header *)(buf + offset);
  346. total_size = get_totalsize(mc_header);
  347. if (offset + total_size > size) {
  348. printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
  349. return -EINVAL;
  350. }
  351. *mc = vmalloc(total_size);
  352. if (!*mc) {
  353. printk(KERN_ERR "microcode: error! Can not allocate memory\n");
  354. return -ENOMEM;
  355. }
  356. memcpy(*mc, buf + offset, total_size);
  357. return offset + total_size;
  358. }
  359. /* fake device for request_firmware */
  360. extern struct platform_device *microcode_pdev;
  361. int cpu_request_microcode(int cpu)
  362. {
  363. char name[30];
  364. struct cpuinfo_x86 *c = &cpu_data(cpu);
  365. const struct firmware *firmware;
  366. const u8 *buf;
  367. unsigned long size;
  368. long offset = 0;
  369. int error;
  370. void *mc;
  371. /* We should bind the task to the CPU */
  372. BUG_ON(cpu != raw_smp_processor_id());
  373. sprintf(name, "intel-ucode/%02x-%02x-%02x",
  374. c->x86, c->x86_model, c->x86_mask);
  375. error = request_firmware(&firmware, name, &microcode_pdev->dev);
  376. if (error) {
  377. pr_debug("microcode: data file %s load failed\n", name);
  378. return error;
  379. }
  380. buf = firmware->data;
  381. size = firmware->size;
  382. while ((offset = get_next_ucode_from_buffer(&mc, buf, size, offset))
  383. > 0) {
  384. error = microcode_sanity_check(mc);
  385. if (error)
  386. break;
  387. error = get_matching_microcode(mc, cpu);
  388. if (error < 0)
  389. break;
  390. /*
  391. * It's possible the data file has multiple matching ucode,
  392. * lets keep searching till the latest version
  393. */
  394. if (error == 1) {
  395. apply_microcode(cpu);
  396. error = 0;
  397. }
  398. vfree(mc);
  399. }
  400. if (offset > 0)
  401. vfree(mc);
  402. if (offset < 0)
  403. error = offset;
  404. release_firmware(firmware);
  405. return error;
  406. }
  407. int apply_microcode_check_cpu(int cpu)
  408. {
  409. struct cpuinfo_x86 *c = &cpu_data(cpu);
  410. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  411. cpumask_t old;
  412. cpumask_of_cpu_ptr(newmask, cpu);
  413. unsigned int val[2];
  414. int err = 0;
  415. /* Check if the microcode is available */
  416. if (!uci->mc)
  417. return 0;
  418. old = current->cpus_allowed;
  419. set_cpus_allowed_ptr(current, newmask);
  420. /* Check if the microcode we have in memory matches the CPU */
  421. if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
  422. cpu_has(c, X86_FEATURE_IA64) || uci->sig != cpuid_eax(0x00000001))
  423. err = -EINVAL;
  424. if (!err && ((c->x86_model >= 5) || (c->x86 > 6))) {
  425. /* get processor flags from MSR 0x17 */
  426. rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  427. if (uci->pf != (1 << ((val[1] >> 18) & 7)))
  428. err = -EINVAL;
  429. }
  430. if (!err) {
  431. wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  432. /* see notes above for revision 1.07. Apparent chip bug */
  433. sync_core();
  434. /* get the current revision from MSR 0x8B */
  435. rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  436. if (uci->rev != val[1])
  437. err = -EINVAL;
  438. }
  439. if (!err)
  440. apply_microcode(cpu);
  441. else
  442. printk(KERN_ERR "microcode: Could not apply microcode to CPU%d:"
  443. " sig=0x%x, pf=0x%x, rev=0x%x\n",
  444. cpu, uci->sig, uci->pf, uci->rev);
  445. set_cpus_allowed_ptr(current, &old);
  446. return err;
  447. }
  448. void microcode_fini_cpu(int cpu)
  449. {
  450. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  451. mutex_lock(&microcode_mutex);
  452. uci->valid = 0;
  453. kfree(uci->mc);
  454. uci->mc = NULL;
  455. mutex_unlock(&microcode_mutex);
  456. }