microcode.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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 = (void *)ext_header + EXT_HEADER_SIZE +
  224. 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 = mc + get_datasize(mc_header) + MC_HEADER_SIZE;
  255. ext_sigcount = ext_header->count;
  256. ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
  257. for (i = 0; i < ext_sigcount; i++) {
  258. if (microcode_update_match(cpu, mc_header,
  259. ext_sig->sig, ext_sig->pf))
  260. goto find;
  261. ext_sig++;
  262. }
  263. return 0;
  264. find:
  265. pr_debug("microcode: CPU %d found a matching microcode update with"
  266. " version 0x%x (current=0x%x)\n", cpu, mc_header->rev,uci->rev);
  267. new_mc = vmalloc(total_size);
  268. if (!new_mc) {
  269. printk(KERN_ERR "microcode: error! Can not allocate memory\n");
  270. return -ENOMEM;
  271. }
  272. /* free previous update file */
  273. vfree(uci->mc);
  274. memcpy(new_mc, mc, total_size);
  275. uci->mc = new_mc;
  276. return 1;
  277. }
  278. static void apply_microcode(int cpu)
  279. {
  280. unsigned long flags;
  281. unsigned int val[2];
  282. int cpu_num = raw_smp_processor_id();
  283. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  284. /* We should bind the task to the CPU */
  285. BUG_ON(cpu_num != cpu);
  286. if (uci->mc == NULL)
  287. return;
  288. /* serialize access to the physical write to MSR 0x79 */
  289. spin_lock_irqsave(&microcode_update_lock, flags);
  290. /* write microcode via MSR 0x79 */
  291. wrmsr(MSR_IA32_UCODE_WRITE,
  292. (unsigned long) uci->mc->bits,
  293. (unsigned long) uci->mc->bits >> 16 >> 16);
  294. wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  295. /* see notes above for revision 1.07. Apparent chip bug */
  296. sync_core();
  297. /* get the current revision from MSR 0x8B */
  298. rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  299. spin_unlock_irqrestore(&microcode_update_lock, flags);
  300. if (val[1] != uci->mc->hdr.rev) {
  301. printk(KERN_ERR "microcode: CPU%d updated from revision "
  302. "0x%x to 0x%x failed\n", cpu_num, uci->rev, val[1]);
  303. return;
  304. }
  305. pr_debug("microcode: CPU%d updated from revision "
  306. "0x%x to 0x%x, date = %08x \n",
  307. cpu_num, uci->rev, val[1], uci->mc->hdr.date);
  308. uci->rev = val[1];
  309. }
  310. #ifdef CONFIG_MICROCODE_OLD_INTERFACE
  311. static void __user *user_buffer; /* user area microcode data buffer */
  312. static unsigned int user_buffer_size; /* it's size */
  313. static long get_next_ucode(void **mc, long offset)
  314. {
  315. microcode_header_t mc_header;
  316. unsigned long total_size;
  317. /* No more data */
  318. if (offset >= user_buffer_size)
  319. return 0;
  320. if (copy_from_user(&mc_header, user_buffer + offset, MC_HEADER_SIZE)) {
  321. printk(KERN_ERR "microcode: error! Can not read user data\n");
  322. return -EFAULT;
  323. }
  324. total_size = get_totalsize(&mc_header);
  325. if (offset + total_size > user_buffer_size) {
  326. printk(KERN_ERR "microcode: error! Bad total size in microcode "
  327. "data file\n");
  328. return -EINVAL;
  329. }
  330. *mc = vmalloc(total_size);
  331. if (!*mc)
  332. return -ENOMEM;
  333. if (copy_from_user(*mc, user_buffer + offset, total_size)) {
  334. printk(KERN_ERR "microcode: error! Can not read user data\n");
  335. vfree(*mc);
  336. return -EFAULT;
  337. }
  338. return offset + total_size;
  339. }
  340. static int do_microcode_update (void)
  341. {
  342. long cursor = 0;
  343. int error = 0;
  344. void *new_mc = NULL;
  345. int cpu;
  346. cpumask_t old;
  347. old = current->cpus_allowed;
  348. while ((cursor = get_next_ucode(&new_mc, cursor)) > 0) {
  349. error = microcode_sanity_check(new_mc);
  350. if (error)
  351. goto out;
  352. /*
  353. * It's possible the data file has multiple matching ucode,
  354. * lets keep searching till the latest version
  355. */
  356. for_each_online_cpu(cpu) {
  357. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  358. if (!uci->valid)
  359. continue;
  360. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  361. error = get_maching_microcode(new_mc, cpu);
  362. if (error < 0)
  363. goto out;
  364. if (error == 1)
  365. apply_microcode(cpu);
  366. }
  367. vfree(new_mc);
  368. }
  369. out:
  370. if (cursor > 0)
  371. vfree(new_mc);
  372. if (cursor < 0)
  373. error = cursor;
  374. set_cpus_allowed(current, old);
  375. return error;
  376. }
  377. static int microcode_open (struct inode *unused1, struct file *unused2)
  378. {
  379. return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
  380. }
  381. static ssize_t microcode_write (struct file *file, const char __user *buf, size_t len, loff_t *ppos)
  382. {
  383. ssize_t ret;
  384. if ((len >> PAGE_SHIFT) > num_physpages) {
  385. printk(KERN_ERR "microcode: too much data (max %ld pages)\n", num_physpages);
  386. return -EINVAL;
  387. }
  388. get_online_cpus();
  389. mutex_lock(&microcode_mutex);
  390. user_buffer = (void __user *) buf;
  391. user_buffer_size = (int) len;
  392. ret = do_microcode_update();
  393. if (!ret)
  394. ret = (ssize_t)len;
  395. mutex_unlock(&microcode_mutex);
  396. put_online_cpus();
  397. return ret;
  398. }
  399. static const struct file_operations microcode_fops = {
  400. .owner = THIS_MODULE,
  401. .write = microcode_write,
  402. .open = microcode_open,
  403. };
  404. static struct miscdevice microcode_dev = {
  405. .minor = MICROCODE_MINOR,
  406. .name = "microcode",
  407. .fops = &microcode_fops,
  408. };
  409. static int __init microcode_dev_init (void)
  410. {
  411. int error;
  412. error = misc_register(&microcode_dev);
  413. if (error) {
  414. printk(KERN_ERR
  415. "microcode: can't misc_register on minor=%d\n",
  416. MICROCODE_MINOR);
  417. return error;
  418. }
  419. return 0;
  420. }
  421. static void microcode_dev_exit (void)
  422. {
  423. misc_deregister(&microcode_dev);
  424. }
  425. MODULE_ALIAS_MISCDEV(MICROCODE_MINOR);
  426. #else
  427. #define microcode_dev_init() 0
  428. #define microcode_dev_exit() do { } while(0)
  429. #endif
  430. static long get_next_ucode_from_buffer(void **mc, void *buf,
  431. unsigned long size, long offset)
  432. {
  433. microcode_header_t *mc_header;
  434. unsigned long total_size;
  435. /* No more data */
  436. if (offset >= size)
  437. return 0;
  438. mc_header = (microcode_header_t *)(buf + offset);
  439. total_size = get_totalsize(mc_header);
  440. if (offset + total_size > size) {
  441. printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
  442. return -EINVAL;
  443. }
  444. *mc = vmalloc(total_size);
  445. if (!*mc) {
  446. printk(KERN_ERR "microcode: error! Can not allocate memory\n");
  447. return -ENOMEM;
  448. }
  449. memcpy(*mc, buf + offset, total_size);
  450. return offset + total_size;
  451. }
  452. /* fake device for request_firmware */
  453. static struct platform_device *microcode_pdev;
  454. static int cpu_request_microcode(int cpu)
  455. {
  456. char name[30];
  457. struct cpuinfo_x86 *c = &cpu_data(cpu);
  458. const struct firmware *firmware;
  459. void *buf;
  460. unsigned long size;
  461. long offset = 0;
  462. int error;
  463. void *mc;
  464. /* We should bind the task to the CPU */
  465. BUG_ON(cpu != raw_smp_processor_id());
  466. sprintf(name,"intel-ucode/%02x-%02x-%02x",
  467. c->x86, c->x86_model, c->x86_mask);
  468. error = request_firmware(&firmware, name, &microcode_pdev->dev);
  469. if (error) {
  470. pr_debug("ucode data file %s load failed\n", name);
  471. return error;
  472. }
  473. buf = firmware->data;
  474. size = firmware->size;
  475. while ((offset = get_next_ucode_from_buffer(&mc, buf, size, offset))
  476. > 0) {
  477. error = microcode_sanity_check(mc);
  478. if (error)
  479. break;
  480. error = get_maching_microcode(mc, cpu);
  481. if (error < 0)
  482. break;
  483. /*
  484. * It's possible the data file has multiple matching ucode,
  485. * lets keep searching till the latest version
  486. */
  487. if (error == 1) {
  488. apply_microcode(cpu);
  489. error = 0;
  490. }
  491. vfree(mc);
  492. }
  493. if (offset > 0)
  494. vfree(mc);
  495. if (offset < 0)
  496. error = offset;
  497. release_firmware(firmware);
  498. return error;
  499. }
  500. static int apply_microcode_check_cpu(int cpu)
  501. {
  502. struct cpuinfo_x86 *c = &cpu_data(cpu);
  503. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  504. cpumask_t old;
  505. unsigned int val[2];
  506. int err = 0;
  507. /* Check if the microcode is available */
  508. if (!uci->mc)
  509. return 0;
  510. old = current->cpus_allowed;
  511. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  512. /* Check if the microcode we have in memory matches the CPU */
  513. if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
  514. cpu_has(c, X86_FEATURE_IA64) || uci->sig != cpuid_eax(0x00000001))
  515. err = -EINVAL;
  516. if (!err && ((c->x86_model >= 5) || (c->x86 > 6))) {
  517. /* get processor flags from MSR 0x17 */
  518. rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  519. if (uci->pf != (1 << ((val[1] >> 18) & 7)))
  520. err = -EINVAL;
  521. }
  522. if (!err) {
  523. wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  524. /* see notes above for revision 1.07. Apparent chip bug */
  525. sync_core();
  526. /* get the current revision from MSR 0x8B */
  527. rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  528. if (uci->rev != val[1])
  529. err = -EINVAL;
  530. }
  531. if (!err)
  532. apply_microcode(cpu);
  533. else
  534. printk(KERN_ERR "microcode: Could not apply microcode to CPU%d:"
  535. " sig=0x%x, pf=0x%x, rev=0x%x\n",
  536. cpu, uci->sig, uci->pf, uci->rev);
  537. set_cpus_allowed(current, old);
  538. return err;
  539. }
  540. static void microcode_init_cpu(int cpu, int resume)
  541. {
  542. cpumask_t old;
  543. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  544. old = current->cpus_allowed;
  545. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  546. mutex_lock(&microcode_mutex);
  547. collect_cpu_info(cpu);
  548. if (uci->valid && system_state == SYSTEM_RUNNING && !resume)
  549. cpu_request_microcode(cpu);
  550. mutex_unlock(&microcode_mutex);
  551. set_cpus_allowed(current, old);
  552. }
  553. static void microcode_fini_cpu(int cpu)
  554. {
  555. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  556. mutex_lock(&microcode_mutex);
  557. uci->valid = 0;
  558. vfree(uci->mc);
  559. uci->mc = NULL;
  560. mutex_unlock(&microcode_mutex);
  561. }
  562. static ssize_t reload_store(struct sys_device *dev, const char *buf, size_t sz)
  563. {
  564. struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
  565. char *end;
  566. unsigned long val = simple_strtoul(buf, &end, 0);
  567. int err = 0;
  568. int cpu = dev->id;
  569. if (end == buf)
  570. return -EINVAL;
  571. if (val == 1) {
  572. cpumask_t old;
  573. old = current->cpus_allowed;
  574. get_online_cpus();
  575. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  576. mutex_lock(&microcode_mutex);
  577. if (uci->valid)
  578. err = cpu_request_microcode(cpu);
  579. mutex_unlock(&microcode_mutex);
  580. put_online_cpus();
  581. set_cpus_allowed(current, old);
  582. }
  583. if (err)
  584. return err;
  585. return sz;
  586. }
  587. static ssize_t version_show(struct sys_device *dev, char *buf)
  588. {
  589. struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
  590. return sprintf(buf, "0x%x\n", uci->rev);
  591. }
  592. static ssize_t pf_show(struct sys_device *dev, char *buf)
  593. {
  594. struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
  595. return sprintf(buf, "0x%x\n", uci->pf);
  596. }
  597. static SYSDEV_ATTR(reload, 0200, NULL, reload_store);
  598. static SYSDEV_ATTR(version, 0400, version_show, NULL);
  599. static SYSDEV_ATTR(processor_flags, 0400, pf_show, NULL);
  600. static struct attribute *mc_default_attrs[] = {
  601. &attr_reload.attr,
  602. &attr_version.attr,
  603. &attr_processor_flags.attr,
  604. NULL
  605. };
  606. static struct attribute_group mc_attr_group = {
  607. .attrs = mc_default_attrs,
  608. .name = "microcode",
  609. };
  610. static int __mc_sysdev_add(struct sys_device *sys_dev, int resume)
  611. {
  612. int err, cpu = sys_dev->id;
  613. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  614. if (!cpu_online(cpu))
  615. return 0;
  616. pr_debug("Microcode:CPU %d added\n", cpu);
  617. memset(uci, 0, sizeof(*uci));
  618. err = sysfs_create_group(&sys_dev->kobj, &mc_attr_group);
  619. if (err)
  620. return err;
  621. microcode_init_cpu(cpu, resume);
  622. return 0;
  623. }
  624. static int mc_sysdev_add(struct sys_device *sys_dev)
  625. {
  626. return __mc_sysdev_add(sys_dev, 0);
  627. }
  628. static int mc_sysdev_remove(struct sys_device *sys_dev)
  629. {
  630. int cpu = sys_dev->id;
  631. if (!cpu_online(cpu))
  632. return 0;
  633. pr_debug("Microcode:CPU %d removed\n", cpu);
  634. microcode_fini_cpu(cpu);
  635. sysfs_remove_group(&sys_dev->kobj, &mc_attr_group);
  636. return 0;
  637. }
  638. static int mc_sysdev_resume(struct sys_device *dev)
  639. {
  640. int cpu = dev->id;
  641. if (!cpu_online(cpu))
  642. return 0;
  643. pr_debug("Microcode:CPU %d resumed\n", cpu);
  644. /* only CPU 0 will apply ucode here */
  645. apply_microcode(0);
  646. return 0;
  647. }
  648. static struct sysdev_driver mc_sysdev_driver = {
  649. .add = mc_sysdev_add,
  650. .remove = mc_sysdev_remove,
  651. .resume = mc_sysdev_resume,
  652. };
  653. static __cpuinit int
  654. mc_cpu_callback(struct notifier_block *nb, unsigned long action, void *hcpu)
  655. {
  656. unsigned int cpu = (unsigned long)hcpu;
  657. struct sys_device *sys_dev;
  658. sys_dev = get_cpu_sysdev(cpu);
  659. switch (action) {
  660. case CPU_UP_CANCELED_FROZEN:
  661. /* The CPU refused to come up during a system resume */
  662. microcode_fini_cpu(cpu);
  663. break;
  664. case CPU_ONLINE:
  665. case CPU_DOWN_FAILED:
  666. mc_sysdev_add(sys_dev);
  667. break;
  668. case CPU_ONLINE_FROZEN:
  669. /* System-wide resume is in progress, try to apply microcode */
  670. if (apply_microcode_check_cpu(cpu)) {
  671. /* The application of microcode failed */
  672. microcode_fini_cpu(cpu);
  673. __mc_sysdev_add(sys_dev, 1);
  674. break;
  675. }
  676. case CPU_DOWN_FAILED_FROZEN:
  677. if (sysfs_create_group(&sys_dev->kobj, &mc_attr_group))
  678. printk(KERN_ERR "Microcode: Failed to create the sysfs "
  679. "group for CPU%d\n", cpu);
  680. break;
  681. case CPU_DOWN_PREPARE:
  682. mc_sysdev_remove(sys_dev);
  683. break;
  684. case CPU_DOWN_PREPARE_FROZEN:
  685. /* Suspend is in progress, only remove the interface */
  686. sysfs_remove_group(&sys_dev->kobj, &mc_attr_group);
  687. break;
  688. }
  689. return NOTIFY_OK;
  690. }
  691. static struct notifier_block __refdata mc_cpu_notifier = {
  692. .notifier_call = mc_cpu_callback,
  693. };
  694. static int __init microcode_init (void)
  695. {
  696. int error;
  697. error = microcode_dev_init();
  698. if (error)
  699. return error;
  700. microcode_pdev = platform_device_register_simple("microcode", -1,
  701. NULL, 0);
  702. if (IS_ERR(microcode_pdev)) {
  703. microcode_dev_exit();
  704. return PTR_ERR(microcode_pdev);
  705. }
  706. get_online_cpus();
  707. error = sysdev_driver_register(&cpu_sysdev_class, &mc_sysdev_driver);
  708. put_online_cpus();
  709. if (error) {
  710. microcode_dev_exit();
  711. platform_device_unregister(microcode_pdev);
  712. return error;
  713. }
  714. register_hotcpu_notifier(&mc_cpu_notifier);
  715. printk(KERN_INFO
  716. "IA-32 Microcode Update Driver: v" MICROCODE_VERSION " <tigran@aivazian.fsnet.co.uk>\n");
  717. return 0;
  718. }
  719. static void __exit microcode_exit (void)
  720. {
  721. microcode_dev_exit();
  722. unregister_hotcpu_notifier(&mc_cpu_notifier);
  723. get_online_cpus();
  724. sysdev_driver_unregister(&cpu_sysdev_class, &mc_sysdev_driver);
  725. put_online_cpus();
  726. platform_device_unregister(microcode_pdev);
  727. }
  728. module_init(microcode_init)
  729. module_exit(microcode_exit)