microcode.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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 <linux/cpu.h>
  86. #include <linux/firmware.h>
  87. #include <linux/platform_device.h>
  88. #include <asm/msr.h>
  89. #include <asm/uaccess.h>
  90. #include <asm/processor.h>
  91. MODULE_DESCRIPTION("Intel CPU (IA-32) Microcode Update Driver");
  92. MODULE_AUTHOR("Tigran Aivazian <tigran@veritas.com>");
  93. MODULE_LICENSE("GPL");
  94. #define MICROCODE_VERSION "1.14a"
  95. #define DEFAULT_UCODE_DATASIZE (2000) /* 2000 bytes */
  96. #define MC_HEADER_SIZE (sizeof (microcode_header_t)) /* 48 bytes */
  97. #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE) /* 2048 bytes */
  98. #define EXT_HEADER_SIZE (sizeof (struct extended_sigtable)) /* 20 bytes */
  99. #define EXT_SIGNATURE_SIZE (sizeof (struct extended_signature)) /* 12 bytes */
  100. #define DWSIZE (sizeof (u32))
  101. #define get_totalsize(mc) \
  102. (((microcode_t *)mc)->hdr.totalsize ? \
  103. ((microcode_t *)mc)->hdr.totalsize : DEFAULT_UCODE_TOTALSIZE)
  104. #define get_datasize(mc) \
  105. (((microcode_t *)mc)->hdr.datasize ? \
  106. ((microcode_t *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE)
  107. #define sigmatch(s1, s2, p1, p2) \
  108. (((s1) == (s2)) && (((p1) & (p2)) || (((p1) == 0) && ((p2) == 0))))
  109. #define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE)
  110. /* serialize access to the physical write to MSR 0x79 */
  111. static DEFINE_SPINLOCK(microcode_update_lock);
  112. /* no concurrent ->write()s are allowed on /dev/cpu/microcode */
  113. static DEFINE_MUTEX(microcode_mutex);
  114. static struct ucode_cpu_info {
  115. int valid;
  116. unsigned int sig;
  117. unsigned int pf;
  118. unsigned int rev;
  119. microcode_t *mc;
  120. } ucode_cpu_info[NR_CPUS];
  121. static void collect_cpu_info(int cpu_num)
  122. {
  123. struct cpuinfo_x86 *c = cpu_data + cpu_num;
  124. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  125. unsigned int val[2];
  126. /* We should bind the task to the CPU */
  127. BUG_ON(raw_smp_processor_id() != cpu_num);
  128. uci->pf = uci->rev = 0;
  129. uci->mc = NULL;
  130. uci->valid = 1;
  131. if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
  132. cpu_has(c, X86_FEATURE_IA64)) {
  133. printk(KERN_ERR "microcode: CPU%d not a capable Intel "
  134. "processor\n", cpu_num);
  135. uci->valid = 0;
  136. return;
  137. }
  138. uci->sig = cpuid_eax(0x00000001);
  139. if ((c->x86_model >= 5) || (c->x86 > 6)) {
  140. /* get processor flags from MSR 0x17 */
  141. rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  142. uci->pf = 1 << ((val[1] >> 18) & 7);
  143. }
  144. wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  145. /* see notes above for revision 1.07. Apparent chip bug */
  146. sync_core();
  147. /* get the current revision from MSR 0x8B */
  148. rdmsr(MSR_IA32_UCODE_REV, val[0], uci->rev);
  149. pr_debug("microcode: collect_cpu_info : sig=0x%x, pf=0x%x, rev=0x%x\n",
  150. uci->sig, uci->pf, uci->rev);
  151. }
  152. static inline int microcode_update_match(int cpu_num,
  153. microcode_header_t *mc_header, int sig, int pf)
  154. {
  155. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  156. if (!sigmatch(sig, uci->sig, pf, uci->pf)
  157. || mc_header->rev <= uci->rev)
  158. return 0;
  159. return 1;
  160. }
  161. static int microcode_sanity_check(void *mc)
  162. {
  163. microcode_header_t *mc_header = mc;
  164. struct extended_sigtable *ext_header = NULL;
  165. struct extended_signature *ext_sig;
  166. unsigned long total_size, data_size, ext_table_size;
  167. int sum, orig_sum, ext_sigcount = 0, i;
  168. total_size = get_totalsize(mc_header);
  169. data_size = get_datasize(mc_header);
  170. if ((data_size + MC_HEADER_SIZE > total_size)
  171. || (data_size < DEFAULT_UCODE_DATASIZE)) {
  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. || (total_size < DEFAULT_UCODE_TOTALSIZE)) {
  329. printk(KERN_ERR "microcode: error! Bad total size in microcode "
  330. "data file\n");
  331. return -EINVAL;
  332. }
  333. *mc = vmalloc(total_size);
  334. if (!*mc)
  335. return -ENOMEM;
  336. if (copy_from_user(*mc, user_buffer + offset, total_size)) {
  337. printk(KERN_ERR "microcode: error! Can not read user data\n");
  338. vfree(*mc);
  339. return -EFAULT;
  340. }
  341. return offset + total_size;
  342. }
  343. static int do_microcode_update (void)
  344. {
  345. long cursor = 0;
  346. int error = 0;
  347. void * new_mc;
  348. int cpu;
  349. cpumask_t old;
  350. old = current->cpus_allowed;
  351. while ((cursor = get_next_ucode(&new_mc, cursor)) > 0) {
  352. error = microcode_sanity_check(new_mc);
  353. if (error)
  354. goto out;
  355. /*
  356. * It's possible the data file has multiple matching ucode,
  357. * lets keep searching till the latest version
  358. */
  359. for_each_online_cpu(cpu) {
  360. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  361. if (!uci->valid)
  362. continue;
  363. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  364. error = get_maching_microcode(new_mc, cpu);
  365. if (error < 0)
  366. goto out;
  367. if (error == 1)
  368. apply_microcode(cpu);
  369. }
  370. vfree(new_mc);
  371. }
  372. out:
  373. if (cursor > 0)
  374. vfree(new_mc);
  375. if (cursor < 0)
  376. error = cursor;
  377. set_cpus_allowed(current, old);
  378. return error;
  379. }
  380. static int microcode_open (struct inode *unused1, struct file *unused2)
  381. {
  382. return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
  383. }
  384. static ssize_t microcode_write (struct file *file, const char __user *buf, size_t len, loff_t *ppos)
  385. {
  386. ssize_t ret;
  387. if (len < DEFAULT_UCODE_TOTALSIZE) {
  388. printk(KERN_ERR "microcode: not enough data\n");
  389. return -EINVAL;
  390. }
  391. if ((len >> PAGE_SHIFT) > num_physpages) {
  392. printk(KERN_ERR "microcode: too much data (max %ld pages)\n", num_physpages);
  393. return -EINVAL;
  394. }
  395. lock_cpu_hotplug();
  396. mutex_lock(&microcode_mutex);
  397. user_buffer = (void __user *) buf;
  398. user_buffer_size = (int) len;
  399. ret = do_microcode_update();
  400. if (!ret)
  401. ret = (ssize_t)len;
  402. mutex_unlock(&microcode_mutex);
  403. unlock_cpu_hotplug();
  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. .fops = &microcode_fops,
  415. };
  416. static int __init microcode_dev_init (void)
  417. {
  418. int error;
  419. error = misc_register(&microcode_dev);
  420. if (error) {
  421. printk(KERN_ERR
  422. "microcode: can't misc_register on minor=%d\n",
  423. MICROCODE_MINOR);
  424. return error;
  425. }
  426. return 0;
  427. }
  428. static void __exit microcode_dev_exit (void)
  429. {
  430. misc_deregister(&microcode_dev);
  431. }
  432. MODULE_ALIAS_MISCDEV(MICROCODE_MINOR);
  433. #else
  434. #define microcode_dev_init() 0
  435. #define microcode_dev_exit() do { } while(0)
  436. #endif
  437. static long get_next_ucode_from_buffer(void **mc, void *buf,
  438. unsigned long size, long offset)
  439. {
  440. microcode_header_t *mc_header;
  441. unsigned long total_size;
  442. /* No more data */
  443. if (offset >= size)
  444. return 0;
  445. mc_header = (microcode_header_t *)(buf + offset);
  446. total_size = get_totalsize(mc_header);
  447. if ((offset + total_size > size)
  448. || (total_size < DEFAULT_UCODE_TOTALSIZE)) {
  449. printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
  450. return -EINVAL;
  451. }
  452. *mc = vmalloc(total_size);
  453. if (!*mc) {
  454. printk(KERN_ERR "microcode: error! Can not allocate memory\n");
  455. return -ENOMEM;
  456. }
  457. memcpy(*mc, buf + offset, total_size);
  458. return offset + total_size;
  459. }
  460. /* fake device for request_firmware */
  461. static struct platform_device *microcode_pdev;
  462. static int cpu_request_microcode(int cpu)
  463. {
  464. char name[30];
  465. struct cpuinfo_x86 *c = cpu_data + cpu;
  466. const struct firmware *firmware;
  467. void * buf;
  468. unsigned long size;
  469. long offset = 0;
  470. int error;
  471. void *mc;
  472. /* We should bind the task to the CPU */
  473. BUG_ON(cpu != raw_smp_processor_id());
  474. sprintf(name,"intel-ucode/%02x-%02x-%02x",
  475. c->x86, c->x86_model, c->x86_mask);
  476. error = request_firmware(&firmware, name, &microcode_pdev->dev);
  477. if (error) {
  478. pr_debug("ucode data file %s load failed\n", name);
  479. return error;
  480. }
  481. buf = (void *)firmware->data;
  482. size = firmware->size;
  483. while ((offset = get_next_ucode_from_buffer(&mc, buf, size, offset))
  484. > 0) {
  485. error = microcode_sanity_check(mc);
  486. if (error)
  487. break;
  488. error = get_maching_microcode(mc, cpu);
  489. if (error < 0)
  490. break;
  491. /*
  492. * It's possible the data file has multiple matching ucode,
  493. * lets keep searching till the latest version
  494. */
  495. if (error == 1) {
  496. apply_microcode(cpu);
  497. error = 0;
  498. }
  499. vfree(mc);
  500. }
  501. if (offset > 0)
  502. vfree(mc);
  503. if (offset < 0)
  504. error = offset;
  505. release_firmware(firmware);
  506. return error;
  507. }
  508. static void microcode_init_cpu(int cpu)
  509. {
  510. cpumask_t old;
  511. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  512. old = current->cpus_allowed;
  513. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  514. mutex_lock(&microcode_mutex);
  515. collect_cpu_info(cpu);
  516. if (uci->valid)
  517. cpu_request_microcode(cpu);
  518. mutex_unlock(&microcode_mutex);
  519. set_cpus_allowed(current, old);
  520. }
  521. static void microcode_fini_cpu(int cpu)
  522. {
  523. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  524. mutex_lock(&microcode_mutex);
  525. uci->valid = 0;
  526. vfree(uci->mc);
  527. uci->mc = NULL;
  528. mutex_unlock(&microcode_mutex);
  529. }
  530. static int __init microcode_init (void)
  531. {
  532. int error;
  533. error = microcode_dev_init();
  534. if (error)
  535. return error;
  536. microcode_pdev = platform_device_register_simple("microcode", -1,
  537. NULL, 0);
  538. if (IS_ERR(microcode_pdev)) {
  539. microcode_dev_exit();
  540. return PTR_ERR(microcode_pdev);
  541. }
  542. printk(KERN_INFO
  543. "IA-32 Microcode Update Driver: v" MICROCODE_VERSION " <tigran@veritas.com>\n");
  544. return 0;
  545. }
  546. static void __exit microcode_exit (void)
  547. {
  548. microcode_dev_exit();
  549. platform_device_unregister(microcode_pdev);
  550. }
  551. module_init(microcode_init)
  552. module_exit(microcode_exit)