microcode.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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/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@aivazian.fsnet.co.uk>");
  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. printk(KERN_ERR "microcode: error! "
  172. "Bad data size in microcode data file\n");
  173. return -EINVAL;
  174. }
  175. if (mc_header->ldrver != 1 || mc_header->hdrver != 1) {
  176. printk(KERN_ERR "microcode: error! "
  177. "Unknown microcode update format\n");
  178. return -EINVAL;
  179. }
  180. ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
  181. if (ext_table_size) {
  182. if ((ext_table_size < EXT_HEADER_SIZE)
  183. || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
  184. printk(KERN_ERR "microcode: error! "
  185. "Small exttable size in microcode data file\n");
  186. return -EINVAL;
  187. }
  188. ext_header = mc + MC_HEADER_SIZE + data_size;
  189. if (ext_table_size != exttable_size(ext_header)) {
  190. printk(KERN_ERR "microcode: error! "
  191. "Bad exttable size in microcode data file\n");
  192. return -EFAULT;
  193. }
  194. ext_sigcount = ext_header->count;
  195. }
  196. /* check extended table checksum */
  197. if (ext_table_size) {
  198. int ext_table_sum = 0;
  199. int *ext_tablep = (int *)ext_header;
  200. i = ext_table_size / DWSIZE;
  201. while (i--)
  202. ext_table_sum += ext_tablep[i];
  203. if (ext_table_sum) {
  204. printk(KERN_WARNING "microcode: aborting, "
  205. "bad extended signature table checksum\n");
  206. return -EINVAL;
  207. }
  208. }
  209. /* calculate the checksum */
  210. orig_sum = 0;
  211. i = (MC_HEADER_SIZE + data_size) / DWSIZE;
  212. while (i--)
  213. orig_sum += ((int *)mc)[i];
  214. if (orig_sum) {
  215. printk(KERN_ERR "microcode: aborting, bad checksum\n");
  216. return -EINVAL;
  217. }
  218. if (!ext_table_size)
  219. return 0;
  220. /* check extended signature checksum */
  221. for (i = 0; i < ext_sigcount; i++) {
  222. ext_sig = (struct extended_signature *)((void *)ext_header
  223. + EXT_HEADER_SIZE + EXT_SIGNATURE_SIZE * i);
  224. sum = orig_sum
  225. - (mc_header->sig + mc_header->pf + mc_header->cksum)
  226. + (ext_sig->sig + ext_sig->pf + ext_sig->cksum);
  227. if (sum) {
  228. printk(KERN_ERR "microcode: aborting, bad checksum\n");
  229. return -EINVAL;
  230. }
  231. }
  232. return 0;
  233. }
  234. /*
  235. * return 0 - no update found
  236. * return 1 - found update
  237. * return < 0 - error
  238. */
  239. static int get_maching_microcode(void *mc, int cpu)
  240. {
  241. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  242. microcode_header_t *mc_header = mc;
  243. struct extended_sigtable *ext_header;
  244. unsigned long total_size = get_totalsize(mc_header);
  245. int ext_sigcount, i;
  246. struct extended_signature *ext_sig;
  247. void *new_mc;
  248. if (microcode_update_match(cpu, mc_header,
  249. mc_header->sig, mc_header->pf))
  250. goto find;
  251. if (total_size <= get_datasize(mc_header) + MC_HEADER_SIZE)
  252. return 0;
  253. ext_header = (struct extended_sigtable *)(mc +
  254. get_datasize(mc_header) + MC_HEADER_SIZE);
  255. ext_sigcount = ext_header->count;
  256. ext_sig = (struct extended_signature *)((void *)ext_header
  257. + EXT_HEADER_SIZE);
  258. for (i = 0; i < ext_sigcount; i++) {
  259. if (microcode_update_match(cpu, mc_header,
  260. ext_sig->sig, ext_sig->pf))
  261. goto find;
  262. ext_sig++;
  263. }
  264. return 0;
  265. find:
  266. pr_debug("microcode: CPU %d found a matching microcode update with"
  267. " version 0x%x (current=0x%x)\n", cpu, mc_header->rev,uci->rev);
  268. new_mc = vmalloc(total_size);
  269. if (!new_mc) {
  270. printk(KERN_ERR "microcode: error! Can not allocate memory\n");
  271. return -ENOMEM;
  272. }
  273. /* free previous update file */
  274. vfree(uci->mc);
  275. memcpy(new_mc, mc, total_size);
  276. uci->mc = new_mc;
  277. return 1;
  278. }
  279. static void apply_microcode(int cpu)
  280. {
  281. unsigned long flags;
  282. unsigned int val[2];
  283. int cpu_num = raw_smp_processor_id();
  284. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  285. /* We should bind the task to the CPU */
  286. BUG_ON(cpu_num != cpu);
  287. if (uci->mc == NULL)
  288. return;
  289. /* serialize access to the physical write to MSR 0x79 */
  290. spin_lock_irqsave(&microcode_update_lock, flags);
  291. /* write microcode via MSR 0x79 */
  292. wrmsr(MSR_IA32_UCODE_WRITE,
  293. (unsigned long) uci->mc->bits,
  294. (unsigned long) uci->mc->bits >> 16 >> 16);
  295. wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  296. /* see notes above for revision 1.07. Apparent chip bug */
  297. sync_core();
  298. /* get the current revision from MSR 0x8B */
  299. rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  300. spin_unlock_irqrestore(&microcode_update_lock, flags);
  301. if (val[1] != uci->mc->hdr.rev) {
  302. printk(KERN_ERR "microcode: CPU%d updated from revision "
  303. "0x%x to 0x%x failed\n", cpu_num, uci->rev, val[1]);
  304. return;
  305. }
  306. pr_debug("microcode: CPU%d updated from revision "
  307. "0x%x to 0x%x, date = %08x \n",
  308. cpu_num, uci->rev, val[1], uci->mc->hdr.date);
  309. uci->rev = val[1];
  310. }
  311. #ifdef CONFIG_MICROCODE_OLD_INTERFACE
  312. static void __user *user_buffer; /* user area microcode data buffer */
  313. static unsigned int user_buffer_size; /* it's size */
  314. static long get_next_ucode(void **mc, long offset)
  315. {
  316. microcode_header_t mc_header;
  317. unsigned long total_size;
  318. /* No more data */
  319. if (offset >= user_buffer_size)
  320. return 0;
  321. if (copy_from_user(&mc_header, user_buffer + offset, MC_HEADER_SIZE)) {
  322. printk(KERN_ERR "microcode: error! Can not read user data\n");
  323. return -EFAULT;
  324. }
  325. total_size = get_totalsize(&mc_header);
  326. if (offset + total_size > user_buffer_size) {
  327. printk(KERN_ERR "microcode: error! Bad total size in microcode "
  328. "data file\n");
  329. return -EINVAL;
  330. }
  331. *mc = vmalloc(total_size);
  332. if (!*mc)
  333. return -ENOMEM;
  334. if (copy_from_user(*mc, user_buffer + offset, total_size)) {
  335. printk(KERN_ERR "microcode: error! Can not read user data\n");
  336. vfree(*mc);
  337. return -EFAULT;
  338. }
  339. return offset + total_size;
  340. }
  341. static int do_microcode_update (void)
  342. {
  343. long cursor = 0;
  344. int error = 0;
  345. void *new_mc;
  346. int cpu;
  347. cpumask_t old;
  348. old = current->cpus_allowed;
  349. while ((cursor = get_next_ucode(&new_mc, cursor)) > 0) {
  350. error = microcode_sanity_check(new_mc);
  351. if (error)
  352. goto out;
  353. /*
  354. * It's possible the data file has multiple matching ucode,
  355. * lets keep searching till the latest version
  356. */
  357. for_each_online_cpu(cpu) {
  358. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  359. if (!uci->valid)
  360. continue;
  361. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  362. error = get_maching_microcode(new_mc, cpu);
  363. if (error < 0)
  364. goto out;
  365. if (error == 1)
  366. apply_microcode(cpu);
  367. }
  368. vfree(new_mc);
  369. }
  370. out:
  371. if (cursor > 0)
  372. vfree(new_mc);
  373. if (cursor < 0)
  374. error = cursor;
  375. set_cpus_allowed(current, old);
  376. return error;
  377. }
  378. static int microcode_open (struct inode *unused1, struct file *unused2)
  379. {
  380. return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
  381. }
  382. static ssize_t microcode_write (struct file *file, const char __user *buf, size_t len, loff_t *ppos)
  383. {
  384. ssize_t ret;
  385. if ((len >> PAGE_SHIFT) > num_physpages) {
  386. printk(KERN_ERR "microcode: too much data (max %ld pages)\n", num_physpages);
  387. return -EINVAL;
  388. }
  389. lock_cpu_hotplug();
  390. mutex_lock(&microcode_mutex);
  391. user_buffer = (void __user *) buf;
  392. user_buffer_size = (int) len;
  393. ret = do_microcode_update();
  394. if (!ret)
  395. ret = (ssize_t)len;
  396. mutex_unlock(&microcode_mutex);
  397. unlock_cpu_hotplug();
  398. return ret;
  399. }
  400. static struct file_operations microcode_fops = {
  401. .owner = THIS_MODULE,
  402. .write = microcode_write,
  403. .open = microcode_open,
  404. };
  405. static struct miscdevice microcode_dev = {
  406. .minor = MICROCODE_MINOR,
  407. .name = "microcode",
  408. .fops = &microcode_fops,
  409. };
  410. static int __init microcode_dev_init (void)
  411. {
  412. int error;
  413. error = misc_register(&microcode_dev);
  414. if (error) {
  415. printk(KERN_ERR
  416. "microcode: can't misc_register on minor=%d\n",
  417. MICROCODE_MINOR);
  418. return error;
  419. }
  420. return 0;
  421. }
  422. static void __exit microcode_dev_exit (void)
  423. {
  424. misc_deregister(&microcode_dev);
  425. }
  426. MODULE_ALIAS_MISCDEV(MICROCODE_MINOR);
  427. #else
  428. #define microcode_dev_init() 0
  429. #define microcode_dev_exit() do { } while(0)
  430. #endif
  431. static long get_next_ucode_from_buffer(void **mc, void *buf,
  432. unsigned long size, long offset)
  433. {
  434. microcode_header_t *mc_header;
  435. unsigned long total_size;
  436. /* No more data */
  437. if (offset >= size)
  438. return 0;
  439. mc_header = (microcode_header_t *)(buf + offset);
  440. total_size = get_totalsize(mc_header);
  441. if (offset + total_size > size) {
  442. printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
  443. return -EINVAL;
  444. }
  445. *mc = vmalloc(total_size);
  446. if (!*mc) {
  447. printk(KERN_ERR "microcode: error! Can not allocate memory\n");
  448. return -ENOMEM;
  449. }
  450. memcpy(*mc, buf + offset, total_size);
  451. return offset + total_size;
  452. }
  453. /* fake device for request_firmware */
  454. static struct platform_device *microcode_pdev;
  455. static int cpu_request_microcode(int cpu)
  456. {
  457. char name[30];
  458. struct cpuinfo_x86 *c = cpu_data + cpu;
  459. const struct firmware *firmware;
  460. void *buf;
  461. unsigned long size;
  462. long offset = 0;
  463. int error;
  464. void *mc;
  465. /* We should bind the task to the CPU */
  466. BUG_ON(cpu != raw_smp_processor_id());
  467. sprintf(name,"intel-ucode/%02x-%02x-%02x",
  468. c->x86, c->x86_model, c->x86_mask);
  469. error = request_firmware(&firmware, name, &microcode_pdev->dev);
  470. if (error) {
  471. pr_debug("ucode data file %s load failed\n", name);
  472. return error;
  473. }
  474. buf = (void *)firmware->data;
  475. size = firmware->size;
  476. while ((offset = get_next_ucode_from_buffer(&mc, buf, size, offset))
  477. > 0) {
  478. error = microcode_sanity_check(mc);
  479. if (error)
  480. break;
  481. error = get_maching_microcode(mc, cpu);
  482. if (error < 0)
  483. break;
  484. /*
  485. * It's possible the data file has multiple matching ucode,
  486. * lets keep searching till the latest version
  487. */
  488. if (error == 1) {
  489. apply_microcode(cpu);
  490. error = 0;
  491. }
  492. vfree(mc);
  493. }
  494. if (offset > 0)
  495. vfree(mc);
  496. if (offset < 0)
  497. error = offset;
  498. release_firmware(firmware);
  499. return error;
  500. }
  501. static void microcode_init_cpu(int cpu)
  502. {
  503. cpumask_t old;
  504. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  505. old = current->cpus_allowed;
  506. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  507. mutex_lock(&microcode_mutex);
  508. collect_cpu_info(cpu);
  509. if (uci->valid && system_state == SYSTEM_RUNNING)
  510. cpu_request_microcode(cpu);
  511. mutex_unlock(&microcode_mutex);
  512. set_cpus_allowed(current, old);
  513. }
  514. static void microcode_fini_cpu(int cpu)
  515. {
  516. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  517. mutex_lock(&microcode_mutex);
  518. uci->valid = 0;
  519. vfree(uci->mc);
  520. uci->mc = NULL;
  521. mutex_unlock(&microcode_mutex);
  522. }
  523. static ssize_t reload_store(struct sys_device *dev, const char *buf, size_t sz)
  524. {
  525. struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
  526. char *end;
  527. unsigned long val = simple_strtoul(buf, &end, 0);
  528. int err = 0;
  529. int cpu = dev->id;
  530. if (end == buf)
  531. return -EINVAL;
  532. if (val == 1) {
  533. cpumask_t old;
  534. old = current->cpus_allowed;
  535. lock_cpu_hotplug();
  536. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  537. mutex_lock(&microcode_mutex);
  538. if (uci->valid)
  539. err = cpu_request_microcode(cpu);
  540. mutex_unlock(&microcode_mutex);
  541. unlock_cpu_hotplug();
  542. set_cpus_allowed(current, old);
  543. }
  544. if (err)
  545. return err;
  546. return sz;
  547. }
  548. static ssize_t version_show(struct sys_device *dev, char *buf)
  549. {
  550. struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
  551. return sprintf(buf, "0x%x\n", uci->rev);
  552. }
  553. static ssize_t pf_show(struct sys_device *dev, char *buf)
  554. {
  555. struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
  556. return sprintf(buf, "0x%x\n", uci->pf);
  557. }
  558. static SYSDEV_ATTR(reload, 0200, NULL, reload_store);
  559. static SYSDEV_ATTR(version, 0400, version_show, NULL);
  560. static SYSDEV_ATTR(processor_flags, 0400, pf_show, NULL);
  561. static struct attribute *mc_default_attrs[] = {
  562. &attr_reload.attr,
  563. &attr_version.attr,
  564. &attr_processor_flags.attr,
  565. NULL
  566. };
  567. static struct attribute_group mc_attr_group = {
  568. .attrs = mc_default_attrs,
  569. .name = "microcode",
  570. };
  571. static int mc_sysdev_add(struct sys_device *sys_dev)
  572. {
  573. int err, cpu = sys_dev->id;
  574. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  575. if (!cpu_online(cpu))
  576. return 0;
  577. pr_debug("Microcode:CPU %d added\n", cpu);
  578. memset(uci, 0, sizeof(*uci));
  579. err = sysfs_create_group(&sys_dev->kobj, &mc_attr_group);
  580. if (err)
  581. return err;
  582. microcode_init_cpu(cpu);
  583. return 0;
  584. }
  585. static int mc_sysdev_remove(struct sys_device *sys_dev)
  586. {
  587. int cpu = sys_dev->id;
  588. if (!cpu_online(cpu))
  589. return 0;
  590. pr_debug("Microcode:CPU %d removed\n", cpu);
  591. microcode_fini_cpu(cpu);
  592. sysfs_remove_group(&sys_dev->kobj, &mc_attr_group);
  593. return 0;
  594. }
  595. static int mc_sysdev_resume(struct sys_device *dev)
  596. {
  597. int cpu = dev->id;
  598. if (!cpu_online(cpu))
  599. return 0;
  600. pr_debug("Microcode:CPU %d resumed\n", cpu);
  601. /* only CPU 0 will apply ucode here */
  602. apply_microcode(0);
  603. return 0;
  604. }
  605. static struct sysdev_driver mc_sysdev_driver = {
  606. .add = mc_sysdev_add,
  607. .remove = mc_sysdev_remove,
  608. .resume = mc_sysdev_resume,
  609. };
  610. static __cpuinit int
  611. mc_cpu_callback(struct notifier_block *nb, unsigned long action, void *hcpu)
  612. {
  613. unsigned int cpu = (unsigned long)hcpu;
  614. struct sys_device *sys_dev;
  615. sys_dev = get_cpu_sysdev(cpu);
  616. switch (action) {
  617. case CPU_ONLINE:
  618. case CPU_DOWN_FAILED:
  619. mc_sysdev_add(sys_dev);
  620. break;
  621. case CPU_DOWN_PREPARE:
  622. mc_sysdev_remove(sys_dev);
  623. break;
  624. }
  625. return NOTIFY_OK;
  626. }
  627. static struct notifier_block mc_cpu_notifier = {
  628. .notifier_call = mc_cpu_callback,
  629. };
  630. static int __init microcode_init (void)
  631. {
  632. int error;
  633. error = microcode_dev_init();
  634. if (error)
  635. return error;
  636. microcode_pdev = platform_device_register_simple("microcode", -1,
  637. NULL, 0);
  638. if (IS_ERR(microcode_pdev)) {
  639. microcode_dev_exit();
  640. return PTR_ERR(microcode_pdev);
  641. }
  642. lock_cpu_hotplug();
  643. error = sysdev_driver_register(&cpu_sysdev_class, &mc_sysdev_driver);
  644. unlock_cpu_hotplug();
  645. if (error) {
  646. microcode_dev_exit();
  647. platform_device_unregister(microcode_pdev);
  648. return error;
  649. }
  650. register_hotcpu_notifier(&mc_cpu_notifier);
  651. printk(KERN_INFO
  652. "IA-32 Microcode Update Driver: v" MICROCODE_VERSION " <tigran@aivazian.fsnet.co.uk>\n");
  653. return 0;
  654. }
  655. static void __exit microcode_exit (void)
  656. {
  657. microcode_dev_exit();
  658. unregister_hotcpu_notifier(&mc_cpu_notifier);
  659. lock_cpu_hotplug();
  660. sysdev_driver_unregister(&cpu_sysdev_class, &mc_sysdev_driver);
  661. unlock_cpu_hotplug();
  662. platform_device_unregister(microcode_pdev);
  663. }
  664. module_init(microcode_init)
  665. module_exit(microcode_exit)