microcode.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  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/cpumask.h>
  79. #include <linux/module.h>
  80. #include <linux/slab.h>
  81. #include <linux/vmalloc.h>
  82. #include <linux/miscdevice.h>
  83. #include <linux/spinlock.h>
  84. #include <linux/mm.h>
  85. #include <linux/fs.h>
  86. #include <linux/mutex.h>
  87. #include <linux/cpu.h>
  88. #include <linux/firmware.h>
  89. #include <linux/platform_device.h>
  90. #include <asm/msr.h>
  91. #include <asm/uaccess.h>
  92. #include <asm/processor.h>
  93. MODULE_DESCRIPTION("Intel CPU (IA-32) Microcode Update Driver");
  94. MODULE_AUTHOR("Tigran Aivazian <tigran@aivazian.fsnet.co.uk>");
  95. MODULE_LICENSE("GPL");
  96. #define MICROCODE_VERSION "1.14a"
  97. #define DEFAULT_UCODE_DATASIZE (2000) /* 2000 bytes */
  98. #define MC_HEADER_SIZE (sizeof (microcode_header_t)) /* 48 bytes */
  99. #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE) /* 2048 bytes */
  100. #define EXT_HEADER_SIZE (sizeof (struct extended_sigtable)) /* 20 bytes */
  101. #define EXT_SIGNATURE_SIZE (sizeof (struct extended_signature)) /* 12 bytes */
  102. #define DWSIZE (sizeof (u32))
  103. #define get_totalsize(mc) \
  104. (((microcode_t *)mc)->hdr.totalsize ? \
  105. ((microcode_t *)mc)->hdr.totalsize : DEFAULT_UCODE_TOTALSIZE)
  106. #define get_datasize(mc) \
  107. (((microcode_t *)mc)->hdr.datasize ? \
  108. ((microcode_t *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE)
  109. #define sigmatch(s1, s2, p1, p2) \
  110. (((s1) == (s2)) && (((p1) & (p2)) || (((p1) == 0) && ((p2) == 0))))
  111. #define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE)
  112. /* serialize access to the physical write to MSR 0x79 */
  113. static DEFINE_SPINLOCK(microcode_update_lock);
  114. /* no concurrent ->write()s are allowed on /dev/cpu/microcode */
  115. static DEFINE_MUTEX(microcode_mutex);
  116. static struct ucode_cpu_info {
  117. int valid;
  118. unsigned int sig;
  119. unsigned int pf;
  120. unsigned int rev;
  121. microcode_t *mc;
  122. } ucode_cpu_info[NR_CPUS];
  123. static void collect_cpu_info(int cpu_num)
  124. {
  125. struct cpuinfo_x86 *c = &cpu_data(cpu_num);
  126. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  127. unsigned int val[2];
  128. /* We should bind the task to the CPU */
  129. BUG_ON(raw_smp_processor_id() != cpu_num);
  130. uci->pf = uci->rev = 0;
  131. uci->mc = NULL;
  132. uci->valid = 1;
  133. if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
  134. cpu_has(c, X86_FEATURE_IA64)) {
  135. printk(KERN_ERR "microcode: CPU%d not a capable Intel "
  136. "processor\n", cpu_num);
  137. uci->valid = 0;
  138. return;
  139. }
  140. uci->sig = cpuid_eax(0x00000001);
  141. if ((c->x86_model >= 5) || (c->x86 > 6)) {
  142. /* get processor flags from MSR 0x17 */
  143. rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  144. uci->pf = 1 << ((val[1] >> 18) & 7);
  145. }
  146. wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  147. /* see notes above for revision 1.07. Apparent chip bug */
  148. sync_core();
  149. /* get the current revision from MSR 0x8B */
  150. rdmsr(MSR_IA32_UCODE_REV, val[0], uci->rev);
  151. pr_debug("microcode: collect_cpu_info : sig=0x%x, pf=0x%x, rev=0x%x\n",
  152. uci->sig, uci->pf, uci->rev);
  153. }
  154. static inline int microcode_update_match(int cpu_num,
  155. microcode_header_t *mc_header, int sig, int pf)
  156. {
  157. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  158. if (!sigmatch(sig, uci->sig, pf, uci->pf)
  159. || mc_header->rev <= uci->rev)
  160. return 0;
  161. return 1;
  162. }
  163. static int microcode_sanity_check(void *mc)
  164. {
  165. microcode_header_t *mc_header = mc;
  166. struct extended_sigtable *ext_header = NULL;
  167. struct extended_signature *ext_sig;
  168. unsigned long total_size, data_size, ext_table_size;
  169. int sum, orig_sum, ext_sigcount = 0, i;
  170. total_size = get_totalsize(mc_header);
  171. data_size = get_datasize(mc_header);
  172. if (data_size + MC_HEADER_SIZE > total_size) {
  173. printk(KERN_ERR "microcode: error! "
  174. "Bad data size in microcode data file\n");
  175. return -EINVAL;
  176. }
  177. if (mc_header->ldrver != 1 || mc_header->hdrver != 1) {
  178. printk(KERN_ERR "microcode: error! "
  179. "Unknown microcode update format\n");
  180. return -EINVAL;
  181. }
  182. ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
  183. if (ext_table_size) {
  184. if ((ext_table_size < EXT_HEADER_SIZE)
  185. || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
  186. printk(KERN_ERR "microcode: error! "
  187. "Small exttable size in microcode data file\n");
  188. return -EINVAL;
  189. }
  190. ext_header = mc + MC_HEADER_SIZE + data_size;
  191. if (ext_table_size != exttable_size(ext_header)) {
  192. printk(KERN_ERR "microcode: error! "
  193. "Bad exttable size in microcode data file\n");
  194. return -EFAULT;
  195. }
  196. ext_sigcount = ext_header->count;
  197. }
  198. /* check extended table checksum */
  199. if (ext_table_size) {
  200. int ext_table_sum = 0;
  201. int *ext_tablep = (int *)ext_header;
  202. i = ext_table_size / DWSIZE;
  203. while (i--)
  204. ext_table_sum += ext_tablep[i];
  205. if (ext_table_sum) {
  206. printk(KERN_WARNING "microcode: aborting, "
  207. "bad extended signature table checksum\n");
  208. return -EINVAL;
  209. }
  210. }
  211. /* calculate the checksum */
  212. orig_sum = 0;
  213. i = (MC_HEADER_SIZE + data_size) / DWSIZE;
  214. while (i--)
  215. orig_sum += ((int *)mc)[i];
  216. if (orig_sum) {
  217. printk(KERN_ERR "microcode: aborting, bad checksum\n");
  218. return -EINVAL;
  219. }
  220. if (!ext_table_size)
  221. return 0;
  222. /* check extended signature checksum */
  223. for (i = 0; i < ext_sigcount; i++) {
  224. ext_sig = (void *)ext_header + EXT_HEADER_SIZE +
  225. EXT_SIGNATURE_SIZE * i;
  226. sum = orig_sum
  227. - (mc_header->sig + mc_header->pf + mc_header->cksum)
  228. + (ext_sig->sig + ext_sig->pf + ext_sig->cksum);
  229. if (sum) {
  230. printk(KERN_ERR "microcode: aborting, bad checksum\n");
  231. return -EINVAL;
  232. }
  233. }
  234. return 0;
  235. }
  236. /*
  237. * return 0 - no update found
  238. * return 1 - found update
  239. * return < 0 - error
  240. */
  241. static int get_maching_microcode(void *mc, int cpu)
  242. {
  243. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  244. microcode_header_t *mc_header = mc;
  245. struct extended_sigtable *ext_header;
  246. unsigned long total_size = get_totalsize(mc_header);
  247. int ext_sigcount, i;
  248. struct extended_signature *ext_sig;
  249. void *new_mc;
  250. if (microcode_update_match(cpu, mc_header,
  251. mc_header->sig, mc_header->pf))
  252. goto find;
  253. if (total_size <= get_datasize(mc_header) + MC_HEADER_SIZE)
  254. return 0;
  255. ext_header = mc + get_datasize(mc_header) + MC_HEADER_SIZE;
  256. ext_sigcount = ext_header->count;
  257. ext_sig = (void *)ext_header + 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 update from revision "
  303. "0x%x to 0x%x failed\n", cpu_num, uci->rev, val[1]);
  304. return;
  305. }
  306. printk(KERN_INFO "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 = NULL;
  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_ptr(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_ptr(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. get_online_cpus();
  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. put_online_cpus();
  398. return ret;
  399. }
  400. static const 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 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("microcode: data file %s load failed\n", name);
  472. return error;
  473. }
  474. buf = 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 int apply_microcode_check_cpu(int cpu)
  502. {
  503. struct cpuinfo_x86 *c = &cpu_data(cpu);
  504. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  505. cpumask_t old;
  506. unsigned int val[2];
  507. int err = 0;
  508. /* Check if the microcode is available */
  509. if (!uci->mc)
  510. return 0;
  511. old = current->cpus_allowed;
  512. set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
  513. /* Check if the microcode we have in memory matches the CPU */
  514. if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
  515. cpu_has(c, X86_FEATURE_IA64) || uci->sig != cpuid_eax(0x00000001))
  516. err = -EINVAL;
  517. if (!err && ((c->x86_model >= 5) || (c->x86 > 6))) {
  518. /* get processor flags from MSR 0x17 */
  519. rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  520. if (uci->pf != (1 << ((val[1] >> 18) & 7)))
  521. err = -EINVAL;
  522. }
  523. if (!err) {
  524. wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  525. /* see notes above for revision 1.07. Apparent chip bug */
  526. sync_core();
  527. /* get the current revision from MSR 0x8B */
  528. rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  529. if (uci->rev != val[1])
  530. err = -EINVAL;
  531. }
  532. if (!err)
  533. apply_microcode(cpu);
  534. else
  535. printk(KERN_ERR "microcode: Could not apply microcode to CPU%d:"
  536. " sig=0x%x, pf=0x%x, rev=0x%x\n",
  537. cpu, uci->sig, uci->pf, uci->rev);
  538. set_cpus_allowed_ptr(current, &old);
  539. return err;
  540. }
  541. static void microcode_init_cpu(int cpu, int resume)
  542. {
  543. cpumask_t old;
  544. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  545. old = current->cpus_allowed;
  546. set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
  547. mutex_lock(&microcode_mutex);
  548. collect_cpu_info(cpu);
  549. if (uci->valid && system_state == SYSTEM_RUNNING && !resume)
  550. cpu_request_microcode(cpu);
  551. mutex_unlock(&microcode_mutex);
  552. set_cpus_allowed_ptr(current, &old);
  553. }
  554. static void microcode_fini_cpu(int cpu)
  555. {
  556. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  557. mutex_lock(&microcode_mutex);
  558. uci->valid = 0;
  559. vfree(uci->mc);
  560. uci->mc = NULL;
  561. mutex_unlock(&microcode_mutex);
  562. }
  563. static ssize_t reload_store(struct sys_device *dev, const char *buf, size_t sz)
  564. {
  565. struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
  566. char *end;
  567. unsigned long val = simple_strtoul(buf, &end, 0);
  568. int err = 0;
  569. int cpu = dev->id;
  570. if (end == buf)
  571. return -EINVAL;
  572. if (val == 1) {
  573. cpumask_t old;
  574. old = current->cpus_allowed;
  575. get_online_cpus();
  576. set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
  577. mutex_lock(&microcode_mutex);
  578. if (uci->valid)
  579. err = cpu_request_microcode(cpu);
  580. mutex_unlock(&microcode_mutex);
  581. put_online_cpus();
  582. set_cpus_allowed_ptr(current, &old);
  583. }
  584. if (err)
  585. return err;
  586. return sz;
  587. }
  588. static ssize_t version_show(struct sys_device *dev, char *buf)
  589. {
  590. struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
  591. return sprintf(buf, "0x%x\n", uci->rev);
  592. }
  593. static ssize_t pf_show(struct sys_device *dev, char *buf)
  594. {
  595. struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
  596. return sprintf(buf, "0x%x\n", uci->pf);
  597. }
  598. static SYSDEV_ATTR(reload, 0200, NULL, reload_store);
  599. static SYSDEV_ATTR(version, 0400, version_show, NULL);
  600. static SYSDEV_ATTR(processor_flags, 0400, pf_show, NULL);
  601. static struct attribute *mc_default_attrs[] = {
  602. &attr_reload.attr,
  603. &attr_version.attr,
  604. &attr_processor_flags.attr,
  605. NULL
  606. };
  607. static struct attribute_group mc_attr_group = {
  608. .attrs = mc_default_attrs,
  609. .name = "microcode",
  610. };
  611. static int __mc_sysdev_add(struct sys_device *sys_dev, int resume)
  612. {
  613. int err, cpu = sys_dev->id;
  614. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  615. if (!cpu_online(cpu))
  616. return 0;
  617. pr_debug("microcode: CPU%d added\n", cpu);
  618. memset(uci, 0, sizeof(*uci));
  619. err = sysfs_create_group(&sys_dev->kobj, &mc_attr_group);
  620. if (err)
  621. return err;
  622. microcode_init_cpu(cpu, resume);
  623. return 0;
  624. }
  625. static int mc_sysdev_add(struct sys_device *sys_dev)
  626. {
  627. return __mc_sysdev_add(sys_dev, 0);
  628. }
  629. static int mc_sysdev_remove(struct sys_device *sys_dev)
  630. {
  631. int cpu = sys_dev->id;
  632. if (!cpu_online(cpu))
  633. return 0;
  634. pr_debug("microcode: CPU%d removed\n", cpu);
  635. microcode_fini_cpu(cpu);
  636. sysfs_remove_group(&sys_dev->kobj, &mc_attr_group);
  637. return 0;
  638. }
  639. static int mc_sysdev_resume(struct sys_device *dev)
  640. {
  641. int cpu = dev->id;
  642. if (!cpu_online(cpu))
  643. return 0;
  644. pr_debug("microcode: CPU%d resumed\n", cpu);
  645. /* only CPU 0 will apply ucode here */
  646. apply_microcode(0);
  647. return 0;
  648. }
  649. static struct sysdev_driver mc_sysdev_driver = {
  650. .add = mc_sysdev_add,
  651. .remove = mc_sysdev_remove,
  652. .resume = mc_sysdev_resume,
  653. };
  654. static __cpuinit int
  655. mc_cpu_callback(struct notifier_block *nb, unsigned long action, void *hcpu)
  656. {
  657. unsigned int cpu = (unsigned long)hcpu;
  658. struct sys_device *sys_dev;
  659. sys_dev = get_cpu_sysdev(cpu);
  660. switch (action) {
  661. case CPU_UP_CANCELED_FROZEN:
  662. /* The CPU refused to come up during a system resume */
  663. microcode_fini_cpu(cpu);
  664. break;
  665. case CPU_ONLINE:
  666. case CPU_DOWN_FAILED:
  667. mc_sysdev_add(sys_dev);
  668. break;
  669. case CPU_ONLINE_FROZEN:
  670. /* System-wide resume is in progress, try to apply microcode */
  671. if (apply_microcode_check_cpu(cpu)) {
  672. /* The application of microcode failed */
  673. microcode_fini_cpu(cpu);
  674. __mc_sysdev_add(sys_dev, 1);
  675. break;
  676. }
  677. case CPU_DOWN_FAILED_FROZEN:
  678. if (sysfs_create_group(&sys_dev->kobj, &mc_attr_group))
  679. printk(KERN_ERR "microcode: Failed to create the sysfs "
  680. "group for CPU%d\n", cpu);
  681. break;
  682. case CPU_DOWN_PREPARE:
  683. mc_sysdev_remove(sys_dev);
  684. break;
  685. case CPU_DOWN_PREPARE_FROZEN:
  686. /* Suspend is in progress, only remove the interface */
  687. sysfs_remove_group(&sys_dev->kobj, &mc_attr_group);
  688. break;
  689. }
  690. return NOTIFY_OK;
  691. }
  692. static struct notifier_block __refdata mc_cpu_notifier = {
  693. .notifier_call = mc_cpu_callback,
  694. };
  695. static int __init microcode_init (void)
  696. {
  697. int error;
  698. printk(KERN_INFO
  699. "IA-32 Microcode Update Driver: v" MICROCODE_VERSION " <tigran@aivazian.fsnet.co.uk>\n");
  700. error = microcode_dev_init();
  701. if (error)
  702. return error;
  703. microcode_pdev = platform_device_register_simple("microcode", -1,
  704. NULL, 0);
  705. if (IS_ERR(microcode_pdev)) {
  706. microcode_dev_exit();
  707. return PTR_ERR(microcode_pdev);
  708. }
  709. get_online_cpus();
  710. error = sysdev_driver_register(&cpu_sysdev_class, &mc_sysdev_driver);
  711. put_online_cpus();
  712. if (error) {
  713. microcode_dev_exit();
  714. platform_device_unregister(microcode_pdev);
  715. return error;
  716. }
  717. register_hotcpu_notifier(&mc_cpu_notifier);
  718. return 0;
  719. }
  720. static void __exit microcode_exit (void)
  721. {
  722. microcode_dev_exit();
  723. unregister_hotcpu_notifier(&mc_cpu_notifier);
  724. get_online_cpus();
  725. sysdev_driver_unregister(&cpu_sysdev_class, &mc_sysdev_driver);
  726. put_online_cpus();
  727. platform_device_unregister(microcode_pdev);
  728. }
  729. module_init(microcode_init)
  730. module_exit(microcode_exit)