intel_early.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. /*
  2. * Intel CPU microcode early update for Linux
  3. *
  4. * Copyright (C) 2012 Fenghua Yu <fenghua.yu@intel.com>
  5. * H Peter Anvin" <hpa@zytor.com>
  6. *
  7. * This allows to early 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 9.11 of Volume 3, IA-32 Intel Architecture
  12. * Software Developer's Manual.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version
  17. * 2 of the License, or (at your option) any later version.
  18. */
  19. /*
  20. * This needs to be before all headers so that pr_debug in printk.h doesn't turn
  21. * printk calls into no_printk().
  22. *
  23. *#define DEBUG
  24. */
  25. #include <linux/module.h>
  26. #include <linux/mm.h>
  27. #include <linux/slab.h>
  28. #include <linux/earlycpio.h>
  29. #include <linux/initrd.h>
  30. #include <linux/cpu.h>
  31. #include <asm/msr.h>
  32. #include <asm/microcode_intel.h>
  33. #include <asm/processor.h>
  34. #include <asm/tlbflush.h>
  35. #include <asm/setup.h>
  36. #undef pr_fmt
  37. #define pr_fmt(fmt) "microcode: " fmt
  38. static unsigned long mc_saved_in_initrd[MAX_UCODE_COUNT];
  39. static struct mc_saved_data {
  40. unsigned int mc_saved_count;
  41. struct microcode_intel **mc_saved;
  42. } mc_saved_data;
  43. static enum ucode_state
  44. load_microcode_early(struct microcode_intel **saved,
  45. unsigned int num_saved, struct ucode_cpu_info *uci)
  46. {
  47. struct microcode_intel *ucode_ptr, *new_mc = NULL;
  48. struct microcode_header_intel *mc_hdr;
  49. int new_rev, ret, i;
  50. new_rev = uci->cpu_sig.rev;
  51. for (i = 0; i < num_saved; i++) {
  52. ucode_ptr = saved[i];
  53. mc_hdr = (struct microcode_header_intel *)ucode_ptr;
  54. ret = has_newer_microcode(ucode_ptr,
  55. uci->cpu_sig.sig,
  56. uci->cpu_sig.pf,
  57. new_rev);
  58. if (!ret)
  59. continue;
  60. new_rev = mc_hdr->rev;
  61. new_mc = ucode_ptr;
  62. }
  63. if (!new_mc)
  64. return UCODE_NFOUND;
  65. uci->mc = (struct microcode_intel *)new_mc;
  66. return UCODE_OK;
  67. }
  68. static inline void
  69. copy_initrd_ptrs(struct microcode_intel **mc_saved, unsigned long *initrd,
  70. unsigned long off, int num_saved)
  71. {
  72. int i;
  73. for (i = 0; i < num_saved; i++)
  74. mc_saved[i] = (struct microcode_intel *)(initrd[i] + off);
  75. }
  76. #ifdef CONFIG_X86_32
  77. static void
  78. microcode_phys(struct microcode_intel **mc_saved_tmp,
  79. struct mc_saved_data *mc_saved_data)
  80. {
  81. int i;
  82. struct microcode_intel ***mc_saved;
  83. mc_saved = (struct microcode_intel ***)
  84. __pa_nodebug(&mc_saved_data->mc_saved);
  85. for (i = 0; i < mc_saved_data->mc_saved_count; i++) {
  86. struct microcode_intel *p;
  87. p = *(struct microcode_intel **)
  88. __pa_nodebug(mc_saved_data->mc_saved + i);
  89. mc_saved_tmp[i] = (struct microcode_intel *)__pa_nodebug(p);
  90. }
  91. }
  92. #endif
  93. static enum ucode_state
  94. load_microcode(struct mc_saved_data *mc_saved_data, unsigned long *initrd,
  95. unsigned long initrd_start, struct ucode_cpu_info *uci)
  96. {
  97. struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
  98. unsigned int count = mc_saved_data->mc_saved_count;
  99. if (!mc_saved_data->mc_saved) {
  100. copy_initrd_ptrs(mc_saved_tmp, initrd, initrd_start, count);
  101. return load_microcode_early(mc_saved_tmp, count, uci);
  102. } else {
  103. #ifdef CONFIG_X86_32
  104. microcode_phys(mc_saved_tmp, mc_saved_data);
  105. return load_microcode_early(mc_saved_tmp, count, uci);
  106. #else
  107. return load_microcode_early(mc_saved_data->mc_saved,
  108. count, uci);
  109. #endif
  110. }
  111. }
  112. /*
  113. * Given CPU signature and a microcode patch, this function finds if the
  114. * microcode patch has matching family and model with the CPU.
  115. */
  116. static enum ucode_state
  117. matching_model_microcode(struct microcode_header_intel *mc_header,
  118. unsigned long sig)
  119. {
  120. unsigned int fam, model;
  121. unsigned int fam_ucode, model_ucode;
  122. struct extended_sigtable *ext_header;
  123. unsigned long total_size = get_totalsize(mc_header);
  124. unsigned long data_size = get_datasize(mc_header);
  125. int ext_sigcount, i;
  126. struct extended_signature *ext_sig;
  127. fam = __x86_family(sig);
  128. model = x86_model(sig);
  129. fam_ucode = __x86_family(mc_header->sig);
  130. model_ucode = x86_model(mc_header->sig);
  131. if (fam == fam_ucode && model == model_ucode)
  132. return UCODE_OK;
  133. /* Look for ext. headers: */
  134. if (total_size <= data_size + MC_HEADER_SIZE)
  135. return UCODE_NFOUND;
  136. ext_header = (void *) mc_header + data_size + MC_HEADER_SIZE;
  137. ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
  138. ext_sigcount = ext_header->count;
  139. for (i = 0; i < ext_sigcount; i++) {
  140. fam_ucode = __x86_family(ext_sig->sig);
  141. model_ucode = x86_model(ext_sig->sig);
  142. if (fam == fam_ucode && model == model_ucode)
  143. return UCODE_OK;
  144. ext_sig++;
  145. }
  146. return UCODE_NFOUND;
  147. }
  148. static int
  149. save_microcode(struct mc_saved_data *mc_saved_data,
  150. struct microcode_intel **mc_saved_src,
  151. unsigned int mc_saved_count)
  152. {
  153. int i, j;
  154. struct microcode_intel **saved_ptr;
  155. int ret;
  156. if (!mc_saved_count)
  157. return -EINVAL;
  158. /*
  159. * Copy new microcode data.
  160. */
  161. saved_ptr = kcalloc(mc_saved_count, sizeof(struct microcode_intel *), GFP_KERNEL);
  162. if (!saved_ptr)
  163. return -ENOMEM;
  164. for (i = 0; i < mc_saved_count; i++) {
  165. struct microcode_header_intel *mc_hdr;
  166. struct microcode_intel *mc;
  167. unsigned long size;
  168. if (!mc_saved_src[i]) {
  169. ret = -EINVAL;
  170. goto err;
  171. }
  172. mc = mc_saved_src[i];
  173. mc_hdr = &mc->hdr;
  174. size = get_totalsize(mc_hdr);
  175. saved_ptr[i] = kmalloc(size, GFP_KERNEL);
  176. if (!saved_ptr[i]) {
  177. ret = -ENOMEM;
  178. goto err;
  179. }
  180. memcpy(saved_ptr[i], mc, size);
  181. }
  182. /*
  183. * Point to newly saved microcode.
  184. */
  185. mc_saved_data->mc_saved = saved_ptr;
  186. mc_saved_data->mc_saved_count = mc_saved_count;
  187. return 0;
  188. err:
  189. for (j = 0; j <= i; j++)
  190. kfree(saved_ptr[j]);
  191. kfree(saved_ptr);
  192. return ret;
  193. }
  194. /*
  195. * A microcode patch in ucode_ptr is saved into mc_saved
  196. * - if it has matching signature and newer revision compared to an existing
  197. * patch mc_saved.
  198. * - or if it is a newly discovered microcode patch.
  199. *
  200. * The microcode patch should have matching model with CPU.
  201. *
  202. * Returns: The updated number @num_saved of saved microcode patches.
  203. */
  204. static unsigned int _save_mc(struct microcode_intel **mc_saved,
  205. u8 *ucode_ptr, unsigned int num_saved)
  206. {
  207. struct microcode_header_intel *mc_hdr, *mc_saved_hdr;
  208. unsigned int sig, pf;
  209. int found = 0, i;
  210. mc_hdr = (struct microcode_header_intel *)ucode_ptr;
  211. for (i = 0; i < num_saved; i++) {
  212. mc_saved_hdr = (struct microcode_header_intel *)mc_saved[i];
  213. sig = mc_saved_hdr->sig;
  214. pf = mc_saved_hdr->pf;
  215. if (!find_matching_signature(ucode_ptr, sig, pf))
  216. continue;
  217. found = 1;
  218. if (mc_hdr->rev <= mc_saved_hdr->rev)
  219. continue;
  220. /*
  221. * Found an older ucode saved earlier. Replace it with
  222. * this newer one.
  223. */
  224. mc_saved[i] = (struct microcode_intel *)ucode_ptr;
  225. break;
  226. }
  227. /* Newly detected microcode, save it to memory. */
  228. if (i >= num_saved && !found)
  229. mc_saved[num_saved++] = (struct microcode_intel *)ucode_ptr;
  230. return num_saved;
  231. }
  232. /*
  233. * Get microcode matching with BSP's model. Only CPUs with the same model as
  234. * BSP can stay in the platform.
  235. */
  236. static enum ucode_state __init
  237. get_matching_model_microcode(int cpu, unsigned long start,
  238. void *data, size_t size,
  239. struct mc_saved_data *mc_saved_data,
  240. unsigned long *mc_saved_in_initrd,
  241. struct ucode_cpu_info *uci)
  242. {
  243. u8 *ucode_ptr = data;
  244. unsigned int leftover = size;
  245. enum ucode_state state = UCODE_OK;
  246. unsigned int mc_size;
  247. struct microcode_header_intel *mc_header;
  248. struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
  249. unsigned int mc_saved_count = mc_saved_data->mc_saved_count;
  250. int i;
  251. while (leftover && mc_saved_count < ARRAY_SIZE(mc_saved_tmp)) {
  252. if (leftover < sizeof(mc_header))
  253. break;
  254. mc_header = (struct microcode_header_intel *)ucode_ptr;
  255. mc_size = get_totalsize(mc_header);
  256. if (!mc_size || mc_size > leftover ||
  257. microcode_sanity_check(ucode_ptr, 0) < 0)
  258. break;
  259. leftover -= mc_size;
  260. /*
  261. * Since APs with same family and model as the BSP may boot in
  262. * the platform, we need to find and save microcode patches
  263. * with the same family and model as the BSP.
  264. */
  265. if (matching_model_microcode(mc_header, uci->cpu_sig.sig) !=
  266. UCODE_OK) {
  267. ucode_ptr += mc_size;
  268. continue;
  269. }
  270. mc_saved_count = _save_mc(mc_saved_tmp, ucode_ptr, mc_saved_count);
  271. ucode_ptr += mc_size;
  272. }
  273. if (leftover) {
  274. state = UCODE_ERROR;
  275. goto out;
  276. }
  277. if (mc_saved_count == 0) {
  278. state = UCODE_NFOUND;
  279. goto out;
  280. }
  281. for (i = 0; i < mc_saved_count; i++)
  282. mc_saved_in_initrd[i] = (unsigned long)mc_saved_tmp[i] - start;
  283. mc_saved_data->mc_saved_count = mc_saved_count;
  284. out:
  285. return state;
  286. }
  287. static int collect_cpu_info_early(struct ucode_cpu_info *uci)
  288. {
  289. unsigned int val[2];
  290. unsigned int family, model;
  291. struct cpu_signature csig;
  292. unsigned int eax, ebx, ecx, edx;
  293. csig.sig = 0;
  294. csig.pf = 0;
  295. csig.rev = 0;
  296. memset(uci, 0, sizeof(*uci));
  297. eax = 0x00000001;
  298. ecx = 0;
  299. native_cpuid(&eax, &ebx, &ecx, &edx);
  300. csig.sig = eax;
  301. family = __x86_family(csig.sig);
  302. model = x86_model(csig.sig);
  303. if ((model >= 5) || (family > 6)) {
  304. /* get processor flags from MSR 0x17 */
  305. native_rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  306. csig.pf = 1 << ((val[1] >> 18) & 7);
  307. }
  308. native_wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  309. /* As documented in the SDM: Do a CPUID 1 here */
  310. sync_core();
  311. /* get the current revision from MSR 0x8B */
  312. native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  313. csig.rev = val[1];
  314. uci->cpu_sig = csig;
  315. uci->valid = 1;
  316. return 0;
  317. }
  318. #ifdef DEBUG
  319. static void show_saved_mc(void)
  320. {
  321. int i, j;
  322. unsigned int sig, pf, rev, total_size, data_size, date;
  323. struct ucode_cpu_info uci;
  324. if (mc_saved_data.mc_saved_count == 0) {
  325. pr_debug("no microcode data saved.\n");
  326. return;
  327. }
  328. pr_debug("Total microcode saved: %d\n", mc_saved_data.mc_saved_count);
  329. collect_cpu_info_early(&uci);
  330. sig = uci.cpu_sig.sig;
  331. pf = uci.cpu_sig.pf;
  332. rev = uci.cpu_sig.rev;
  333. pr_debug("CPU: sig=0x%x, pf=0x%x, rev=0x%x\n", sig, pf, rev);
  334. for (i = 0; i < mc_saved_data.mc_saved_count; i++) {
  335. struct microcode_header_intel *mc_saved_header;
  336. struct extended_sigtable *ext_header;
  337. int ext_sigcount;
  338. struct extended_signature *ext_sig;
  339. mc_saved_header = (struct microcode_header_intel *)
  340. mc_saved_data.mc_saved[i];
  341. sig = mc_saved_header->sig;
  342. pf = mc_saved_header->pf;
  343. rev = mc_saved_header->rev;
  344. total_size = get_totalsize(mc_saved_header);
  345. data_size = get_datasize(mc_saved_header);
  346. date = mc_saved_header->date;
  347. pr_debug("mc_saved[%d]: sig=0x%x, pf=0x%x, rev=0x%x, toal size=0x%x, date = %04x-%02x-%02x\n",
  348. i, sig, pf, rev, total_size,
  349. date & 0xffff,
  350. date >> 24,
  351. (date >> 16) & 0xff);
  352. /* Look for ext. headers: */
  353. if (total_size <= data_size + MC_HEADER_SIZE)
  354. continue;
  355. ext_header = (void *) mc_saved_header + data_size + MC_HEADER_SIZE;
  356. ext_sigcount = ext_header->count;
  357. ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
  358. for (j = 0; j < ext_sigcount; j++) {
  359. sig = ext_sig->sig;
  360. pf = ext_sig->pf;
  361. pr_debug("\tExtended[%d]: sig=0x%x, pf=0x%x\n",
  362. j, sig, pf);
  363. ext_sig++;
  364. }
  365. }
  366. }
  367. #else
  368. static inline void show_saved_mc(void)
  369. {
  370. }
  371. #endif
  372. #if defined(CONFIG_MICROCODE_INTEL_EARLY) && defined(CONFIG_HOTPLUG_CPU)
  373. static DEFINE_MUTEX(x86_cpu_microcode_mutex);
  374. /*
  375. * Save this mc into mc_saved_data. So it will be loaded early when a CPU is
  376. * hot added or resumes.
  377. *
  378. * Please make sure this mc should be a valid microcode patch before calling
  379. * this function.
  380. */
  381. int save_mc_for_early(u8 *mc)
  382. {
  383. struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
  384. unsigned int mc_saved_count_init;
  385. unsigned int mc_saved_count;
  386. struct microcode_intel **mc_saved;
  387. int ret = 0;
  388. int i;
  389. /*
  390. * Hold hotplug lock so mc_saved_data is not accessed by a CPU in
  391. * hotplug.
  392. */
  393. mutex_lock(&x86_cpu_microcode_mutex);
  394. mc_saved_count_init = mc_saved_data.mc_saved_count;
  395. mc_saved_count = mc_saved_data.mc_saved_count;
  396. mc_saved = mc_saved_data.mc_saved;
  397. if (mc_saved && mc_saved_count)
  398. memcpy(mc_saved_tmp, mc_saved,
  399. mc_saved_count * sizeof(struct microcode_intel *));
  400. /*
  401. * Save the microcode patch mc in mc_save_tmp structure if it's a newer
  402. * version.
  403. */
  404. mc_saved_count = _save_mc(mc_saved_tmp, mc, mc_saved_count);
  405. /*
  406. * Save the mc_save_tmp in global mc_saved_data.
  407. */
  408. ret = save_microcode(&mc_saved_data, mc_saved_tmp, mc_saved_count);
  409. if (ret) {
  410. pr_err("Cannot save microcode patch.\n");
  411. goto out;
  412. }
  413. show_saved_mc();
  414. /*
  415. * Free old saved microcode data.
  416. */
  417. if (mc_saved) {
  418. for (i = 0; i < mc_saved_count_init; i++)
  419. kfree(mc_saved[i]);
  420. kfree(mc_saved);
  421. }
  422. out:
  423. mutex_unlock(&x86_cpu_microcode_mutex);
  424. return ret;
  425. }
  426. EXPORT_SYMBOL_GPL(save_mc_for_early);
  427. #endif
  428. static bool __init load_builtin_intel_microcode(struct cpio_data *cp)
  429. {
  430. #ifdef CONFIG_X86_64
  431. unsigned int eax = 0x00000001, ebx, ecx = 0, edx;
  432. unsigned int family, model, stepping;
  433. char name[30];
  434. native_cpuid(&eax, &ebx, &ecx, &edx);
  435. family = __x86_family(eax);
  436. model = x86_model(eax);
  437. stepping = eax & 0xf;
  438. sprintf(name, "intel-ucode/%02x-%02x-%02x", family, model, stepping);
  439. return get_builtin_firmware(cp, name);
  440. #else
  441. return false;
  442. #endif
  443. }
  444. static __initdata char ucode_name[] = "kernel/x86/microcode/GenuineIntel.bin";
  445. static __init enum ucode_state
  446. scan_microcode(struct mc_saved_data *mc_saved_data, unsigned long *initrd,
  447. unsigned long start, unsigned long size,
  448. struct ucode_cpu_info *uci)
  449. {
  450. struct cpio_data cd;
  451. long offset = 0;
  452. #ifdef CONFIG_X86_32
  453. char *p = (char *)__pa_nodebug(ucode_name);
  454. #else
  455. char *p = ucode_name;
  456. #endif
  457. cd.data = NULL;
  458. cd.size = 0;
  459. cd = find_cpio_data(p, (void *)start, size, &offset);
  460. if (!cd.data) {
  461. if (!load_builtin_intel_microcode(&cd))
  462. return UCODE_ERROR;
  463. }
  464. return get_matching_model_microcode(0, start, cd.data, cd.size,
  465. mc_saved_data, initrd, uci);
  466. }
  467. /*
  468. * Print ucode update info.
  469. */
  470. static void
  471. print_ucode_info(struct ucode_cpu_info *uci, unsigned int date)
  472. {
  473. int cpu = smp_processor_id();
  474. pr_info("CPU%d microcode updated early to revision 0x%x, date = %04x-%02x-%02x\n",
  475. cpu,
  476. uci->cpu_sig.rev,
  477. date & 0xffff,
  478. date >> 24,
  479. (date >> 16) & 0xff);
  480. }
  481. #ifdef CONFIG_X86_32
  482. static int delay_ucode_info;
  483. static int current_mc_date;
  484. /*
  485. * Print early updated ucode info after printk works. This is delayed info dump.
  486. */
  487. void show_ucode_info_early(void)
  488. {
  489. struct ucode_cpu_info uci;
  490. if (delay_ucode_info) {
  491. collect_cpu_info_early(&uci);
  492. print_ucode_info(&uci, current_mc_date);
  493. delay_ucode_info = 0;
  494. }
  495. }
  496. /*
  497. * At this point, we can not call printk() yet. Keep microcode patch number in
  498. * mc_saved_data.mc_saved and delay printing microcode info in
  499. * show_ucode_info_early() until printk() works.
  500. */
  501. static void print_ucode(struct ucode_cpu_info *uci)
  502. {
  503. struct microcode_intel *mc_intel;
  504. int *delay_ucode_info_p;
  505. int *current_mc_date_p;
  506. mc_intel = uci->mc;
  507. if (mc_intel == NULL)
  508. return;
  509. delay_ucode_info_p = (int *)__pa_nodebug(&delay_ucode_info);
  510. current_mc_date_p = (int *)__pa_nodebug(&current_mc_date);
  511. *delay_ucode_info_p = 1;
  512. *current_mc_date_p = mc_intel->hdr.date;
  513. }
  514. #else
  515. /*
  516. * Flush global tlb. We only do this in x86_64 where paging has been enabled
  517. * already and PGE should be enabled as well.
  518. */
  519. static inline void flush_tlb_early(void)
  520. {
  521. __native_flush_tlb_global_irq_disabled();
  522. }
  523. static inline void print_ucode(struct ucode_cpu_info *uci)
  524. {
  525. struct microcode_intel *mc_intel;
  526. mc_intel = uci->mc;
  527. if (mc_intel == NULL)
  528. return;
  529. print_ucode_info(uci, mc_intel->hdr.date);
  530. }
  531. #endif
  532. static int apply_microcode_early(struct ucode_cpu_info *uci, bool early)
  533. {
  534. struct microcode_intel *mc_intel;
  535. unsigned int val[2];
  536. mc_intel = uci->mc;
  537. if (mc_intel == NULL)
  538. return 0;
  539. /* write microcode via MSR 0x79 */
  540. native_wrmsr(MSR_IA32_UCODE_WRITE,
  541. (unsigned long) mc_intel->bits,
  542. (unsigned long) mc_intel->bits >> 16 >> 16);
  543. native_wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  544. /* As documented in the SDM: Do a CPUID 1 here */
  545. sync_core();
  546. /* get the current revision from MSR 0x8B */
  547. native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  548. if (val[1] != mc_intel->hdr.rev)
  549. return -1;
  550. #ifdef CONFIG_X86_64
  551. /* Flush global tlb. This is precaution. */
  552. flush_tlb_early();
  553. #endif
  554. uci->cpu_sig.rev = val[1];
  555. if (early)
  556. print_ucode(uci);
  557. else
  558. print_ucode_info(uci, mc_intel->hdr.date);
  559. return 0;
  560. }
  561. /*
  562. * This function converts microcode patch offsets previously stored in
  563. * mc_saved_in_initrd to pointers and stores the pointers in mc_saved_data.
  564. */
  565. int __init save_microcode_in_initrd_intel(void)
  566. {
  567. unsigned int count = mc_saved_data.mc_saved_count;
  568. struct microcode_intel *mc_saved[MAX_UCODE_COUNT];
  569. int ret = 0;
  570. if (count == 0)
  571. return ret;
  572. copy_initrd_ptrs(mc_saved, mc_saved_in_initrd, initrd_start, count);
  573. ret = save_microcode(&mc_saved_data, mc_saved, count);
  574. if (ret)
  575. pr_err("Cannot save microcode patches from initrd.\n");
  576. show_saved_mc();
  577. return ret;
  578. }
  579. static void __init
  580. _load_ucode_intel_bsp(struct mc_saved_data *mc_saved_data,
  581. unsigned long *initrd,
  582. unsigned long start, unsigned long size)
  583. {
  584. struct ucode_cpu_info uci;
  585. enum ucode_state ret;
  586. collect_cpu_info_early(&uci);
  587. ret = scan_microcode(mc_saved_data, initrd, start, size, &uci);
  588. if (ret != UCODE_OK)
  589. return;
  590. ret = load_microcode(mc_saved_data, initrd, start, &uci);
  591. if (ret != UCODE_OK)
  592. return;
  593. apply_microcode_early(&uci, true);
  594. }
  595. void __init load_ucode_intel_bsp(void)
  596. {
  597. u64 start, size;
  598. #ifdef CONFIG_X86_32
  599. struct boot_params *p;
  600. p = (struct boot_params *)__pa_nodebug(&boot_params);
  601. start = p->hdr.ramdisk_image;
  602. size = p->hdr.ramdisk_size;
  603. _load_ucode_intel_bsp(
  604. (struct mc_saved_data *)__pa_nodebug(&mc_saved_data),
  605. (unsigned long *)__pa_nodebug(&mc_saved_in_initrd),
  606. start, size);
  607. #else
  608. start = boot_params.hdr.ramdisk_image + PAGE_OFFSET;
  609. size = boot_params.hdr.ramdisk_size;
  610. _load_ucode_intel_bsp(&mc_saved_data, mc_saved_in_initrd, start, size);
  611. #endif
  612. }
  613. void load_ucode_intel_ap(void)
  614. {
  615. struct mc_saved_data *mc_saved_data_p;
  616. struct ucode_cpu_info uci;
  617. unsigned long *mc_saved_in_initrd_p;
  618. unsigned long initrd_start_addr;
  619. enum ucode_state ret;
  620. #ifdef CONFIG_X86_32
  621. unsigned long *initrd_start_p;
  622. mc_saved_in_initrd_p =
  623. (unsigned long *)__pa_nodebug(mc_saved_in_initrd);
  624. mc_saved_data_p = (struct mc_saved_data *)__pa_nodebug(&mc_saved_data);
  625. initrd_start_p = (unsigned long *)__pa_nodebug(&initrd_start);
  626. initrd_start_addr = (unsigned long)__pa_nodebug(*initrd_start_p);
  627. #else
  628. mc_saved_data_p = &mc_saved_data;
  629. mc_saved_in_initrd_p = mc_saved_in_initrd;
  630. initrd_start_addr = initrd_start;
  631. #endif
  632. /*
  633. * If there is no valid ucode previously saved in memory, no need to
  634. * update ucode on this AP.
  635. */
  636. if (mc_saved_data_p->mc_saved_count == 0)
  637. return;
  638. collect_cpu_info_early(&uci);
  639. ret = load_microcode(mc_saved_data_p, mc_saved_in_initrd_p,
  640. initrd_start_addr, &uci);
  641. if (ret != UCODE_OK)
  642. return;
  643. apply_microcode_early(&uci, true);
  644. }
  645. void reload_ucode_intel(void)
  646. {
  647. struct ucode_cpu_info uci;
  648. enum ucode_state ret;
  649. if (!mc_saved_data.mc_saved_count)
  650. return;
  651. collect_cpu_info_early(&uci);
  652. ret = load_microcode_early(mc_saved_data.mc_saved,
  653. mc_saved_data.mc_saved_count, &uci);
  654. if (ret != UCODE_OK)
  655. return;
  656. apply_microcode_early(&uci, false);
  657. }