intel.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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. * Intel CPU microcode early update for Linux
  8. *
  9. * Copyright (C) 2012 Fenghua Yu <fenghua.yu@intel.com>
  10. * H Peter Anvin" <hpa@zytor.com>
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. /*
  18. * This needs to be before all headers so that pr_debug in printk.h doesn't turn
  19. * printk calls into no_printk().
  20. *
  21. *#define DEBUG
  22. */
  23. #define pr_fmt(fmt) "microcode: " fmt
  24. #include <linux/earlycpio.h>
  25. #include <linux/firmware.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/initrd.h>
  29. #include <linux/kernel.h>
  30. #include <linux/slab.h>
  31. #include <linux/cpu.h>
  32. #include <linux/mm.h>
  33. #include <asm/microcode_intel.h>
  34. #include <asm/processor.h>
  35. #include <asm/tlbflush.h>
  36. #include <asm/setup.h>
  37. #include <asm/msr.h>
  38. static const char ucode_path[] = "kernel/x86/microcode/GenuineIntel.bin";
  39. /* Current microcode patch used in early patching */
  40. struct microcode_intel *intel_ucode_patch;
  41. static inline bool cpu_signatures_match(unsigned int s1, unsigned int p1,
  42. unsigned int s2, unsigned int p2)
  43. {
  44. if (s1 != s2)
  45. return false;
  46. /* Processor flags are either both 0 ... */
  47. if (!p1 && !p2)
  48. return true;
  49. /* ... or they intersect. */
  50. return p1 & p2;
  51. }
  52. /*
  53. * Returns 1 if update has been found, 0 otherwise.
  54. */
  55. static int find_matching_signature(void *mc, unsigned int csig, int cpf)
  56. {
  57. struct microcode_header_intel *mc_hdr = mc;
  58. struct extended_sigtable *ext_hdr;
  59. struct extended_signature *ext_sig;
  60. int i;
  61. if (cpu_signatures_match(csig, cpf, mc_hdr->sig, mc_hdr->pf))
  62. return 1;
  63. /* Look for ext. headers: */
  64. if (get_totalsize(mc_hdr) <= get_datasize(mc_hdr) + MC_HEADER_SIZE)
  65. return 0;
  66. ext_hdr = mc + get_datasize(mc_hdr) + MC_HEADER_SIZE;
  67. ext_sig = (void *)ext_hdr + EXT_HEADER_SIZE;
  68. for (i = 0; i < ext_hdr->count; i++) {
  69. if (cpu_signatures_match(csig, cpf, ext_sig->sig, ext_sig->pf))
  70. return 1;
  71. ext_sig++;
  72. }
  73. return 0;
  74. }
  75. /*
  76. * Returns 1 if update has been found, 0 otherwise.
  77. */
  78. static int has_newer_microcode(void *mc, unsigned int csig, int cpf, int new_rev)
  79. {
  80. struct microcode_header_intel *mc_hdr = mc;
  81. if (mc_hdr->rev <= new_rev)
  82. return 0;
  83. return find_matching_signature(mc, csig, cpf);
  84. }
  85. /*
  86. * Given CPU signature and a microcode patch, this function finds if the
  87. * microcode patch has matching family and model with the CPU.
  88. *
  89. * %true - if there's a match
  90. * %false - otherwise
  91. */
  92. static bool microcode_matches(struct microcode_header_intel *mc_header,
  93. unsigned long sig)
  94. {
  95. unsigned long total_size = get_totalsize(mc_header);
  96. unsigned long data_size = get_datasize(mc_header);
  97. struct extended_sigtable *ext_header;
  98. unsigned int fam_ucode, model_ucode;
  99. struct extended_signature *ext_sig;
  100. unsigned int fam, model;
  101. int ext_sigcount, i;
  102. fam = x86_family(sig);
  103. model = x86_model(sig);
  104. fam_ucode = x86_family(mc_header->sig);
  105. model_ucode = x86_model(mc_header->sig);
  106. if (fam == fam_ucode && model == model_ucode)
  107. return true;
  108. /* Look for ext. headers: */
  109. if (total_size <= data_size + MC_HEADER_SIZE)
  110. return false;
  111. ext_header = (void *) mc_header + data_size + MC_HEADER_SIZE;
  112. ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
  113. ext_sigcount = ext_header->count;
  114. for (i = 0; i < ext_sigcount; i++) {
  115. fam_ucode = x86_family(ext_sig->sig);
  116. model_ucode = x86_model(ext_sig->sig);
  117. if (fam == fam_ucode && model == model_ucode)
  118. return true;
  119. ext_sig++;
  120. }
  121. return false;
  122. }
  123. static struct ucode_patch *__alloc_microcode_buf(void *data, unsigned int size)
  124. {
  125. struct ucode_patch *p;
  126. p = kzalloc(size, GFP_KERNEL);
  127. if (!p)
  128. return ERR_PTR(-ENOMEM);
  129. p->data = kmemdup(data, size, GFP_KERNEL);
  130. if (!p->data) {
  131. kfree(p);
  132. return ERR_PTR(-ENOMEM);
  133. }
  134. return p;
  135. }
  136. static void save_microcode_patch(void *data, unsigned int size)
  137. {
  138. struct microcode_header_intel *mc_hdr, *mc_saved_hdr;
  139. struct ucode_patch *iter, *tmp, *p;
  140. bool prev_found = false;
  141. unsigned int sig, pf;
  142. mc_hdr = (struct microcode_header_intel *)data;
  143. list_for_each_entry_safe(iter, tmp, &microcode_cache, plist) {
  144. mc_saved_hdr = (struct microcode_header_intel *)iter->data;
  145. sig = mc_saved_hdr->sig;
  146. pf = mc_saved_hdr->pf;
  147. if (find_matching_signature(data, sig, pf)) {
  148. prev_found = true;
  149. if (mc_hdr->rev <= mc_saved_hdr->rev)
  150. continue;
  151. p = __alloc_microcode_buf(data, size);
  152. if (IS_ERR(p))
  153. pr_err("Error allocating buffer %p\n", data);
  154. else
  155. list_replace(&iter->plist, &p->plist);
  156. }
  157. }
  158. /*
  159. * There weren't any previous patches found in the list cache; save the
  160. * newly found.
  161. */
  162. if (!prev_found) {
  163. p = __alloc_microcode_buf(data, size);
  164. if (IS_ERR(p))
  165. pr_err("Error allocating buffer for %p\n", data);
  166. else
  167. list_add_tail(&p->plist, &microcode_cache);
  168. }
  169. }
  170. static int microcode_sanity_check(void *mc, int print_err)
  171. {
  172. unsigned long total_size, data_size, ext_table_size;
  173. struct microcode_header_intel *mc_header = mc;
  174. struct extended_sigtable *ext_header = NULL;
  175. u32 sum, orig_sum, ext_sigcount = 0, i;
  176. struct extended_signature *ext_sig;
  177. total_size = get_totalsize(mc_header);
  178. data_size = get_datasize(mc_header);
  179. if (data_size + MC_HEADER_SIZE > total_size) {
  180. if (print_err)
  181. pr_err("Error: bad microcode data file size.\n");
  182. return -EINVAL;
  183. }
  184. if (mc_header->ldrver != 1 || mc_header->hdrver != 1) {
  185. if (print_err)
  186. pr_err("Error: invalid/unknown microcode update format.\n");
  187. return -EINVAL;
  188. }
  189. ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
  190. if (ext_table_size) {
  191. u32 ext_table_sum = 0;
  192. u32 *ext_tablep;
  193. if ((ext_table_size < EXT_HEADER_SIZE)
  194. || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
  195. if (print_err)
  196. pr_err("Error: truncated extended signature table.\n");
  197. return -EINVAL;
  198. }
  199. ext_header = mc + MC_HEADER_SIZE + data_size;
  200. if (ext_table_size != exttable_size(ext_header)) {
  201. if (print_err)
  202. pr_err("Error: extended signature table size mismatch.\n");
  203. return -EFAULT;
  204. }
  205. ext_sigcount = ext_header->count;
  206. /*
  207. * Check extended table checksum: the sum of all dwords that
  208. * comprise a valid table must be 0.
  209. */
  210. ext_tablep = (u32 *)ext_header;
  211. i = ext_table_size / sizeof(u32);
  212. while (i--)
  213. ext_table_sum += ext_tablep[i];
  214. if (ext_table_sum) {
  215. if (print_err)
  216. pr_warn("Bad extended signature table checksum, aborting.\n");
  217. return -EINVAL;
  218. }
  219. }
  220. /*
  221. * Calculate the checksum of update data and header. The checksum of
  222. * valid update data and header including the extended signature table
  223. * must be 0.
  224. */
  225. orig_sum = 0;
  226. i = (MC_HEADER_SIZE + data_size) / sizeof(u32);
  227. while (i--)
  228. orig_sum += ((u32 *)mc)[i];
  229. if (orig_sum) {
  230. if (print_err)
  231. pr_err("Bad microcode data checksum, aborting.\n");
  232. return -EINVAL;
  233. }
  234. if (!ext_table_size)
  235. return 0;
  236. /*
  237. * Check extended signature checksum: 0 => valid.
  238. */
  239. for (i = 0; i < ext_sigcount; i++) {
  240. ext_sig = (void *)ext_header + EXT_HEADER_SIZE +
  241. EXT_SIGNATURE_SIZE * i;
  242. sum = (mc_header->sig + mc_header->pf + mc_header->cksum) -
  243. (ext_sig->sig + ext_sig->pf + ext_sig->cksum);
  244. if (sum) {
  245. if (print_err)
  246. pr_err("Bad extended signature checksum, aborting.\n");
  247. return -EINVAL;
  248. }
  249. }
  250. return 0;
  251. }
  252. /*
  253. * Get microcode matching with BSP's model. Only CPUs with the same model as
  254. * BSP can stay in the platform.
  255. */
  256. static struct microcode_intel *
  257. scan_microcode(void *data, size_t size, struct ucode_cpu_info *uci, bool save)
  258. {
  259. struct microcode_header_intel *mc_header;
  260. struct microcode_intel *patch = NULL;
  261. unsigned int mc_size;
  262. while (size) {
  263. if (size < sizeof(struct microcode_header_intel))
  264. break;
  265. mc_header = (struct microcode_header_intel *)data;
  266. mc_size = get_totalsize(mc_header);
  267. if (!mc_size ||
  268. mc_size > size ||
  269. microcode_sanity_check(data, 0) < 0)
  270. break;
  271. size -= mc_size;
  272. if (!microcode_matches(mc_header, uci->cpu_sig.sig)) {
  273. data += mc_size;
  274. continue;
  275. }
  276. if (save) {
  277. save_microcode_patch(data, mc_size);
  278. goto next;
  279. }
  280. if (!patch) {
  281. if (!has_newer_microcode(data,
  282. uci->cpu_sig.sig,
  283. uci->cpu_sig.pf,
  284. uci->cpu_sig.rev))
  285. goto next;
  286. } else {
  287. struct microcode_header_intel *phdr = &patch->hdr;
  288. if (!has_newer_microcode(data,
  289. phdr->sig,
  290. phdr->pf,
  291. phdr->rev))
  292. goto next;
  293. }
  294. /* We have a newer patch, save it. */
  295. patch = data;
  296. next:
  297. data += mc_size;
  298. }
  299. if (size)
  300. return NULL;
  301. return patch;
  302. }
  303. static int collect_cpu_info_early(struct ucode_cpu_info *uci)
  304. {
  305. unsigned int val[2];
  306. unsigned int family, model;
  307. struct cpu_signature csig = { 0 };
  308. unsigned int eax, ebx, ecx, edx;
  309. memset(uci, 0, sizeof(*uci));
  310. eax = 0x00000001;
  311. ecx = 0;
  312. native_cpuid(&eax, &ebx, &ecx, &edx);
  313. csig.sig = eax;
  314. family = x86_family(eax);
  315. model = x86_model(eax);
  316. if ((model >= 5) || (family > 6)) {
  317. /* get processor flags from MSR 0x17 */
  318. native_rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  319. csig.pf = 1 << ((val[1] >> 18) & 7);
  320. }
  321. native_wrmsrl(MSR_IA32_UCODE_REV, 0);
  322. /* As documented in the SDM: Do a CPUID 1 here */
  323. sync_core();
  324. /* get the current revision from MSR 0x8B */
  325. native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  326. csig.rev = val[1];
  327. uci->cpu_sig = csig;
  328. uci->valid = 1;
  329. return 0;
  330. }
  331. static void show_saved_mc(void)
  332. {
  333. #ifdef DEBUG
  334. int i = 0, j;
  335. unsigned int sig, pf, rev, total_size, data_size, date;
  336. struct ucode_cpu_info uci;
  337. struct ucode_patch *p;
  338. if (list_empty(&microcode_cache)) {
  339. pr_debug("no microcode data saved.\n");
  340. return;
  341. }
  342. collect_cpu_info_early(&uci);
  343. sig = uci.cpu_sig.sig;
  344. pf = uci.cpu_sig.pf;
  345. rev = uci.cpu_sig.rev;
  346. pr_debug("CPU: sig=0x%x, pf=0x%x, rev=0x%x\n", sig, pf, rev);
  347. list_for_each_entry(p, &microcode_cache, plist) {
  348. struct microcode_header_intel *mc_saved_header;
  349. struct extended_sigtable *ext_header;
  350. struct extended_signature *ext_sig;
  351. int ext_sigcount;
  352. mc_saved_header = (struct microcode_header_intel *)p->data;
  353. sig = mc_saved_header->sig;
  354. pf = mc_saved_header->pf;
  355. rev = mc_saved_header->rev;
  356. date = mc_saved_header->date;
  357. total_size = get_totalsize(mc_saved_header);
  358. data_size = get_datasize(mc_saved_header);
  359. pr_debug("mc_saved[%d]: sig=0x%x, pf=0x%x, rev=0x%x, total size=0x%x, date = %04x-%02x-%02x\n",
  360. i++, sig, pf, rev, total_size,
  361. date & 0xffff,
  362. date >> 24,
  363. (date >> 16) & 0xff);
  364. /* Look for ext. headers: */
  365. if (total_size <= data_size + MC_HEADER_SIZE)
  366. continue;
  367. ext_header = (void *)mc_saved_header + data_size + MC_HEADER_SIZE;
  368. ext_sigcount = ext_header->count;
  369. ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
  370. for (j = 0; j < ext_sigcount; j++) {
  371. sig = ext_sig->sig;
  372. pf = ext_sig->pf;
  373. pr_debug("\tExtended[%d]: sig=0x%x, pf=0x%x\n",
  374. j, sig, pf);
  375. ext_sig++;
  376. }
  377. }
  378. #endif
  379. }
  380. /*
  381. * Save this microcode patch. It will be loaded early when a CPU is
  382. * hot-added or resumes.
  383. */
  384. static void save_mc_for_early(u8 *mc, unsigned int size)
  385. {
  386. #ifdef CONFIG_HOTPLUG_CPU
  387. /* Synchronization during CPU hotplug. */
  388. static DEFINE_MUTEX(x86_cpu_microcode_mutex);
  389. mutex_lock(&x86_cpu_microcode_mutex);
  390. save_microcode_patch(mc, size);
  391. show_saved_mc();
  392. mutex_unlock(&x86_cpu_microcode_mutex);
  393. #endif
  394. }
  395. static bool load_builtin_intel_microcode(struct cpio_data *cp)
  396. {
  397. unsigned int eax = 1, ebx, ecx = 0, edx;
  398. char name[30];
  399. if (IS_ENABLED(CONFIG_X86_32))
  400. return false;
  401. native_cpuid(&eax, &ebx, &ecx, &edx);
  402. sprintf(name, "intel-ucode/%02x-%02x-%02x",
  403. x86_family(eax), x86_model(eax), x86_stepping(eax));
  404. return get_builtin_firmware(cp, name);
  405. }
  406. /*
  407. * Print ucode update info.
  408. */
  409. static void
  410. print_ucode_info(struct ucode_cpu_info *uci, unsigned int date)
  411. {
  412. pr_info_once("microcode updated early to revision 0x%x, date = %04x-%02x-%02x\n",
  413. uci->cpu_sig.rev,
  414. date & 0xffff,
  415. date >> 24,
  416. (date >> 16) & 0xff);
  417. }
  418. #ifdef CONFIG_X86_32
  419. static int delay_ucode_info;
  420. static int current_mc_date;
  421. /*
  422. * Print early updated ucode info after printk works. This is delayed info dump.
  423. */
  424. void show_ucode_info_early(void)
  425. {
  426. struct ucode_cpu_info uci;
  427. if (delay_ucode_info) {
  428. collect_cpu_info_early(&uci);
  429. print_ucode_info(&uci, current_mc_date);
  430. delay_ucode_info = 0;
  431. }
  432. }
  433. /*
  434. * At this point, we can not call printk() yet. Delay printing microcode info in
  435. * show_ucode_info_early() until printk() works.
  436. */
  437. static void print_ucode(struct ucode_cpu_info *uci)
  438. {
  439. struct microcode_intel *mc;
  440. int *delay_ucode_info_p;
  441. int *current_mc_date_p;
  442. mc = uci->mc;
  443. if (!mc)
  444. return;
  445. delay_ucode_info_p = (int *)__pa_nodebug(&delay_ucode_info);
  446. current_mc_date_p = (int *)__pa_nodebug(&current_mc_date);
  447. *delay_ucode_info_p = 1;
  448. *current_mc_date_p = mc->hdr.date;
  449. }
  450. #else
  451. /*
  452. * Flush global tlb. We only do this in x86_64 where paging has been enabled
  453. * already and PGE should be enabled as well.
  454. */
  455. static inline void flush_tlb_early(void)
  456. {
  457. __native_flush_tlb_global_irq_disabled();
  458. }
  459. static inline void print_ucode(struct ucode_cpu_info *uci)
  460. {
  461. struct microcode_intel *mc;
  462. mc = uci->mc;
  463. if (!mc)
  464. return;
  465. print_ucode_info(uci, mc->hdr.date);
  466. }
  467. #endif
  468. static int apply_microcode_early(struct ucode_cpu_info *uci, bool early)
  469. {
  470. struct microcode_intel *mc;
  471. unsigned int val[2];
  472. mc = uci->mc;
  473. if (!mc)
  474. return 0;
  475. /* write microcode via MSR 0x79 */
  476. native_wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits);
  477. native_wrmsrl(MSR_IA32_UCODE_REV, 0);
  478. /* As documented in the SDM: Do a CPUID 1 here */
  479. sync_core();
  480. /* get the current revision from MSR 0x8B */
  481. native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  482. if (val[1] != mc->hdr.rev)
  483. return -1;
  484. #ifdef CONFIG_X86_64
  485. /* Flush global tlb. This is precaution. */
  486. flush_tlb_early();
  487. #endif
  488. uci->cpu_sig.rev = val[1];
  489. if (early)
  490. print_ucode(uci);
  491. else
  492. print_ucode_info(uci, mc->hdr.date);
  493. return 0;
  494. }
  495. int __init save_microcode_in_initrd_intel(void)
  496. {
  497. struct ucode_cpu_info uci;
  498. struct cpio_data cp;
  499. /*
  500. * AP loading didn't find any microcode patch, no need to save anything.
  501. */
  502. if (!intel_ucode_patch || IS_ERR(intel_ucode_patch))
  503. return 0;
  504. if (!load_builtin_intel_microcode(&cp))
  505. cp = find_microcode_in_initrd(ucode_path, false);
  506. if (!(cp.data && cp.size))
  507. return 0;
  508. collect_cpu_info_early(&uci);
  509. scan_microcode(cp.data, cp.size, &uci, true);
  510. show_saved_mc();
  511. return 0;
  512. }
  513. /*
  514. * @res_patch, output: a pointer to the patch we found.
  515. */
  516. static struct microcode_intel *__load_ucode_intel(struct ucode_cpu_info *uci)
  517. {
  518. static const char *path;
  519. struct cpio_data cp;
  520. bool use_pa;
  521. if (IS_ENABLED(CONFIG_X86_32)) {
  522. path = (const char *)__pa_nodebug(ucode_path);
  523. use_pa = true;
  524. } else {
  525. path = ucode_path;
  526. use_pa = false;
  527. }
  528. /* try built-in microcode first */
  529. if (!load_builtin_intel_microcode(&cp))
  530. cp = find_microcode_in_initrd(path, use_pa);
  531. if (!(cp.data && cp.size))
  532. return NULL;
  533. collect_cpu_info_early(uci);
  534. return scan_microcode(cp.data, cp.size, uci, false);
  535. }
  536. void __init load_ucode_intel_bsp(void)
  537. {
  538. struct microcode_intel *patch;
  539. struct ucode_cpu_info uci;
  540. patch = __load_ucode_intel(&uci);
  541. if (!patch)
  542. return;
  543. uci.mc = patch;
  544. apply_microcode_early(&uci, true);
  545. }
  546. void load_ucode_intel_ap(void)
  547. {
  548. struct microcode_intel *patch, **iup;
  549. struct ucode_cpu_info uci;
  550. if (IS_ENABLED(CONFIG_X86_32))
  551. iup = (struct microcode_intel **) __pa_nodebug(&intel_ucode_patch);
  552. else
  553. iup = &intel_ucode_patch;
  554. reget:
  555. if (!*iup) {
  556. patch = __load_ucode_intel(&uci);
  557. if (!patch)
  558. return;
  559. *iup = patch;
  560. }
  561. uci.mc = *iup;
  562. if (apply_microcode_early(&uci, true)) {
  563. /* Mixed-silicon system? Try to refetch the proper patch: */
  564. *iup = NULL;
  565. goto reget;
  566. }
  567. }
  568. static struct microcode_intel *find_patch(struct ucode_cpu_info *uci)
  569. {
  570. struct microcode_header_intel *phdr;
  571. struct ucode_patch *iter, *tmp;
  572. list_for_each_entry_safe(iter, tmp, &microcode_cache, plist) {
  573. phdr = (struct microcode_header_intel *)iter->data;
  574. if (phdr->rev <= uci->cpu_sig.rev)
  575. continue;
  576. if (!find_matching_signature(phdr,
  577. uci->cpu_sig.sig,
  578. uci->cpu_sig.pf))
  579. continue;
  580. return iter->data;
  581. }
  582. return NULL;
  583. }
  584. void reload_ucode_intel(void)
  585. {
  586. struct microcode_intel *p;
  587. struct ucode_cpu_info uci;
  588. collect_cpu_info_early(&uci);
  589. p = find_patch(&uci);
  590. if (!p)
  591. return;
  592. uci.mc = p;
  593. apply_microcode_early(&uci, false);
  594. }
  595. static int collect_cpu_info(int cpu_num, struct cpu_signature *csig)
  596. {
  597. static struct cpu_signature prev;
  598. struct cpuinfo_x86 *c = &cpu_data(cpu_num);
  599. unsigned int val[2];
  600. memset(csig, 0, sizeof(*csig));
  601. csig->sig = cpuid_eax(0x00000001);
  602. if ((c->x86_model >= 5) || (c->x86 > 6)) {
  603. /* get processor flags from MSR 0x17 */
  604. rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  605. csig->pf = 1 << ((val[1] >> 18) & 7);
  606. }
  607. csig->rev = c->microcode;
  608. /* No extra locking on prev, races are harmless. */
  609. if (csig->sig != prev.sig || csig->pf != prev.pf || csig->rev != prev.rev) {
  610. pr_info("sig=0x%x, pf=0x%x, revision=0x%x\n",
  611. csig->sig, csig->pf, csig->rev);
  612. prev = *csig;
  613. }
  614. return 0;
  615. }
  616. static int apply_microcode_intel(int cpu)
  617. {
  618. struct microcode_intel *mc;
  619. struct ucode_cpu_info *uci;
  620. struct cpuinfo_x86 *c;
  621. unsigned int val[2];
  622. static int prev_rev;
  623. /* We should bind the task to the CPU */
  624. if (WARN_ON(raw_smp_processor_id() != cpu))
  625. return -1;
  626. uci = ucode_cpu_info + cpu;
  627. mc = uci->mc;
  628. if (!mc) {
  629. /* Look for a newer patch in our cache: */
  630. mc = find_patch(uci);
  631. if (!mc)
  632. return 0;
  633. }
  634. /* write microcode via MSR 0x79 */
  635. wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits);
  636. wrmsrl(MSR_IA32_UCODE_REV, 0);
  637. /* As documented in the SDM: Do a CPUID 1 here */
  638. sync_core();
  639. /* get the current revision from MSR 0x8B */
  640. rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  641. if (val[1] != mc->hdr.rev) {
  642. pr_err("CPU%d update to revision 0x%x failed\n",
  643. cpu, mc->hdr.rev);
  644. return -1;
  645. }
  646. if (val[1] != prev_rev) {
  647. pr_info("updated to revision 0x%x, date = %04x-%02x-%02x\n",
  648. val[1],
  649. mc->hdr.date & 0xffff,
  650. mc->hdr.date >> 24,
  651. (mc->hdr.date >> 16) & 0xff);
  652. prev_rev = val[1];
  653. }
  654. c = &cpu_data(cpu);
  655. uci->cpu_sig.rev = val[1];
  656. c->microcode = val[1];
  657. return 0;
  658. }
  659. static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size,
  660. int (*get_ucode_data)(void *, const void *, size_t))
  661. {
  662. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  663. u8 *ucode_ptr = data, *new_mc = NULL, *mc = NULL;
  664. int new_rev = uci->cpu_sig.rev;
  665. unsigned int leftover = size;
  666. unsigned int curr_mc_size = 0;
  667. unsigned int csig, cpf;
  668. while (leftover) {
  669. struct microcode_header_intel mc_header;
  670. unsigned int mc_size;
  671. if (leftover < sizeof(mc_header)) {
  672. pr_err("error! Truncated header in microcode data file\n");
  673. break;
  674. }
  675. if (get_ucode_data(&mc_header, ucode_ptr, sizeof(mc_header)))
  676. break;
  677. mc_size = get_totalsize(&mc_header);
  678. if (!mc_size || mc_size > leftover) {
  679. pr_err("error! Bad data in microcode data file\n");
  680. break;
  681. }
  682. /* For performance reasons, reuse mc area when possible */
  683. if (!mc || mc_size > curr_mc_size) {
  684. vfree(mc);
  685. mc = vmalloc(mc_size);
  686. if (!mc)
  687. break;
  688. curr_mc_size = mc_size;
  689. }
  690. if (get_ucode_data(mc, ucode_ptr, mc_size) ||
  691. microcode_sanity_check(mc, 1) < 0) {
  692. break;
  693. }
  694. csig = uci->cpu_sig.sig;
  695. cpf = uci->cpu_sig.pf;
  696. if (has_newer_microcode(mc, csig, cpf, new_rev)) {
  697. vfree(new_mc);
  698. new_rev = mc_header.rev;
  699. new_mc = mc;
  700. mc = NULL; /* trigger new vmalloc */
  701. }
  702. ucode_ptr += mc_size;
  703. leftover -= mc_size;
  704. }
  705. vfree(mc);
  706. if (leftover) {
  707. vfree(new_mc);
  708. return UCODE_ERROR;
  709. }
  710. if (!new_mc)
  711. return UCODE_NFOUND;
  712. vfree(uci->mc);
  713. uci->mc = (struct microcode_intel *)new_mc;
  714. /*
  715. * If early loading microcode is supported, save this mc into
  716. * permanent memory. So it will be loaded early when a CPU is hot added
  717. * or resumes.
  718. */
  719. save_mc_for_early(new_mc, curr_mc_size);
  720. pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n",
  721. cpu, new_rev, uci->cpu_sig.rev);
  722. return UCODE_OK;
  723. }
  724. static int get_ucode_fw(void *to, const void *from, size_t n)
  725. {
  726. memcpy(to, from, n);
  727. return 0;
  728. }
  729. static enum ucode_state request_microcode_fw(int cpu, struct device *device,
  730. bool refresh_fw)
  731. {
  732. char name[30];
  733. struct cpuinfo_x86 *c = &cpu_data(cpu);
  734. const struct firmware *firmware;
  735. enum ucode_state ret;
  736. sprintf(name, "intel-ucode/%02x-%02x-%02x",
  737. c->x86, c->x86_model, c->x86_mask);
  738. if (request_firmware_direct(&firmware, name, device)) {
  739. pr_debug("data file %s load failed\n", name);
  740. return UCODE_NFOUND;
  741. }
  742. ret = generic_load_microcode(cpu, (void *)firmware->data,
  743. firmware->size, &get_ucode_fw);
  744. release_firmware(firmware);
  745. return ret;
  746. }
  747. static int get_ucode_user(void *to, const void *from, size_t n)
  748. {
  749. return copy_from_user(to, from, n);
  750. }
  751. static enum ucode_state
  752. request_microcode_user(int cpu, const void __user *buf, size_t size)
  753. {
  754. return generic_load_microcode(cpu, (void *)buf, size, &get_ucode_user);
  755. }
  756. static struct microcode_ops microcode_intel_ops = {
  757. .request_microcode_user = request_microcode_user,
  758. .request_microcode_fw = request_microcode_fw,
  759. .collect_cpu_info = collect_cpu_info,
  760. .apply_microcode = apply_microcode_intel,
  761. };
  762. struct microcode_ops * __init init_intel_microcode(void)
  763. {
  764. struct cpuinfo_x86 *c = &boot_cpu_data;
  765. if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
  766. cpu_has(c, X86_FEATURE_IA64)) {
  767. pr_err("Intel CPU family 0x%x not supported\n", c->x86);
  768. return NULL;
  769. }
  770. return &microcode_intel_ops;
  771. }