microcode.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  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.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/fs.h>
  85. #include <linux/mutex.h>
  86. #include <linux/cpu.h>
  87. #include <linux/firmware.h>
  88. #include <linux/platform_device.h>
  89. #include <asm/msr.h>
  90. #include <asm/uaccess.h>
  91. #include <asm/processor.h>
  92. MODULE_DESCRIPTION("Intel CPU (IA-32) Microcode Update Driver");
  93. MODULE_AUTHOR("Tigran Aivazian <tigran@aivazian.fsnet.co.uk>");
  94. MODULE_LICENSE("GPL");
  95. #define MICROCODE_VERSION "1.14a"
  96. #define DEFAULT_UCODE_DATASIZE (2000) /* 2000 bytes */
  97. #define MC_HEADER_SIZE (sizeof (microcode_header_t)) /* 48 bytes */
  98. #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE) /* 2048 bytes */
  99. #define EXT_HEADER_SIZE (sizeof (struct extended_sigtable)) /* 20 bytes */
  100. #define EXT_SIGNATURE_SIZE (sizeof (struct extended_signature)) /* 12 bytes */
  101. #define DWSIZE (sizeof (u32))
  102. #define get_totalsize(mc) \
  103. (((microcode_t *)mc)->hdr.totalsize ? \
  104. ((microcode_t *)mc)->hdr.totalsize : DEFAULT_UCODE_TOTALSIZE)
  105. #define get_datasize(mc) \
  106. (((microcode_t *)mc)->hdr.datasize ? \
  107. ((microcode_t *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE)
  108. #define sigmatch(s1, s2, p1, p2) \
  109. (((s1) == (s2)) && (((p1) & (p2)) || (((p1) == 0) && ((p2) == 0))))
  110. #define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE)
  111. /* serialize access to the physical write to MSR 0x79 */
  112. static DEFINE_SPINLOCK(microcode_update_lock);
  113. /* no concurrent ->write()s are allowed on /dev/cpu/microcode */
  114. static DEFINE_MUTEX(microcode_mutex);
  115. static struct ucode_cpu_info {
  116. int valid;
  117. unsigned int sig;
  118. unsigned int pf;
  119. unsigned int rev;
  120. microcode_t *mc;
  121. } ucode_cpu_info[NR_CPUS];
  122. static void collect_cpu_info(int cpu_num)
  123. {
  124. struct cpuinfo_x86 *c = &cpu_data(cpu_num);
  125. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  126. unsigned int val[2];
  127. /* We should bind the task to the CPU */
  128. BUG_ON(raw_smp_processor_id() != cpu_num);
  129. uci->pf = uci->rev = 0;
  130. uci->mc = NULL;
  131. uci->valid = 1;
  132. if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
  133. cpu_has(c, X86_FEATURE_IA64)) {
  134. printk(KERN_ERR "microcode: CPU%d not a capable Intel "
  135. "processor\n", cpu_num);
  136. uci->valid = 0;
  137. return;
  138. }
  139. uci->sig = cpuid_eax(0x00000001);
  140. if ((c->x86_model >= 5) || (c->x86 > 6)) {
  141. /* get processor flags from MSR 0x17 */
  142. rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  143. uci->pf = 1 << ((val[1] >> 18) & 7);
  144. }
  145. wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  146. /* see notes above for revision 1.07. Apparent chip bug */
  147. sync_core();
  148. /* get the current revision from MSR 0x8B */
  149. rdmsr(MSR_IA32_UCODE_REV, val[0], uci->rev);
  150. pr_debug("microcode: collect_cpu_info : sig=0x%x, pf=0x%x, rev=0x%x\n",
  151. uci->sig, uci->pf, uci->rev);
  152. }
  153. static inline int microcode_update_match(int cpu_num,
  154. microcode_header_t *mc_header, int sig, int pf)
  155. {
  156. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  157. if (!sigmatch(sig, uci->sig, pf, uci->pf)
  158. || mc_header->rev <= uci->rev)
  159. return 0;
  160. return 1;
  161. }
  162. static int microcode_sanity_check(void *mc)
  163. {
  164. microcode_header_t *mc_header = mc;
  165. struct extended_sigtable *ext_header = NULL;
  166. struct extended_signature *ext_sig;
  167. unsigned long total_size, data_size, ext_table_size;
  168. int sum, orig_sum, ext_sigcount = 0, i;
  169. total_size = get_totalsize(mc_header);
  170. data_size = get_datasize(mc_header);
  171. if (data_size + MC_HEADER_SIZE > total_size) {
  172. printk(KERN_ERR "microcode: error! "
  173. "Bad data size in microcode data file\n");
  174. return -EINVAL;
  175. }
  176. if (mc_header->ldrver != 1 || mc_header->hdrver != 1) {
  177. printk(KERN_ERR "microcode: error! "
  178. "Unknown microcode update format\n");
  179. return -EINVAL;
  180. }
  181. ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
  182. if (ext_table_size) {
  183. if ((ext_table_size < EXT_HEADER_SIZE)
  184. || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
  185. printk(KERN_ERR "microcode: error! "
  186. "Small exttable size in microcode data file\n");
  187. return -EINVAL;
  188. }
  189. ext_header = mc + MC_HEADER_SIZE + data_size;
  190. if (ext_table_size != exttable_size(ext_header)) {
  191. printk(KERN_ERR "microcode: error! "
  192. "Bad exttable size in microcode data file\n");
  193. return -EFAULT;
  194. }
  195. ext_sigcount = ext_header->count;
  196. }
  197. /* check extended table checksum */
  198. if (ext_table_size) {
  199. int ext_table_sum = 0;
  200. int *ext_tablep = (int *)ext_header;
  201. i = ext_table_size / DWSIZE;
  202. while (i--)
  203. ext_table_sum += ext_tablep[i];
  204. if (ext_table_sum) {
  205. printk(KERN_WARNING "microcode: aborting, "
  206. "bad extended signature table checksum\n");
  207. return -EINVAL;
  208. }
  209. }
  210. /* calculate the checksum */
  211. orig_sum = 0;
  212. i = (MC_HEADER_SIZE + data_size) / DWSIZE;
  213. while (i--)
  214. orig_sum += ((int *)mc)[i];
  215. if (orig_sum) {
  216. printk(KERN_ERR "microcode: aborting, bad checksum\n");
  217. return -EINVAL;
  218. }
  219. if (!ext_table_size)
  220. return 0;
  221. /* check extended signature checksum */
  222. for (i = 0; i < ext_sigcount; i++) {
  223. ext_sig = (struct extended_signature *)((void *)ext_header
  224. + EXT_HEADER_SIZE + EXT_SIGNATURE_SIZE * i);
  225. sum = orig_sum
  226. - (mc_header->sig + mc_header->pf + mc_header->cksum)
  227. + (ext_sig->sig + ext_sig->pf + ext_sig->cksum);
  228. if (sum) {
  229. printk(KERN_ERR "microcode: aborting, bad checksum\n");
  230. return -EINVAL;
  231. }
  232. }
  233. return 0;
  234. }
  235. /*
  236. * return 0 - no update found
  237. * return 1 - found update
  238. * return < 0 - error
  239. */
  240. static int get_maching_microcode(void *mc, int cpu)
  241. {
  242. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  243. microcode_header_t *mc_header = mc;
  244. struct extended_sigtable *ext_header;
  245. unsigned long total_size = get_totalsize(mc_header);
  246. int ext_sigcount, i;
  247. struct extended_signature *ext_sig;
  248. void *new_mc;
  249. if (microcode_update_match(cpu, mc_header,
  250. mc_header->sig, mc_header->pf))
  251. goto find;
  252. if (total_size <= get_datasize(mc_header) + MC_HEADER_SIZE)
  253. return 0;
  254. ext_header = (struct extended_sigtable *)(mc +
  255. get_datasize(mc_header) + MC_HEADER_SIZE);
  256. ext_sigcount = ext_header->count;
  257. ext_sig = (struct extended_signature *)((void *)ext_header
  258. + EXT_HEADER_SIZE);
  259. for (i = 0; i < ext_sigcount; i++) {
  260. if (microcode_update_match(cpu, mc_header,
  261. ext_sig->sig, ext_sig->pf))
  262. goto find;
  263. ext_sig++;
  264. }
  265. return 0;
  266. find:
  267. pr_debug("microcode: CPU %d found a matching microcode update with"
  268. " version 0x%x (current=0x%x)\n", cpu, mc_header->rev,uci->rev);
  269. new_mc = vmalloc(total_size);
  270. if (!new_mc) {
  271. printk(KERN_ERR "microcode: error! Can not allocate memory\n");
  272. return -ENOMEM;
  273. }
  274. /* free previous update file */
  275. vfree(uci->mc);
  276. memcpy(new_mc, mc, total_size);
  277. uci->mc = new_mc;
  278. return 1;
  279. }
  280. static void apply_microcode(int cpu)
  281. {
  282. unsigned long flags;
  283. unsigned int val[2];
  284. int cpu_num = raw_smp_processor_id();
  285. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  286. /* We should bind the task to the CPU */
  287. BUG_ON(cpu_num != cpu);
  288. if (uci->mc == NULL)
  289. return;
  290. /* serialize access to the physical write to MSR 0x79 */
  291. spin_lock_irqsave(&microcode_update_lock, flags);
  292. /* write microcode via MSR 0x79 */
  293. wrmsr(MSR_IA32_UCODE_WRITE,
  294. (unsigned long) uci->mc->bits,
  295. (unsigned long) uci->mc->bits >> 16 >> 16);
  296. wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  297. /* see notes above for revision 1.07. Apparent chip bug */
  298. sync_core();
  299. /* get the current revision from MSR 0x8B */
  300. rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  301. spin_unlock_irqrestore(&microcode_update_lock, flags);
  302. if (val[1] != uci->mc->hdr.rev) {
  303. printk(KERN_ERR "microcode: CPU%d updated from revision "
  304. "0x%x to 0x%x failed\n", cpu_num, uci->rev, val[1]);
  305. return;
  306. }
  307. pr_debug("microcode: CPU%d updated from revision "
  308. "0x%x to 0x%x, date = %08x \n",
  309. cpu_num, uci->rev, val[1], uci->mc->hdr.date);
  310. uci->rev = val[1];
  311. }
  312. #ifdef CONFIG_MICROCODE_OLD_INTERFACE
  313. static void __user *user_buffer; /* user area microcode data buffer */
  314. static unsigned int user_buffer_size; /* it's size */
  315. static long get_next_ucode(void **mc, long offset)
  316. {
  317. microcode_header_t mc_header;
  318. unsigned long total_size;
  319. /* No more data */
  320. if (offset >= user_buffer_size)
  321. return 0;
  322. if (copy_from_user(&mc_header, user_buffer + offset, MC_HEADER_SIZE)) {
  323. printk(KERN_ERR "microcode: error! Can not read user data\n");
  324. return -EFAULT;
  325. }
  326. total_size = get_totalsize(&mc_header);
  327. if (offset + total_size > user_buffer_size) {
  328. printk(KERN_ERR "microcode: error! Bad total size in microcode "
  329. "data file\n");
  330. return -EINVAL;
  331. }
  332. *mc = vmalloc(total_size);
  333. if (!*mc)
  334. return -ENOMEM;
  335. if (copy_from_user(*mc, user_buffer + offset, total_size)) {
  336. printk(KERN_ERR "microcode: error! Can not read user data\n");
  337. vfree(*mc);
  338. return -EFAULT;
  339. }
  340. return offset + total_size;
  341. }
  342. static int do_microcode_update (void)
  343. {
  344. long cursor = 0;
  345. int error = 0;
  346. void *new_mc = NULL;
  347. int cpu;
  348. cpumask_t old;
  349. old = current->cpus_allowed;
  350. while ((cursor = get_next_ucode(&new_mc, cursor)) > 0) {
  351. error = microcode_sanity_check(new_mc);
  352. if (error)
  353. goto out;
  354. /*
  355. * It's possible the data file has multiple matching ucode,
  356. * lets keep searching till the latest version
  357. */
  358. for_each_online_cpu(cpu) {
  359. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  360. if (!uci->valid)
  361. continue;
  362. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  363. error = get_maching_microcode(new_mc, cpu);
  364. if (error < 0)
  365. goto out;
  366. if (error == 1)
  367. apply_microcode(cpu);
  368. }
  369. vfree(new_mc);
  370. }
  371. out:
  372. if (cursor > 0)
  373. vfree(new_mc);
  374. if (cursor < 0)
  375. error = cursor;
  376. set_cpus_allowed(current, old);
  377. return error;
  378. }
  379. static int microcode_open (struct inode *unused1, struct file *unused2)
  380. {
  381. return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
  382. }
  383. static ssize_t microcode_write (struct file *file, const char __user *buf, size_t len, loff_t *ppos)
  384. {
  385. ssize_t ret;
  386. if ((len >> PAGE_SHIFT) > num_physpages) {
  387. printk(KERN_ERR "microcode: too much data (max %ld pages)\n", num_physpages);
  388. return -EINVAL;
  389. }
  390. get_online_cpus();
  391. mutex_lock(&microcode_mutex);
  392. user_buffer = (void __user *) buf;
  393. user_buffer_size = (int) len;
  394. ret = do_microcode_update();
  395. if (!ret)
  396. ret = (ssize_t)len;
  397. mutex_unlock(&microcode_mutex);
  398. put_online_cpus();
  399. return ret;
  400. }
  401. static const struct file_operations microcode_fops = {
  402. .owner = THIS_MODULE,
  403. .write = microcode_write,
  404. .open = microcode_open,
  405. };
  406. static struct miscdevice microcode_dev = {
  407. .minor = MICROCODE_MINOR,
  408. .name = "microcode",
  409. .fops = &microcode_fops,
  410. };
  411. static int __init microcode_dev_init (void)
  412. {
  413. int error;
  414. error = misc_register(&microcode_dev);
  415. if (error) {
  416. printk(KERN_ERR
  417. "microcode: can't misc_register on minor=%d\n",
  418. MICROCODE_MINOR);
  419. return error;
  420. }
  421. return 0;
  422. }
  423. static void microcode_dev_exit (void)
  424. {
  425. misc_deregister(&microcode_dev);
  426. }
  427. MODULE_ALIAS_MISCDEV(MICROCODE_MINOR);
  428. #else
  429. #define microcode_dev_init() 0
  430. #define microcode_dev_exit() do { } while(0)
  431. #endif
  432. static long get_next_ucode_from_buffer(void **mc, void *buf,
  433. unsigned long size, long offset)
  434. {
  435. microcode_header_t *mc_header;
  436. unsigned long total_size;
  437. /* No more data */
  438. if (offset >= size)
  439. return 0;
  440. mc_header = (microcode_header_t *)(buf + offset);
  441. total_size = get_totalsize(mc_header);
  442. if (offset + total_size > size) {
  443. printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
  444. return -EINVAL;
  445. }
  446. *mc = vmalloc(total_size);
  447. if (!*mc) {
  448. printk(KERN_ERR "microcode: error! Can not allocate memory\n");
  449. return -ENOMEM;
  450. }
  451. memcpy(*mc, buf + offset, total_size);
  452. return offset + total_size;
  453. }
  454. /* fake device for request_firmware */
  455. static struct platform_device *microcode_pdev;
  456. static int cpu_request_microcode(int cpu)
  457. {
  458. char name[30];
  459. struct cpuinfo_x86 *c = &cpu_data(cpu);
  460. const struct firmware *firmware;
  461. void *buf;
  462. unsigned long size;
  463. long offset = 0;
  464. int error;
  465. void *mc;
  466. /* We should bind the task to the CPU */
  467. BUG_ON(cpu != raw_smp_processor_id());
  468. sprintf(name,"intel-ucode/%02x-%02x-%02x",
  469. c->x86, c->x86_model, c->x86_mask);
  470. error = request_firmware(&firmware, name, &microcode_pdev->dev);
  471. if (error) {
  472. pr_debug("ucode data file %s load failed\n", name);
  473. return error;
  474. }
  475. buf = (void *)firmware->data;
  476. size = firmware->size;
  477. while ((offset = get_next_ucode_from_buffer(&mc, buf, size, offset))
  478. > 0) {
  479. error = microcode_sanity_check(mc);
  480. if (error)
  481. break;
  482. error = get_maching_microcode(mc, cpu);
  483. if (error < 0)
  484. break;
  485. /*
  486. * It's possible the data file has multiple matching ucode,
  487. * lets keep searching till the latest version
  488. */
  489. if (error == 1) {
  490. apply_microcode(cpu);
  491. error = 0;
  492. }
  493. vfree(mc);
  494. }
  495. if (offset > 0)
  496. vfree(mc);
  497. if (offset < 0)
  498. error = offset;
  499. release_firmware(firmware);
  500. return error;
  501. }
  502. static int apply_microcode_check_cpu(int cpu)
  503. {
  504. struct cpuinfo_x86 *c = &cpu_data(cpu);
  505. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  506. cpumask_t old;
  507. unsigned int val[2];
  508. int err = 0;
  509. /* Check if the microcode is available */
  510. if (!uci->mc)
  511. return 0;
  512. old = current->cpus_allowed;
  513. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  514. /* Check if the microcode we have in memory matches the CPU */
  515. if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
  516. cpu_has(c, X86_FEATURE_IA64) || uci->sig != cpuid_eax(0x00000001))
  517. err = -EINVAL;
  518. if (!err && ((c->x86_model >= 5) || (c->x86 > 6))) {
  519. /* get processor flags from MSR 0x17 */
  520. rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  521. if (uci->pf != (1 << ((val[1] >> 18) & 7)))
  522. err = -EINVAL;
  523. }
  524. if (!err) {
  525. wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  526. /* see notes above for revision 1.07. Apparent chip bug */
  527. sync_core();
  528. /* get the current revision from MSR 0x8B */
  529. rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  530. if (uci->rev != val[1])
  531. err = -EINVAL;
  532. }
  533. if (!err)
  534. apply_microcode(cpu);
  535. else
  536. printk(KERN_ERR "microcode: Could not apply microcode to CPU%d:"
  537. " sig=0x%x, pf=0x%x, rev=0x%x\n",
  538. cpu, uci->sig, uci->pf, uci->rev);
  539. set_cpus_allowed(current, old);
  540. return err;
  541. }
  542. static void microcode_init_cpu(int cpu, int resume)
  543. {
  544. cpumask_t old;
  545. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  546. old = current->cpus_allowed;
  547. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  548. mutex_lock(&microcode_mutex);
  549. collect_cpu_info(cpu);
  550. if (uci->valid && system_state == SYSTEM_RUNNING && !resume)
  551. cpu_request_microcode(cpu);
  552. mutex_unlock(&microcode_mutex);
  553. set_cpus_allowed(current, old);
  554. }
  555. static void microcode_fini_cpu(int cpu)
  556. {
  557. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  558. mutex_lock(&microcode_mutex);
  559. uci->valid = 0;
  560. vfree(uci->mc);
  561. uci->mc = NULL;
  562. mutex_unlock(&microcode_mutex);
  563. }
  564. static ssize_t reload_store(struct sys_device *dev, const char *buf, size_t sz)
  565. {
  566. struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
  567. char *end;
  568. unsigned long val = simple_strtoul(buf, &end, 0);
  569. int err = 0;
  570. int cpu = dev->id;
  571. if (end == buf)
  572. return -EINVAL;
  573. if (val == 1) {
  574. cpumask_t old;
  575. old = current->cpus_allowed;
  576. get_online_cpus();
  577. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  578. mutex_lock(&microcode_mutex);
  579. if (uci->valid)
  580. err = cpu_request_microcode(cpu);
  581. mutex_unlock(&microcode_mutex);
  582. put_online_cpus();
  583. set_cpus_allowed(current, old);
  584. }
  585. if (err)
  586. return err;
  587. return sz;
  588. }
  589. static ssize_t version_show(struct sys_device *dev, char *buf)
  590. {
  591. struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
  592. return sprintf(buf, "0x%x\n", uci->rev);
  593. }
  594. static ssize_t pf_show(struct sys_device *dev, char *buf)
  595. {
  596. struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
  597. return sprintf(buf, "0x%x\n", uci->pf);
  598. }
  599. static SYSDEV_ATTR(reload, 0200, NULL, reload_store);
  600. static SYSDEV_ATTR(version, 0400, version_show, NULL);
  601. static SYSDEV_ATTR(processor_flags, 0400, pf_show, NULL);
  602. static struct attribute *mc_default_attrs[] = {
  603. &attr_reload.attr,
  604. &attr_version.attr,
  605. &attr_processor_flags.attr,
  606. NULL
  607. };
  608. static struct attribute_group mc_attr_group = {
  609. .attrs = mc_default_attrs,
  610. .name = "microcode",
  611. };
  612. static int __mc_sysdev_add(struct sys_device *sys_dev, int resume)
  613. {
  614. int err, cpu = sys_dev->id;
  615. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  616. if (!cpu_online(cpu))
  617. return 0;
  618. pr_debug("Microcode:CPU %d added\n", cpu);
  619. memset(uci, 0, sizeof(*uci));
  620. err = sysfs_create_group(&sys_dev->kobj, &mc_attr_group);
  621. if (err)
  622. return err;
  623. microcode_init_cpu(cpu, resume);
  624. return 0;
  625. }
  626. static int mc_sysdev_add(struct sys_device *sys_dev)
  627. {
  628. return __mc_sysdev_add(sys_dev, 0);
  629. }
  630. static int mc_sysdev_remove(struct sys_device *sys_dev)
  631. {
  632. int cpu = sys_dev->id;
  633. if (!cpu_online(cpu))
  634. return 0;
  635. pr_debug("Microcode:CPU %d removed\n", cpu);
  636. microcode_fini_cpu(cpu);
  637. sysfs_remove_group(&sys_dev->kobj, &mc_attr_group);
  638. return 0;
  639. }
  640. static int mc_sysdev_resume(struct sys_device *dev)
  641. {
  642. int cpu = dev->id;
  643. if (!cpu_online(cpu))
  644. return 0;
  645. pr_debug("Microcode:CPU %d resumed\n", cpu);
  646. /* only CPU 0 will apply ucode here */
  647. apply_microcode(0);
  648. return 0;
  649. }
  650. static struct sysdev_driver mc_sysdev_driver = {
  651. .add = mc_sysdev_add,
  652. .remove = mc_sysdev_remove,
  653. .resume = mc_sysdev_resume,
  654. };
  655. static __cpuinit int
  656. mc_cpu_callback(struct notifier_block *nb, unsigned long action, void *hcpu)
  657. {
  658. unsigned int cpu = (unsigned long)hcpu;
  659. struct sys_device *sys_dev;
  660. sys_dev = get_cpu_sysdev(cpu);
  661. switch (action) {
  662. case CPU_UP_CANCELED_FROZEN:
  663. /* The CPU refused to come up during a system resume */
  664. microcode_fini_cpu(cpu);
  665. break;
  666. case CPU_ONLINE:
  667. case CPU_DOWN_FAILED:
  668. mc_sysdev_add(sys_dev);
  669. break;
  670. case CPU_ONLINE_FROZEN:
  671. /* System-wide resume is in progress, try to apply microcode */
  672. if (apply_microcode_check_cpu(cpu)) {
  673. /* The application of microcode failed */
  674. microcode_fini_cpu(cpu);
  675. __mc_sysdev_add(sys_dev, 1);
  676. break;
  677. }
  678. case CPU_DOWN_FAILED_FROZEN:
  679. if (sysfs_create_group(&sys_dev->kobj, &mc_attr_group))
  680. printk(KERN_ERR "Microcode: Failed to create the sysfs "
  681. "group for CPU%d\n", cpu);
  682. break;
  683. case CPU_DOWN_PREPARE:
  684. mc_sysdev_remove(sys_dev);
  685. break;
  686. case CPU_DOWN_PREPARE_FROZEN:
  687. /* Suspend is in progress, only remove the interface */
  688. sysfs_remove_group(&sys_dev->kobj, &mc_attr_group);
  689. break;
  690. }
  691. return NOTIFY_OK;
  692. }
  693. static struct notifier_block __cpuinitdata mc_cpu_notifier = {
  694. .notifier_call = mc_cpu_callback,
  695. };
  696. static int __init microcode_init (void)
  697. {
  698. int error;
  699. error = microcode_dev_init();
  700. if (error)
  701. return error;
  702. microcode_pdev = platform_device_register_simple("microcode", -1,
  703. NULL, 0);
  704. if (IS_ERR(microcode_pdev)) {
  705. microcode_dev_exit();
  706. return PTR_ERR(microcode_pdev);
  707. }
  708. get_online_cpus();
  709. error = sysdev_driver_register(&cpu_sysdev_class, &mc_sysdev_driver);
  710. put_online_cpus();
  711. if (error) {
  712. microcode_dev_exit();
  713. platform_device_unregister(microcode_pdev);
  714. return error;
  715. }
  716. register_hotcpu_notifier(&mc_cpu_notifier);
  717. printk(KERN_INFO
  718. "IA-32 Microcode Update Driver: v" MICROCODE_VERSION " <tigran@aivazian.fsnet.co.uk>\n");
  719. return 0;
  720. }
  721. static void __exit microcode_exit (void)
  722. {
  723. microcode_dev_exit();
  724. unregister_hotcpu_notifier(&mc_cpu_notifier);
  725. get_online_cpus();
  726. sysdev_driver_unregister(&cpu_sysdev_class, &mc_sysdev_driver);
  727. put_online_cpus();
  728. platform_device_unregister(microcode_pdev);
  729. }
  730. module_init(microcode_init)
  731. module_exit(microcode_exit)