intel_early.c 19 KB

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