microcode.c 23 KB

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