microcode.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. /*
  2. * Intel CPU Microcode Update Driver for Linux
  3. *
  4. * Copyright (C) 2000-2006 Tigran Aivazian <tigran@aivazian.fsnet.co.uk>
  5. * 2006 Shaohua Li <shaohua.li@intel.com>
  6. *
  7. * This driver allows to upgrade microcode on Intel processors
  8. * belonging to IA-32 family - PentiumPro, Pentium II,
  9. * Pentium III, Xeon, Pentium 4, etc.
  10. *
  11. * Reference: Section 8.11 of Volume 3a, IA-32 Intel? Architecture
  12. * Software Developer's Manual
  13. * Order Number 253668 or free download from:
  14. *
  15. * http://developer.intel.com/design/pentium4/manuals/253668.htm
  16. *
  17. * For more information, go to http://www.urbanmyth.org/microcode
  18. *
  19. * This program is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU General Public License
  21. * as published by the Free Software Foundation; either version
  22. * 2 of the License, or (at your option) any later version.
  23. *
  24. * 1.0 16 Feb 2000, Tigran Aivazian <tigran@sco.com>
  25. * Initial release.
  26. * 1.01 18 Feb 2000, Tigran Aivazian <tigran@sco.com>
  27. * Added read() support + cleanups.
  28. * 1.02 21 Feb 2000, Tigran Aivazian <tigran@sco.com>
  29. * Added 'device trimming' support. open(O_WRONLY) zeroes
  30. * and frees the saved copy of applied microcode.
  31. * 1.03 29 Feb 2000, Tigran Aivazian <tigran@sco.com>
  32. * Made to use devfs (/dev/cpu/microcode) + cleanups.
  33. * 1.04 06 Jun 2000, Simon Trimmer <simon@veritas.com>
  34. * Added misc device support (now uses both devfs and misc).
  35. * Added MICROCODE_IOCFREE ioctl to clear memory.
  36. * 1.05 09 Jun 2000, Simon Trimmer <simon@veritas.com>
  37. * Messages for error cases (non Intel & no suitable microcode).
  38. * 1.06 03 Aug 2000, Tigran Aivazian <tigran@veritas.com>
  39. * Removed ->release(). Removed exclusive open and status bitmap.
  40. * Added microcode_rwsem to serialize read()/write()/ioctl().
  41. * Removed global kernel lock usage.
  42. * 1.07 07 Sep 2000, Tigran Aivazian <tigran@veritas.com>
  43. * Write 0 to 0x8B msr and then cpuid before reading revision,
  44. * so that it works even if there were no update done by the
  45. * BIOS. Otherwise, reading from 0x8B gives junk (which happened
  46. * to be 0 on my machine which is why it worked even when I
  47. * disabled update by the BIOS)
  48. * Thanks to Eric W. Biederman <ebiederman@lnxi.com> for the fix.
  49. * 1.08 11 Dec 2000, Richard Schaal <richard.schaal@intel.com> and
  50. * Tigran Aivazian <tigran@veritas.com>
  51. * Intel Pentium 4 processor support and bugfixes.
  52. * 1.09 30 Oct 2001, Tigran Aivazian <tigran@veritas.com>
  53. * Bugfix for HT (Hyper-Threading) enabled processors
  54. * whereby processor resources are shared by all logical processors
  55. * in a single CPU package.
  56. * 1.10 28 Feb 2002 Asit K Mallick <asit.k.mallick@intel.com> and
  57. * Tigran Aivazian <tigran@veritas.com>,
  58. * Serialize updates as required on HT processors due to speculative
  59. * nature of implementation.
  60. * 1.11 22 Mar 2002 Tigran Aivazian <tigran@veritas.com>
  61. * Fix the panic when writing zero-length microcode chunk.
  62. * 1.12 29 Sep 2003 Nitin Kamble <nitin.a.kamble@intel.com>,
  63. * Jun Nakajima <jun.nakajima@intel.com>
  64. * Support for the microcode updates in the new format.
  65. * 1.13 10 Oct 2003 Tigran Aivazian <tigran@veritas.com>
  66. * Removed ->read() method and obsoleted MICROCODE_IOCFREE ioctl
  67. * because we no longer hold a copy of applied microcode
  68. * in kernel memory.
  69. * 1.14 25 Jun 2004 Tigran Aivazian <tigran@veritas.com>
  70. * Fix sigmatch() macro to handle old CPUs with pf == 0.
  71. * Thanks to Stuart Swales for pointing out this bug.
  72. */
  73. //#define DEBUG /* pr_debug */
  74. #include <linux/capability.h>
  75. #include <linux/kernel.h>
  76. #include <linux/init.h>
  77. #include <linux/sched.h>
  78. #include <linux/smp_lock.h>
  79. #include <linux/cpumask.h>
  80. #include <linux/module.h>
  81. #include <linux/slab.h>
  82. #include <linux/vmalloc.h>
  83. #include <linux/miscdevice.h>
  84. #include <linux/spinlock.h>
  85. #include <linux/mm.h>
  86. #include <linux/fs.h>
  87. #include <linux/mutex.h>
  88. #include <linux/cpu.h>
  89. #include <linux/firmware.h>
  90. #include <linux/platform_device.h>
  91. #include <asm/msr.h>
  92. #include <asm/uaccess.h>
  93. #include <asm/processor.h>
  94. MODULE_DESCRIPTION("Intel CPU (IA-32) Microcode Update Driver");
  95. MODULE_AUTHOR("Tigran Aivazian <tigran@aivazian.fsnet.co.uk>");
  96. MODULE_LICENSE("GPL");
  97. #define MICROCODE_VERSION "1.14a"
  98. #define DEFAULT_UCODE_DATASIZE (2000) /* 2000 bytes */
  99. #define MC_HEADER_SIZE (sizeof (microcode_header_t)) /* 48 bytes */
  100. #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE) /* 2048 bytes */
  101. #define EXT_HEADER_SIZE (sizeof (struct extended_sigtable)) /* 20 bytes */
  102. #define EXT_SIGNATURE_SIZE (sizeof (struct extended_signature)) /* 12 bytes */
  103. #define DWSIZE (sizeof (u32))
  104. #define get_totalsize(mc) \
  105. (((microcode_t *)mc)->hdr.totalsize ? \
  106. ((microcode_t *)mc)->hdr.totalsize : DEFAULT_UCODE_TOTALSIZE)
  107. #define get_datasize(mc) \
  108. (((microcode_t *)mc)->hdr.datasize ? \
  109. ((microcode_t *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE)
  110. #define sigmatch(s1, s2, p1, p2) \
  111. (((s1) == (s2)) && (((p1) & (p2)) || (((p1) == 0) && ((p2) == 0))))
  112. #define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE)
  113. /* serialize access to the physical write to MSR 0x79 */
  114. static DEFINE_SPINLOCK(microcode_update_lock);
  115. /* no concurrent ->write()s are allowed on /dev/cpu/microcode */
  116. static DEFINE_MUTEX(microcode_mutex);
  117. static struct ucode_cpu_info {
  118. int valid;
  119. unsigned int sig;
  120. unsigned int pf;
  121. unsigned int rev;
  122. microcode_t *mc;
  123. } ucode_cpu_info[NR_CPUS];
  124. static void collect_cpu_info(int cpu_num)
  125. {
  126. struct cpuinfo_x86 *c = &cpu_data(cpu_num);
  127. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  128. unsigned int val[2];
  129. /* We should bind the task to the CPU */
  130. BUG_ON(raw_smp_processor_id() != cpu_num);
  131. uci->pf = uci->rev = 0;
  132. uci->mc = NULL;
  133. uci->valid = 1;
  134. if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
  135. cpu_has(c, X86_FEATURE_IA64)) {
  136. printk(KERN_ERR "microcode: CPU%d not a capable Intel "
  137. "processor\n", cpu_num);
  138. uci->valid = 0;
  139. return;
  140. }
  141. uci->sig = cpuid_eax(0x00000001);
  142. if ((c->x86_model >= 5) || (c->x86 > 6)) {
  143. /* get processor flags from MSR 0x17 */
  144. rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  145. uci->pf = 1 << ((val[1] >> 18) & 7);
  146. }
  147. wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  148. /* see notes above for revision 1.07. Apparent chip bug */
  149. sync_core();
  150. /* get the current revision from MSR 0x8B */
  151. rdmsr(MSR_IA32_UCODE_REV, val[0], uci->rev);
  152. pr_debug("microcode: collect_cpu_info : sig=0x%x, pf=0x%x, rev=0x%x\n",
  153. uci->sig, uci->pf, uci->rev);
  154. }
  155. static inline int microcode_update_match(int cpu_num,
  156. microcode_header_t *mc_header, int sig, int pf)
  157. {
  158. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  159. if (!sigmatch(sig, uci->sig, pf, uci->pf)
  160. || mc_header->rev <= uci->rev)
  161. return 0;
  162. return 1;
  163. }
  164. static int microcode_sanity_check(void *mc)
  165. {
  166. microcode_header_t *mc_header = mc;
  167. struct extended_sigtable *ext_header = NULL;
  168. struct extended_signature *ext_sig;
  169. unsigned long total_size, data_size, ext_table_size;
  170. int sum, orig_sum, ext_sigcount = 0, i;
  171. total_size = get_totalsize(mc_header);
  172. data_size = get_datasize(mc_header);
  173. if (data_size + MC_HEADER_SIZE > total_size) {
  174. printk(KERN_ERR "microcode: error! "
  175. "Bad data size in microcode data file\n");
  176. return -EINVAL;
  177. }
  178. if (mc_header->ldrver != 1 || mc_header->hdrver != 1) {
  179. printk(KERN_ERR "microcode: error! "
  180. "Unknown microcode update format\n");
  181. return -EINVAL;
  182. }
  183. ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
  184. if (ext_table_size) {
  185. if ((ext_table_size < EXT_HEADER_SIZE)
  186. || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
  187. printk(KERN_ERR "microcode: error! "
  188. "Small exttable size in microcode data file\n");
  189. return -EINVAL;
  190. }
  191. ext_header = mc + MC_HEADER_SIZE + data_size;
  192. if (ext_table_size != exttable_size(ext_header)) {
  193. printk(KERN_ERR "microcode: error! "
  194. "Bad exttable size in microcode data file\n");
  195. return -EFAULT;
  196. }
  197. ext_sigcount = ext_header->count;
  198. }
  199. /* check extended table checksum */
  200. if (ext_table_size) {
  201. int ext_table_sum = 0;
  202. int *ext_tablep = (int *)ext_header;
  203. i = ext_table_size / DWSIZE;
  204. while (i--)
  205. ext_table_sum += ext_tablep[i];
  206. if (ext_table_sum) {
  207. printk(KERN_WARNING "microcode: aborting, "
  208. "bad extended signature table checksum\n");
  209. return -EINVAL;
  210. }
  211. }
  212. /* calculate the checksum */
  213. orig_sum = 0;
  214. i = (MC_HEADER_SIZE + data_size) / DWSIZE;
  215. while (i--)
  216. orig_sum += ((int *)mc)[i];
  217. if (orig_sum) {
  218. printk(KERN_ERR "microcode: aborting, bad checksum\n");
  219. return -EINVAL;
  220. }
  221. if (!ext_table_size)
  222. return 0;
  223. /* check extended signature checksum */
  224. for (i = 0; i < ext_sigcount; i++) {
  225. ext_sig = (void *)ext_header + EXT_HEADER_SIZE +
  226. EXT_SIGNATURE_SIZE * i;
  227. sum = orig_sum
  228. - (mc_header->sig + mc_header->pf + mc_header->cksum)
  229. + (ext_sig->sig + ext_sig->pf + ext_sig->cksum);
  230. if (sum) {
  231. printk(KERN_ERR "microcode: aborting, bad checksum\n");
  232. return -EINVAL;
  233. }
  234. }
  235. return 0;
  236. }
  237. /*
  238. * return 0 - no update found
  239. * return 1 - found update
  240. * return < 0 - error
  241. */
  242. static int get_maching_microcode(void *mc, int cpu)
  243. {
  244. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  245. microcode_header_t *mc_header = mc;
  246. struct extended_sigtable *ext_header;
  247. unsigned long total_size = get_totalsize(mc_header);
  248. int ext_sigcount, i;
  249. struct extended_signature *ext_sig;
  250. void *new_mc;
  251. if (microcode_update_match(cpu, mc_header,
  252. mc_header->sig, mc_header->pf))
  253. goto find;
  254. if (total_size <= get_datasize(mc_header) + MC_HEADER_SIZE)
  255. return 0;
  256. ext_header = mc + get_datasize(mc_header) + MC_HEADER_SIZE;
  257. ext_sigcount = ext_header->count;
  258. ext_sig = (void *)ext_header + 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 update from revision "
  304. "0x%x to 0x%x failed\n", cpu_num, uci->rev, val[1]);
  305. return;
  306. }
  307. printk(KERN_INFO "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_ptr(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_ptr(current, &old);
  377. return error;
  378. }
  379. static int microcode_open (struct inode *unused1, struct file *unused2)
  380. {
  381. cycle_kernel_lock();
  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 >> PAGE_SHIFT) > num_physpages) {
  388. printk(KERN_ERR "microcode: too much data (max %ld pages)\n", num_physpages);
  389. return -EINVAL;
  390. }
  391. get_online_cpus();
  392. mutex_lock(&microcode_mutex);
  393. user_buffer = (void __user *) buf;
  394. user_buffer_size = (int) len;
  395. ret = do_microcode_update();
  396. if (!ret)
  397. ret = (ssize_t)len;
  398. mutex_unlock(&microcode_mutex);
  399. put_online_cpus();
  400. return ret;
  401. }
  402. static const struct file_operations microcode_fops = {
  403. .owner = THIS_MODULE,
  404. .write = microcode_write,
  405. .open = microcode_open,
  406. };
  407. static struct miscdevice microcode_dev = {
  408. .minor = MICROCODE_MINOR,
  409. .name = "microcode",
  410. .fops = &microcode_fops,
  411. };
  412. static int __init microcode_dev_init (void)
  413. {
  414. int error;
  415. error = misc_register(&microcode_dev);
  416. if (error) {
  417. printk(KERN_ERR
  418. "microcode: can't misc_register on minor=%d\n",
  419. MICROCODE_MINOR);
  420. return error;
  421. }
  422. return 0;
  423. }
  424. static void microcode_dev_exit (void)
  425. {
  426. misc_deregister(&microcode_dev);
  427. }
  428. MODULE_ALIAS_MISCDEV(MICROCODE_MINOR);
  429. #else
  430. #define microcode_dev_init() 0
  431. #define microcode_dev_exit() do { } while(0)
  432. #endif
  433. static long get_next_ucode_from_buffer(void **mc, const u8 *buf,
  434. unsigned long size, long offset)
  435. {
  436. microcode_header_t *mc_header;
  437. unsigned long total_size;
  438. /* No more data */
  439. if (offset >= size)
  440. return 0;
  441. mc_header = (microcode_header_t *)(buf + offset);
  442. total_size = get_totalsize(mc_header);
  443. if (offset + total_size > size) {
  444. printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
  445. return -EINVAL;
  446. }
  447. *mc = vmalloc(total_size);
  448. if (!*mc) {
  449. printk(KERN_ERR "microcode: error! Can not allocate memory\n");
  450. return -ENOMEM;
  451. }
  452. memcpy(*mc, buf + offset, total_size);
  453. return offset + total_size;
  454. }
  455. /* fake device for request_firmware */
  456. static struct platform_device *microcode_pdev;
  457. static int cpu_request_microcode(int cpu)
  458. {
  459. char name[30];
  460. struct cpuinfo_x86 *c = &cpu_data(cpu);
  461. const struct firmware *firmware;
  462. const u8 *buf;
  463. unsigned long size;
  464. long offset = 0;
  465. int error;
  466. void *mc;
  467. /* We should bind the task to the CPU */
  468. BUG_ON(cpu != raw_smp_processor_id());
  469. sprintf(name,"intel-ucode/%02x-%02x-%02x",
  470. c->x86, c->x86_model, c->x86_mask);
  471. error = request_firmware(&firmware, name, &microcode_pdev->dev);
  472. if (error) {
  473. pr_debug("microcode: data file %s load failed\n", name);
  474. return error;
  475. }
  476. buf = firmware->data;
  477. size = firmware->size;
  478. while ((offset = get_next_ucode_from_buffer(&mc, buf, size, offset))
  479. > 0) {
  480. error = microcode_sanity_check(mc);
  481. if (error)
  482. break;
  483. error = get_maching_microcode(mc, cpu);
  484. if (error < 0)
  485. break;
  486. /*
  487. * It's possible the data file has multiple matching ucode,
  488. * lets keep searching till the latest version
  489. */
  490. if (error == 1) {
  491. apply_microcode(cpu);
  492. error = 0;
  493. }
  494. vfree(mc);
  495. }
  496. if (offset > 0)
  497. vfree(mc);
  498. if (offset < 0)
  499. error = offset;
  500. release_firmware(firmware);
  501. return error;
  502. }
  503. static int apply_microcode_check_cpu(int cpu)
  504. {
  505. struct cpuinfo_x86 *c = &cpu_data(cpu);
  506. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  507. cpumask_t old;
  508. unsigned int val[2];
  509. int err = 0;
  510. /* Check if the microcode is available */
  511. if (!uci->mc)
  512. return 0;
  513. old = current->cpus_allowed;
  514. set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
  515. /* Check if the microcode we have in memory matches the CPU */
  516. if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
  517. cpu_has(c, X86_FEATURE_IA64) || uci->sig != cpuid_eax(0x00000001))
  518. err = -EINVAL;
  519. if (!err && ((c->x86_model >= 5) || (c->x86 > 6))) {
  520. /* get processor flags from MSR 0x17 */
  521. rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  522. if (uci->pf != (1 << ((val[1] >> 18) & 7)))
  523. err = -EINVAL;
  524. }
  525. if (!err) {
  526. wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  527. /* see notes above for revision 1.07. Apparent chip bug */
  528. sync_core();
  529. /* get the current revision from MSR 0x8B */
  530. rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  531. if (uci->rev != val[1])
  532. err = -EINVAL;
  533. }
  534. if (!err)
  535. apply_microcode(cpu);
  536. else
  537. printk(KERN_ERR "microcode: Could not apply microcode to CPU%d:"
  538. " sig=0x%x, pf=0x%x, rev=0x%x\n",
  539. cpu, uci->sig, uci->pf, uci->rev);
  540. set_cpus_allowed_ptr(current, &old);
  541. return err;
  542. }
  543. static void microcode_init_cpu(int cpu, int resume)
  544. {
  545. cpumask_t old;
  546. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  547. old = current->cpus_allowed;
  548. set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
  549. mutex_lock(&microcode_mutex);
  550. collect_cpu_info(cpu);
  551. if (uci->valid && system_state == SYSTEM_RUNNING && !resume)
  552. cpu_request_microcode(cpu);
  553. mutex_unlock(&microcode_mutex);
  554. set_cpus_allowed_ptr(current, &old);
  555. }
  556. static void microcode_fini_cpu(int cpu)
  557. {
  558. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  559. mutex_lock(&microcode_mutex);
  560. uci->valid = 0;
  561. vfree(uci->mc);
  562. uci->mc = NULL;
  563. mutex_unlock(&microcode_mutex);
  564. }
  565. static ssize_t reload_store(struct sys_device *dev,
  566. struct sysdev_attribute *attr,
  567. const char *buf, size_t sz)
  568. {
  569. struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
  570. char *end;
  571. unsigned long val = simple_strtoul(buf, &end, 0);
  572. int err = 0;
  573. int cpu = dev->id;
  574. if (end == buf)
  575. return -EINVAL;
  576. if (val == 1) {
  577. cpumask_t old;
  578. old = current->cpus_allowed;
  579. get_online_cpus();
  580. set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
  581. mutex_lock(&microcode_mutex);
  582. if (uci->valid)
  583. err = cpu_request_microcode(cpu);
  584. mutex_unlock(&microcode_mutex);
  585. put_online_cpus();
  586. set_cpus_allowed_ptr(current, &old);
  587. }
  588. if (err)
  589. return err;
  590. return sz;
  591. }
  592. static ssize_t version_show(struct sys_device *dev,
  593. struct sysdev_attribute *attr, char *buf)
  594. {
  595. struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
  596. return sprintf(buf, "0x%x\n", uci->rev);
  597. }
  598. static ssize_t pf_show(struct sys_device *dev,
  599. struct sysdev_attribute *attr, char *buf)
  600. {
  601. struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
  602. return sprintf(buf, "0x%x\n", uci->pf);
  603. }
  604. static SYSDEV_ATTR(reload, 0200, NULL, reload_store);
  605. static SYSDEV_ATTR(version, 0400, version_show, NULL);
  606. static SYSDEV_ATTR(processor_flags, 0400, pf_show, NULL);
  607. static struct attribute *mc_default_attrs[] = {
  608. &attr_reload.attr,
  609. &attr_version.attr,
  610. &attr_processor_flags.attr,
  611. NULL
  612. };
  613. static struct attribute_group mc_attr_group = {
  614. .attrs = mc_default_attrs,
  615. .name = "microcode",
  616. };
  617. static int __mc_sysdev_add(struct sys_device *sys_dev, int resume)
  618. {
  619. int err, cpu = sys_dev->id;
  620. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  621. if (!cpu_online(cpu))
  622. return 0;
  623. pr_debug("microcode: CPU%d added\n", cpu);
  624. memset(uci, 0, sizeof(*uci));
  625. err = sysfs_create_group(&sys_dev->kobj, &mc_attr_group);
  626. if (err)
  627. return err;
  628. microcode_init_cpu(cpu, resume);
  629. return 0;
  630. }
  631. static int mc_sysdev_add(struct sys_device *sys_dev)
  632. {
  633. return __mc_sysdev_add(sys_dev, 0);
  634. }
  635. static int mc_sysdev_remove(struct sys_device *sys_dev)
  636. {
  637. int cpu = sys_dev->id;
  638. if (!cpu_online(cpu))
  639. return 0;
  640. pr_debug("microcode: CPU%d removed\n", cpu);
  641. microcode_fini_cpu(cpu);
  642. sysfs_remove_group(&sys_dev->kobj, &mc_attr_group);
  643. return 0;
  644. }
  645. static int mc_sysdev_resume(struct sys_device *dev)
  646. {
  647. int cpu = dev->id;
  648. if (!cpu_online(cpu))
  649. return 0;
  650. pr_debug("microcode: CPU%d resumed\n", cpu);
  651. /* only CPU 0 will apply ucode here */
  652. apply_microcode(0);
  653. return 0;
  654. }
  655. static struct sysdev_driver mc_sysdev_driver = {
  656. .add = mc_sysdev_add,
  657. .remove = mc_sysdev_remove,
  658. .resume = mc_sysdev_resume,
  659. };
  660. static __cpuinit int
  661. mc_cpu_callback(struct notifier_block *nb, unsigned long action, void *hcpu)
  662. {
  663. unsigned int cpu = (unsigned long)hcpu;
  664. struct sys_device *sys_dev;
  665. sys_dev = get_cpu_sysdev(cpu);
  666. switch (action) {
  667. case CPU_UP_CANCELED_FROZEN:
  668. /* The CPU refused to come up during a system resume */
  669. microcode_fini_cpu(cpu);
  670. break;
  671. case CPU_ONLINE:
  672. case CPU_DOWN_FAILED:
  673. mc_sysdev_add(sys_dev);
  674. break;
  675. case CPU_ONLINE_FROZEN:
  676. /* System-wide resume is in progress, try to apply microcode */
  677. if (apply_microcode_check_cpu(cpu)) {
  678. /* The application of microcode failed */
  679. microcode_fini_cpu(cpu);
  680. __mc_sysdev_add(sys_dev, 1);
  681. break;
  682. }
  683. case CPU_DOWN_FAILED_FROZEN:
  684. if (sysfs_create_group(&sys_dev->kobj, &mc_attr_group))
  685. printk(KERN_ERR "microcode: Failed to create the sysfs "
  686. "group for CPU%d\n", cpu);
  687. break;
  688. case CPU_DOWN_PREPARE:
  689. mc_sysdev_remove(sys_dev);
  690. break;
  691. case CPU_DOWN_PREPARE_FROZEN:
  692. /* Suspend is in progress, only remove the interface */
  693. sysfs_remove_group(&sys_dev->kobj, &mc_attr_group);
  694. break;
  695. }
  696. return NOTIFY_OK;
  697. }
  698. static struct notifier_block __refdata mc_cpu_notifier = {
  699. .notifier_call = mc_cpu_callback,
  700. };
  701. static int __init microcode_init (void)
  702. {
  703. int error;
  704. printk(KERN_INFO
  705. "IA-32 Microcode Update Driver: v" MICROCODE_VERSION " <tigran@aivazian.fsnet.co.uk>\n");
  706. error = microcode_dev_init();
  707. if (error)
  708. return error;
  709. microcode_pdev = platform_device_register_simple("microcode", -1,
  710. NULL, 0);
  711. if (IS_ERR(microcode_pdev)) {
  712. microcode_dev_exit();
  713. return PTR_ERR(microcode_pdev);
  714. }
  715. get_online_cpus();
  716. error = sysdev_driver_register(&cpu_sysdev_class, &mc_sysdev_driver);
  717. put_online_cpus();
  718. if (error) {
  719. microcode_dev_exit();
  720. platform_device_unregister(microcode_pdev);
  721. return error;
  722. }
  723. register_hotcpu_notifier(&mc_cpu_notifier);
  724. return 0;
  725. }
  726. static void __exit microcode_exit (void)
  727. {
  728. microcode_dev_exit();
  729. unregister_hotcpu_notifier(&mc_cpu_notifier);
  730. get_online_cpus();
  731. sysdev_driver_unregister(&cpu_sysdev_class, &mc_sysdev_driver);
  732. put_online_cpus();
  733. platform_device_unregister(microcode_pdev);
  734. }
  735. module_init(microcode_init)
  736. module_exit(microcode_exit)