microcode.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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. #include <asm/microcode.h>
  95. MODULE_DESCRIPTION("Intel CPU (IA-32) Microcode Update Driver");
  96. MODULE_AUTHOR("Tigran Aivazian <tigran@aivazian.fsnet.co.uk>");
  97. MODULE_LICENSE("GPL");
  98. #define MICROCODE_VERSION "1.14a"
  99. #define DEFAULT_UCODE_DATASIZE (2000) /* 2000 bytes */
  100. #define MC_HEADER_SIZE (sizeof (microcode_header_t)) /* 48 bytes */
  101. #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE) /* 2048 bytes */
  102. #define EXT_HEADER_SIZE (sizeof (struct extended_sigtable)) /* 20 bytes */
  103. #define EXT_SIGNATURE_SIZE (sizeof (struct extended_signature)) /* 12 bytes */
  104. #define DWSIZE (sizeof (u32))
  105. #define get_totalsize(mc) \
  106. (((microcode_t *)mc)->hdr.totalsize ? \
  107. ((microcode_t *)mc)->hdr.totalsize : DEFAULT_UCODE_TOTALSIZE)
  108. #define get_datasize(mc) \
  109. (((microcode_t *)mc)->hdr.datasize ? \
  110. ((microcode_t *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE)
  111. #define sigmatch(s1, s2, p1, p2) \
  112. (((s1) == (s2)) && (((p1) & (p2)) || (((p1) == 0) && ((p2) == 0))))
  113. #define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE)
  114. /* serialize access to the physical write to MSR 0x79 */
  115. static DEFINE_SPINLOCK(microcode_update_lock);
  116. /* no concurrent ->write()s are allowed on /dev/cpu/microcode */
  117. static DEFINE_MUTEX(microcode_mutex);
  118. static struct ucode_cpu_info {
  119. int valid;
  120. unsigned int sig;
  121. unsigned int pf;
  122. unsigned int rev;
  123. microcode_t *mc;
  124. } ucode_cpu_info[NR_CPUS];
  125. static void collect_cpu_info(int cpu_num)
  126. {
  127. struct cpuinfo_x86 *c = &cpu_data(cpu_num);
  128. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  129. unsigned int val[2];
  130. /* We should bind the task to the CPU */
  131. BUG_ON(raw_smp_processor_id() != cpu_num);
  132. uci->pf = uci->rev = 0;
  133. uci->mc = NULL;
  134. uci->valid = 1;
  135. if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
  136. cpu_has(c, X86_FEATURE_IA64)) {
  137. printk(KERN_ERR "microcode: CPU%d not a capable Intel "
  138. "processor\n", cpu_num);
  139. uci->valid = 0;
  140. return;
  141. }
  142. uci->sig = cpuid_eax(0x00000001);
  143. if ((c->x86_model >= 5) || (c->x86 > 6)) {
  144. /* get processor flags from MSR 0x17 */
  145. rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  146. uci->pf = 1 << ((val[1] >> 18) & 7);
  147. }
  148. wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  149. /* see notes above for revision 1.07. Apparent chip bug */
  150. sync_core();
  151. /* get the current revision from MSR 0x8B */
  152. rdmsr(MSR_IA32_UCODE_REV, val[0], uci->rev);
  153. pr_debug("microcode: collect_cpu_info : sig=0x%x, pf=0x%x, rev=0x%x\n",
  154. uci->sig, uci->pf, uci->rev);
  155. }
  156. static inline int microcode_update_match(int cpu_num,
  157. microcode_header_t *mc_header, int sig, int pf)
  158. {
  159. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  160. if (!sigmatch(sig, uci->sig, pf, uci->pf)
  161. || mc_header->rev <= uci->rev)
  162. return 0;
  163. return 1;
  164. }
  165. static int microcode_sanity_check(void *mc)
  166. {
  167. microcode_header_t *mc_header = mc;
  168. struct extended_sigtable *ext_header = NULL;
  169. struct extended_signature *ext_sig;
  170. unsigned long total_size, data_size, ext_table_size;
  171. int sum, orig_sum, ext_sigcount = 0, i;
  172. total_size = get_totalsize(mc_header);
  173. data_size = get_datasize(mc_header);
  174. if (data_size + MC_HEADER_SIZE > total_size) {
  175. printk(KERN_ERR "microcode: error! "
  176. "Bad data size in microcode data file\n");
  177. return -EINVAL;
  178. }
  179. if (mc_header->ldrver != 1 || mc_header->hdrver != 1) {
  180. printk(KERN_ERR "microcode: error! "
  181. "Unknown microcode update format\n");
  182. return -EINVAL;
  183. }
  184. ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
  185. if (ext_table_size) {
  186. if ((ext_table_size < EXT_HEADER_SIZE)
  187. || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
  188. printk(KERN_ERR "microcode: error! "
  189. "Small exttable size in microcode data file\n");
  190. return -EINVAL;
  191. }
  192. ext_header = mc + MC_HEADER_SIZE + data_size;
  193. if (ext_table_size != exttable_size(ext_header)) {
  194. printk(KERN_ERR "microcode: error! "
  195. "Bad exttable size in microcode data file\n");
  196. return -EFAULT;
  197. }
  198. ext_sigcount = ext_header->count;
  199. }
  200. /* check extended table checksum */
  201. if (ext_table_size) {
  202. int ext_table_sum = 0;
  203. int *ext_tablep = (int *)ext_header;
  204. i = ext_table_size / DWSIZE;
  205. while (i--)
  206. ext_table_sum += ext_tablep[i];
  207. if (ext_table_sum) {
  208. printk(KERN_WARNING "microcode: aborting, "
  209. "bad extended signature table checksum\n");
  210. return -EINVAL;
  211. }
  212. }
  213. /* calculate the checksum */
  214. orig_sum = 0;
  215. i = (MC_HEADER_SIZE + data_size) / DWSIZE;
  216. while (i--)
  217. orig_sum += ((int *)mc)[i];
  218. if (orig_sum) {
  219. printk(KERN_ERR "microcode: aborting, bad checksum\n");
  220. return -EINVAL;
  221. }
  222. if (!ext_table_size)
  223. return 0;
  224. /* check extended signature checksum */
  225. for (i = 0; i < ext_sigcount; i++) {
  226. ext_sig = (void *)ext_header + EXT_HEADER_SIZE +
  227. EXT_SIGNATURE_SIZE * i;
  228. sum = orig_sum
  229. - (mc_header->sig + mc_header->pf + mc_header->cksum)
  230. + (ext_sig->sig + ext_sig->pf + ext_sig->cksum);
  231. if (sum) {
  232. printk(KERN_ERR "microcode: aborting, bad checksum\n");
  233. return -EINVAL;
  234. }
  235. }
  236. return 0;
  237. }
  238. /*
  239. * return 0 - no update found
  240. * return 1 - found update
  241. * return < 0 - error
  242. */
  243. static int get_maching_microcode(void *mc, int cpu)
  244. {
  245. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  246. microcode_header_t *mc_header = mc;
  247. struct extended_sigtable *ext_header;
  248. unsigned long total_size = get_totalsize(mc_header);
  249. int ext_sigcount, i;
  250. struct extended_signature *ext_sig;
  251. void *new_mc;
  252. if (microcode_update_match(cpu, mc_header,
  253. mc_header->sig, mc_header->pf))
  254. goto find;
  255. if (total_size <= get_datasize(mc_header) + MC_HEADER_SIZE)
  256. return 0;
  257. ext_header = mc + get_datasize(mc_header) + MC_HEADER_SIZE;
  258. ext_sigcount = ext_header->count;
  259. ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
  260. for (i = 0; i < ext_sigcount; i++) {
  261. if (microcode_update_match(cpu, mc_header,
  262. ext_sig->sig, ext_sig->pf))
  263. goto find;
  264. ext_sig++;
  265. }
  266. return 0;
  267. find:
  268. pr_debug("microcode: CPU%d found a matching microcode update with"
  269. " version 0x%x (current=0x%x)\n", cpu, mc_header->rev,uci->rev);
  270. new_mc = vmalloc(total_size);
  271. if (!new_mc) {
  272. printk(KERN_ERR "microcode: error! Can not allocate memory\n");
  273. return -ENOMEM;
  274. }
  275. /* free previous update file */
  276. vfree(uci->mc);
  277. memcpy(new_mc, mc, total_size);
  278. uci->mc = new_mc;
  279. return 1;
  280. }
  281. static void apply_microcode(int cpu)
  282. {
  283. unsigned long flags;
  284. unsigned int val[2];
  285. int cpu_num = raw_smp_processor_id();
  286. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  287. /* We should bind the task to the CPU */
  288. BUG_ON(cpu_num != cpu);
  289. if (uci->mc == NULL)
  290. return;
  291. /* serialize access to the physical write to MSR 0x79 */
  292. spin_lock_irqsave(&microcode_update_lock, flags);
  293. /* write microcode via MSR 0x79 */
  294. wrmsr(MSR_IA32_UCODE_WRITE,
  295. (unsigned long) uci->mc->bits,
  296. (unsigned long) uci->mc->bits >> 16 >> 16);
  297. wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  298. /* see notes above for revision 1.07. Apparent chip bug */
  299. sync_core();
  300. /* get the current revision from MSR 0x8B */
  301. rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  302. spin_unlock_irqrestore(&microcode_update_lock, flags);
  303. if (val[1] != uci->mc->hdr.rev) {
  304. printk(KERN_ERR "microcode: CPU%d update from revision "
  305. "0x%x to 0x%x failed\n", cpu_num, uci->rev, val[1]);
  306. return;
  307. }
  308. printk(KERN_INFO "microcode: CPU%d updated from revision "
  309. "0x%x to 0x%x, date = %08x \n",
  310. cpu_num, uci->rev, val[1], uci->mc->hdr.date);
  311. uci->rev = val[1];
  312. }
  313. #ifdef CONFIG_MICROCODE_OLD_INTERFACE
  314. static void __user *user_buffer; /* user area microcode data buffer */
  315. static unsigned int user_buffer_size; /* it's size */
  316. static long get_next_ucode(void **mc, long offset)
  317. {
  318. microcode_header_t mc_header;
  319. unsigned long total_size;
  320. /* No more data */
  321. if (offset >= user_buffer_size)
  322. return 0;
  323. if (copy_from_user(&mc_header, user_buffer + offset, MC_HEADER_SIZE)) {
  324. printk(KERN_ERR "microcode: error! Can not read user data\n");
  325. return -EFAULT;
  326. }
  327. total_size = get_totalsize(&mc_header);
  328. if (offset + total_size > user_buffer_size) {
  329. printk(KERN_ERR "microcode: error! Bad total size in microcode "
  330. "data file\n");
  331. return -EINVAL;
  332. }
  333. *mc = vmalloc(total_size);
  334. if (!*mc)
  335. return -ENOMEM;
  336. if (copy_from_user(*mc, user_buffer + offset, total_size)) {
  337. printk(KERN_ERR "microcode: error! Can not read user data\n");
  338. vfree(*mc);
  339. return -EFAULT;
  340. }
  341. return offset + total_size;
  342. }
  343. static int do_microcode_update (void)
  344. {
  345. long cursor = 0;
  346. int error = 0;
  347. void *new_mc = NULL;
  348. int cpu;
  349. cpumask_t old;
  350. cpumask_of_cpu_ptr_declare(newmask);
  351. old = current->cpus_allowed;
  352. while ((cursor = get_next_ucode(&new_mc, cursor)) > 0) {
  353. error = microcode_sanity_check(new_mc);
  354. if (error)
  355. goto out;
  356. /*
  357. * It's possible the data file has multiple matching ucode,
  358. * lets keep searching till the latest version
  359. */
  360. for_each_online_cpu(cpu) {
  361. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  362. if (!uci->valid)
  363. continue;
  364. cpumask_of_cpu_ptr_next(newmask, cpu);
  365. set_cpus_allowed_ptr(current, newmask);
  366. error = get_maching_microcode(new_mc, cpu);
  367. if (error < 0)
  368. goto out;
  369. if (error == 1)
  370. apply_microcode(cpu);
  371. }
  372. vfree(new_mc);
  373. }
  374. out:
  375. if (cursor > 0)
  376. vfree(new_mc);
  377. if (cursor < 0)
  378. error = cursor;
  379. set_cpus_allowed_ptr(current, &old);
  380. return error;
  381. }
  382. static int microcode_open (struct inode *unused1, struct file *unused2)
  383. {
  384. cycle_kernel_lock();
  385. return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
  386. }
  387. static ssize_t microcode_write (struct file *file, const char __user *buf, size_t len, loff_t *ppos)
  388. {
  389. ssize_t ret;
  390. if ((len >> PAGE_SHIFT) > num_physpages) {
  391. printk(KERN_ERR "microcode: too much data (max %ld pages)\n", num_physpages);
  392. return -EINVAL;
  393. }
  394. get_online_cpus();
  395. mutex_lock(&microcode_mutex);
  396. user_buffer = (void __user *) buf;
  397. user_buffer_size = (int) len;
  398. ret = do_microcode_update();
  399. if (!ret)
  400. ret = (ssize_t)len;
  401. mutex_unlock(&microcode_mutex);
  402. put_online_cpus();
  403. return ret;
  404. }
  405. static const struct file_operations microcode_fops = {
  406. .owner = THIS_MODULE,
  407. .write = microcode_write,
  408. .open = microcode_open,
  409. };
  410. static struct miscdevice microcode_dev = {
  411. .minor = MICROCODE_MINOR,
  412. .name = "microcode",
  413. .fops = &microcode_fops,
  414. };
  415. static int __init microcode_dev_init (void)
  416. {
  417. int error;
  418. error = misc_register(&microcode_dev);
  419. if (error) {
  420. printk(KERN_ERR
  421. "microcode: can't misc_register on minor=%d\n",
  422. MICROCODE_MINOR);
  423. return error;
  424. }
  425. return 0;
  426. }
  427. static void microcode_dev_exit (void)
  428. {
  429. misc_deregister(&microcode_dev);
  430. }
  431. MODULE_ALIAS_MISCDEV(MICROCODE_MINOR);
  432. #else
  433. #define microcode_dev_init() 0
  434. #define microcode_dev_exit() do { } while(0)
  435. #endif
  436. static long get_next_ucode_from_buffer(void **mc, const u8 *buf,
  437. unsigned long size, long offset)
  438. {
  439. microcode_header_t *mc_header;
  440. unsigned long total_size;
  441. /* No more data */
  442. if (offset >= size)
  443. return 0;
  444. mc_header = (microcode_header_t *)(buf + offset);
  445. total_size = get_totalsize(mc_header);
  446. if (offset + total_size > size) {
  447. printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
  448. return -EINVAL;
  449. }
  450. *mc = vmalloc(total_size);
  451. if (!*mc) {
  452. printk(KERN_ERR "microcode: error! Can not allocate memory\n");
  453. return -ENOMEM;
  454. }
  455. memcpy(*mc, buf + offset, total_size);
  456. return offset + total_size;
  457. }
  458. /* fake device for request_firmware */
  459. static struct platform_device *microcode_pdev;
  460. static int cpu_request_microcode(int cpu)
  461. {
  462. char name[30];
  463. struct cpuinfo_x86 *c = &cpu_data(cpu);
  464. const struct firmware *firmware;
  465. const u8 *buf;
  466. unsigned long size;
  467. long offset = 0;
  468. int error;
  469. void *mc;
  470. /* We should bind the task to the CPU */
  471. BUG_ON(cpu != raw_smp_processor_id());
  472. sprintf(name,"intel-ucode/%02x-%02x-%02x",
  473. c->x86, c->x86_model, c->x86_mask);
  474. error = request_firmware(&firmware, name, &microcode_pdev->dev);
  475. if (error) {
  476. pr_debug("microcode: data file %s load failed\n", name);
  477. return error;
  478. }
  479. buf = firmware->data;
  480. size = firmware->size;
  481. while ((offset = get_next_ucode_from_buffer(&mc, buf, size, offset))
  482. > 0) {
  483. error = microcode_sanity_check(mc);
  484. if (error)
  485. break;
  486. error = get_maching_microcode(mc, cpu);
  487. if (error < 0)
  488. break;
  489. /*
  490. * It's possible the data file has multiple matching ucode,
  491. * lets keep searching till the latest version
  492. */
  493. if (error == 1) {
  494. apply_microcode(cpu);
  495. error = 0;
  496. }
  497. vfree(mc);
  498. }
  499. if (offset > 0)
  500. vfree(mc);
  501. if (offset < 0)
  502. error = offset;
  503. release_firmware(firmware);
  504. return error;
  505. }
  506. static int apply_microcode_check_cpu(int cpu)
  507. {
  508. struct cpuinfo_x86 *c = &cpu_data(cpu);
  509. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  510. cpumask_t old;
  511. cpumask_of_cpu_ptr(newmask, cpu);
  512. unsigned int val[2];
  513. int err = 0;
  514. /* Check if the microcode is available */
  515. if (!uci->mc)
  516. return 0;
  517. old = current->cpus_allowed;
  518. set_cpus_allowed_ptr(current, newmask);
  519. /* Check if the microcode we have in memory matches the CPU */
  520. if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
  521. cpu_has(c, X86_FEATURE_IA64) || uci->sig != cpuid_eax(0x00000001))
  522. err = -EINVAL;
  523. if (!err && ((c->x86_model >= 5) || (c->x86 > 6))) {
  524. /* get processor flags from MSR 0x17 */
  525. rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  526. if (uci->pf != (1 << ((val[1] >> 18) & 7)))
  527. err = -EINVAL;
  528. }
  529. if (!err) {
  530. wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  531. /* see notes above for revision 1.07. Apparent chip bug */
  532. sync_core();
  533. /* get the current revision from MSR 0x8B */
  534. rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  535. if (uci->rev != val[1])
  536. err = -EINVAL;
  537. }
  538. if (!err)
  539. apply_microcode(cpu);
  540. else
  541. printk(KERN_ERR "microcode: Could not apply microcode to CPU%d:"
  542. " sig=0x%x, pf=0x%x, rev=0x%x\n",
  543. cpu, uci->sig, uci->pf, uci->rev);
  544. set_cpus_allowed_ptr(current, &old);
  545. return err;
  546. }
  547. static void microcode_init_cpu(int cpu, int resume)
  548. {
  549. cpumask_t old;
  550. cpumask_of_cpu_ptr(newmask, cpu);
  551. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  552. old = current->cpus_allowed;
  553. set_cpus_allowed_ptr(current, newmask);
  554. mutex_lock(&microcode_mutex);
  555. collect_cpu_info(cpu);
  556. if (uci->valid && system_state == SYSTEM_RUNNING && !resume)
  557. cpu_request_microcode(cpu);
  558. mutex_unlock(&microcode_mutex);
  559. set_cpus_allowed_ptr(current, &old);
  560. }
  561. static void microcode_fini_cpu(int cpu)
  562. {
  563. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  564. mutex_lock(&microcode_mutex);
  565. uci->valid = 0;
  566. vfree(uci->mc);
  567. uci->mc = NULL;
  568. mutex_unlock(&microcode_mutex);
  569. }
  570. static ssize_t reload_store(struct sys_device *dev,
  571. struct sysdev_attribute *attr,
  572. const char *buf, size_t sz)
  573. {
  574. struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
  575. char *end;
  576. unsigned long val = simple_strtoul(buf, &end, 0);
  577. int err = 0;
  578. int cpu = dev->id;
  579. if (end == buf)
  580. return -EINVAL;
  581. if (val == 1) {
  582. cpumask_t old;
  583. cpumask_of_cpu_ptr(newmask, cpu);
  584. old = current->cpus_allowed;
  585. get_online_cpus();
  586. set_cpus_allowed_ptr(current, newmask);
  587. mutex_lock(&microcode_mutex);
  588. if (uci->valid)
  589. err = cpu_request_microcode(cpu);
  590. mutex_unlock(&microcode_mutex);
  591. put_online_cpus();
  592. set_cpus_allowed_ptr(current, &old);
  593. }
  594. if (err)
  595. return err;
  596. return sz;
  597. }
  598. static ssize_t version_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->rev);
  603. }
  604. static ssize_t pf_show(struct sys_device *dev,
  605. struct sysdev_attribute *attr, char *buf)
  606. {
  607. struct ucode_cpu_info *uci = ucode_cpu_info + dev->id;
  608. return sprintf(buf, "0x%x\n", uci->pf);
  609. }
  610. static SYSDEV_ATTR(reload, 0200, NULL, reload_store);
  611. static SYSDEV_ATTR(version, 0400, version_show, NULL);
  612. static SYSDEV_ATTR(processor_flags, 0400, pf_show, NULL);
  613. static struct attribute *mc_default_attrs[] = {
  614. &attr_reload.attr,
  615. &attr_version.attr,
  616. &attr_processor_flags.attr,
  617. NULL
  618. };
  619. static struct attribute_group mc_attr_group = {
  620. .attrs = mc_default_attrs,
  621. .name = "microcode",
  622. };
  623. static int __mc_sysdev_add(struct sys_device *sys_dev, int resume)
  624. {
  625. int err, cpu = sys_dev->id;
  626. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  627. if (!cpu_online(cpu))
  628. return 0;
  629. pr_debug("microcode: CPU%d added\n", cpu);
  630. memset(uci, 0, sizeof(*uci));
  631. err = sysfs_create_group(&sys_dev->kobj, &mc_attr_group);
  632. if (err)
  633. return err;
  634. microcode_init_cpu(cpu, resume);
  635. return 0;
  636. }
  637. static int mc_sysdev_add(struct sys_device *sys_dev)
  638. {
  639. return __mc_sysdev_add(sys_dev, 0);
  640. }
  641. static int mc_sysdev_remove(struct sys_device *sys_dev)
  642. {
  643. int cpu = sys_dev->id;
  644. if (!cpu_online(cpu))
  645. return 0;
  646. pr_debug("microcode: CPU%d removed\n", cpu);
  647. microcode_fini_cpu(cpu);
  648. sysfs_remove_group(&sys_dev->kobj, &mc_attr_group);
  649. return 0;
  650. }
  651. static int mc_sysdev_resume(struct sys_device *dev)
  652. {
  653. int cpu = dev->id;
  654. if (!cpu_online(cpu))
  655. return 0;
  656. pr_debug("microcode: CPU%d resumed\n", cpu);
  657. /* only CPU 0 will apply ucode here */
  658. apply_microcode(0);
  659. return 0;
  660. }
  661. static struct sysdev_driver mc_sysdev_driver = {
  662. .add = mc_sysdev_add,
  663. .remove = mc_sysdev_remove,
  664. .resume = mc_sysdev_resume,
  665. };
  666. static __cpuinit int
  667. mc_cpu_callback(struct notifier_block *nb, unsigned long action, void *hcpu)
  668. {
  669. unsigned int cpu = (unsigned long)hcpu;
  670. struct sys_device *sys_dev;
  671. sys_dev = get_cpu_sysdev(cpu);
  672. switch (action) {
  673. case CPU_UP_CANCELED_FROZEN:
  674. /* The CPU refused to come up during a system resume */
  675. microcode_fini_cpu(cpu);
  676. break;
  677. case CPU_ONLINE:
  678. case CPU_DOWN_FAILED:
  679. mc_sysdev_add(sys_dev);
  680. break;
  681. case CPU_ONLINE_FROZEN:
  682. /* System-wide resume is in progress, try to apply microcode */
  683. if (apply_microcode_check_cpu(cpu)) {
  684. /* The application of microcode failed */
  685. microcode_fini_cpu(cpu);
  686. __mc_sysdev_add(sys_dev, 1);
  687. break;
  688. }
  689. case CPU_DOWN_FAILED_FROZEN:
  690. if (sysfs_create_group(&sys_dev->kobj, &mc_attr_group))
  691. printk(KERN_ERR "microcode: Failed to create the sysfs "
  692. "group for CPU%d\n", cpu);
  693. break;
  694. case CPU_DOWN_PREPARE:
  695. mc_sysdev_remove(sys_dev);
  696. break;
  697. case CPU_DOWN_PREPARE_FROZEN:
  698. /* Suspend is in progress, only remove the interface */
  699. sysfs_remove_group(&sys_dev->kobj, &mc_attr_group);
  700. break;
  701. }
  702. return NOTIFY_OK;
  703. }
  704. static struct notifier_block __refdata mc_cpu_notifier = {
  705. .notifier_call = mc_cpu_callback,
  706. };
  707. static int __init microcode_init (void)
  708. {
  709. int error;
  710. printk(KERN_INFO
  711. "IA-32 Microcode Update Driver: v" MICROCODE_VERSION " <tigran@aivazian.fsnet.co.uk>\n");
  712. error = microcode_dev_init();
  713. if (error)
  714. return error;
  715. microcode_pdev = platform_device_register_simple("microcode", -1,
  716. NULL, 0);
  717. if (IS_ERR(microcode_pdev)) {
  718. microcode_dev_exit();
  719. return PTR_ERR(microcode_pdev);
  720. }
  721. get_online_cpus();
  722. error = sysdev_driver_register(&cpu_sysdev_class, &mc_sysdev_driver);
  723. put_online_cpus();
  724. if (error) {
  725. microcode_dev_exit();
  726. platform_device_unregister(microcode_pdev);
  727. return error;
  728. }
  729. register_hotcpu_notifier(&mc_cpu_notifier);
  730. return 0;
  731. }
  732. static void __exit microcode_exit (void)
  733. {
  734. microcode_dev_exit();
  735. unregister_hotcpu_notifier(&mc_cpu_notifier);
  736. get_online_cpus();
  737. sysdev_driver_unregister(&cpu_sysdev_class, &mc_sysdev_driver);
  738. put_online_cpus();
  739. platform_device_unregister(microcode_pdev);
  740. }
  741. module_init(microcode_init)
  742. module_exit(microcode_exit)