microcode_intel_early.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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 __cpuinit
  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 __cpuinit
  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 __cpuinit
  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 __cpuinit
  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. #define native_rdmsr(msr, val1, val2) \
  313. do { \
  314. u64 __val = native_read_msr((msr)); \
  315. (void)((val1) = (u32)__val); \
  316. (void)((val2) = (u32)(__val >> 32)); \
  317. } while (0)
  318. #define native_wrmsr(msr, low, high) \
  319. native_write_msr(msr, low, high);
  320. static int __cpuinit collect_cpu_info_early(struct ucode_cpu_info *uci)
  321. {
  322. unsigned int val[2];
  323. u8 x86, x86_model;
  324. struct cpu_signature csig;
  325. unsigned int eax, ebx, ecx, edx;
  326. csig.sig = 0;
  327. csig.pf = 0;
  328. csig.rev = 0;
  329. memset(uci, 0, sizeof(*uci));
  330. eax = 0x00000001;
  331. ecx = 0;
  332. native_cpuid(&eax, &ebx, &ecx, &edx);
  333. csig.sig = eax;
  334. x86 = get_x86_family(csig.sig);
  335. x86_model = get_x86_model(csig.sig);
  336. if ((x86_model >= 5) || (x86 > 6)) {
  337. /* get processor flags from MSR 0x17 */
  338. native_rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  339. csig.pf = 1 << ((val[1] >> 18) & 7);
  340. }
  341. native_wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  342. /* As documented in the SDM: Do a CPUID 1 here */
  343. sync_core();
  344. /* get the current revision from MSR 0x8B */
  345. native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  346. csig.rev = val[1];
  347. uci->cpu_sig = csig;
  348. uci->valid = 1;
  349. return 0;
  350. }
  351. #ifdef DEBUG
  352. static void __ref show_saved_mc(void)
  353. {
  354. int i, j;
  355. unsigned int sig, pf, rev, total_size, data_size, date;
  356. struct ucode_cpu_info uci;
  357. if (mc_saved_data.mc_saved_count == 0) {
  358. pr_debug("no micorcode data saved.\n");
  359. return;
  360. }
  361. pr_debug("Total microcode saved: %d\n", mc_saved_data.mc_saved_count);
  362. collect_cpu_info_early(&uci);
  363. sig = uci.cpu_sig.sig;
  364. pf = uci.cpu_sig.pf;
  365. rev = uci.cpu_sig.rev;
  366. pr_debug("CPU%d: sig=0x%x, pf=0x%x, rev=0x%x\n",
  367. smp_processor_id(), sig, pf, rev);
  368. for (i = 0; i < mc_saved_data.mc_saved_count; i++) {
  369. struct microcode_header_intel *mc_saved_header;
  370. struct extended_sigtable *ext_header;
  371. int ext_sigcount;
  372. struct extended_signature *ext_sig;
  373. mc_saved_header = (struct microcode_header_intel *)
  374. mc_saved_data.mc_saved[i];
  375. sig = mc_saved_header->sig;
  376. pf = mc_saved_header->pf;
  377. rev = mc_saved_header->rev;
  378. total_size = get_totalsize(mc_saved_header);
  379. data_size = get_datasize(mc_saved_header);
  380. date = mc_saved_header->date;
  381. pr_debug("mc_saved[%d]: sig=0x%x, pf=0x%x, rev=0x%x, toal size=0x%x, date = %04x-%02x-%02x\n",
  382. i, sig, pf, rev, total_size,
  383. date & 0xffff,
  384. date >> 24,
  385. (date >> 16) & 0xff);
  386. /* Look for ext. headers: */
  387. if (total_size <= data_size + MC_HEADER_SIZE)
  388. continue;
  389. ext_header = (struct extended_sigtable *)
  390. mc_saved_header + data_size + MC_HEADER_SIZE;
  391. ext_sigcount = ext_header->count;
  392. ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
  393. for (j = 0; j < ext_sigcount; j++) {
  394. sig = ext_sig->sig;
  395. pf = ext_sig->pf;
  396. pr_debug("\tExtended[%d]: sig=0x%x, pf=0x%x\n",
  397. j, sig, pf);
  398. ext_sig++;
  399. }
  400. }
  401. }
  402. #else
  403. static inline void show_saved_mc(void)
  404. {
  405. }
  406. #endif
  407. #if defined(CONFIG_MICROCODE_INTEL_EARLY) && defined(CONFIG_HOTPLUG_CPU)
  408. /*
  409. * Save this mc into mc_saved_data. So it will be loaded early when a CPU is
  410. * hot added or resumes.
  411. *
  412. * Please make sure this mc should be a valid microcode patch before calling
  413. * this function.
  414. */
  415. int save_mc_for_early(u8 *mc)
  416. {
  417. struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
  418. unsigned int mc_saved_count_init;
  419. unsigned int mc_saved_count;
  420. struct microcode_intel **mc_saved;
  421. int ret = 0;
  422. int i;
  423. /*
  424. * Hold hotplug lock so mc_saved_data is not accessed by a CPU in
  425. * hotplug.
  426. */
  427. cpu_hotplug_driver_lock();
  428. mc_saved_count_init = mc_saved_data.mc_saved_count;
  429. mc_saved_count = mc_saved_data.mc_saved_count;
  430. mc_saved = mc_saved_data.mc_saved;
  431. if (mc_saved && mc_saved_count)
  432. memcpy(mc_saved_tmp, mc_saved,
  433. mc_saved_count * sizeof(struct mirocode_intel *));
  434. /*
  435. * Save the microcode patch mc in mc_save_tmp structure if it's a newer
  436. * version.
  437. */
  438. _save_mc(mc_saved_tmp, mc, &mc_saved_count);
  439. /*
  440. * Save the mc_save_tmp in global mc_saved_data.
  441. */
  442. ret = save_microcode(&mc_saved_data, mc_saved_tmp, mc_saved_count);
  443. if (ret) {
  444. pr_err("Can not save microcode patch.\n");
  445. goto out;
  446. }
  447. show_saved_mc();
  448. /*
  449. * Free old saved microcod data.
  450. */
  451. if (mc_saved) {
  452. for (i = 0; i < mc_saved_count_init; i++)
  453. kfree(mc_saved[i]);
  454. kfree(mc_saved);
  455. }
  456. out:
  457. cpu_hotplug_driver_unlock();
  458. return ret;
  459. }
  460. EXPORT_SYMBOL_GPL(save_mc_for_early);
  461. #endif
  462. static __initdata char ucode_name[] = "kernel/x86/microcode/GenuineIntel.bin";
  463. static __init enum ucode_state
  464. scan_microcode(unsigned long start, unsigned long end,
  465. struct mc_saved_data *mc_saved_data,
  466. unsigned long *mc_saved_in_initrd,
  467. struct ucode_cpu_info *uci)
  468. {
  469. unsigned int size = end - start + 1;
  470. struct cpio_data cd;
  471. long offset = 0;
  472. #ifdef CONFIG_X86_32
  473. char *p = (char *)__pa_nodebug(ucode_name);
  474. #else
  475. char *p = ucode_name;
  476. #endif
  477. cd.data = NULL;
  478. cd.size = 0;
  479. cd = find_cpio_data(p, (void *)start, size, &offset);
  480. if (!cd.data)
  481. return UCODE_ERROR;
  482. return get_matching_model_microcode(0, start, cd.data, cd.size,
  483. mc_saved_data, mc_saved_in_initrd,
  484. uci);
  485. }
  486. /*
  487. * Print ucode update info.
  488. */
  489. static void __cpuinit
  490. print_ucode_info(struct ucode_cpu_info *uci, unsigned int date)
  491. {
  492. int cpu = smp_processor_id();
  493. pr_info("CPU%d microcode updated early to revision 0x%x, date = %04x-%02x-%02x\n",
  494. cpu,
  495. uci->cpu_sig.rev,
  496. date & 0xffff,
  497. date >> 24,
  498. (date >> 16) & 0xff);
  499. }
  500. #ifdef CONFIG_X86_32
  501. static int delay_ucode_info;
  502. static int current_mc_date;
  503. /*
  504. * Print early updated ucode info after printk works. This is delayed info dump.
  505. */
  506. void __cpuinit show_ucode_info_early(void)
  507. {
  508. struct ucode_cpu_info uci;
  509. if (delay_ucode_info) {
  510. collect_cpu_info_early(&uci);
  511. print_ucode_info(&uci, current_mc_date);
  512. delay_ucode_info = 0;
  513. }
  514. }
  515. /*
  516. * At this point, we can not call printk() yet. Keep microcode patch number in
  517. * mc_saved_data.mc_saved and delay printing microcode info in
  518. * show_ucode_info_early() until printk() works.
  519. */
  520. static void __cpuinit print_ucode(struct ucode_cpu_info *uci)
  521. {
  522. struct microcode_intel *mc_intel;
  523. int *delay_ucode_info_p;
  524. int *current_mc_date_p;
  525. mc_intel = uci->mc;
  526. if (mc_intel == NULL)
  527. return;
  528. delay_ucode_info_p = (int *)__pa_nodebug(&delay_ucode_info);
  529. current_mc_date_p = (int *)__pa_nodebug(&current_mc_date);
  530. *delay_ucode_info_p = 1;
  531. *current_mc_date_p = mc_intel->hdr.date;
  532. }
  533. #else
  534. /*
  535. * Flush global tlb. We only do this in x86_64 where paging has been enabled
  536. * already and PGE should be enabled as well.
  537. */
  538. static inline void __cpuinit flush_tlb_early(void)
  539. {
  540. __native_flush_tlb_global_irq_disabled();
  541. }
  542. static inline void __cpuinit print_ucode(struct ucode_cpu_info *uci)
  543. {
  544. struct microcode_intel *mc_intel;
  545. mc_intel = uci->mc;
  546. if (mc_intel == NULL)
  547. return;
  548. print_ucode_info(uci, mc_intel->hdr.date);
  549. }
  550. #endif
  551. static int __cpuinit apply_microcode_early(struct mc_saved_data *mc_saved_data,
  552. struct ucode_cpu_info *uci)
  553. {
  554. struct microcode_intel *mc_intel;
  555. unsigned int val[2];
  556. mc_intel = uci->mc;
  557. if (mc_intel == NULL)
  558. return 0;
  559. /* write microcode via MSR 0x79 */
  560. native_wrmsr(MSR_IA32_UCODE_WRITE,
  561. (unsigned long) mc_intel->bits,
  562. (unsigned long) mc_intel->bits >> 16 >> 16);
  563. native_wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  564. /* As documented in the SDM: Do a CPUID 1 here */
  565. sync_core();
  566. /* get the current revision from MSR 0x8B */
  567. native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  568. if (val[1] != mc_intel->hdr.rev)
  569. return -1;
  570. #ifdef CONFIG_X86_64
  571. /* Flush global tlb. This is precaution. */
  572. flush_tlb_early();
  573. #endif
  574. uci->cpu_sig.rev = val[1];
  575. print_ucode(uci);
  576. return 0;
  577. }
  578. /*
  579. * This function converts microcode patch offsets previously stored in
  580. * mc_saved_in_initrd to pointers and stores the pointers in mc_saved_data.
  581. */
  582. int __init save_microcode_in_initrd(void)
  583. {
  584. unsigned int count = mc_saved_data.mc_saved_count;
  585. struct microcode_intel *mc_saved[MAX_UCODE_COUNT];
  586. int ret = 0;
  587. if (count == 0)
  588. return ret;
  589. microcode_pointer(mc_saved, mc_saved_in_initrd, initrd_start, count);
  590. ret = save_microcode(&mc_saved_data, mc_saved, count);
  591. if (ret)
  592. pr_err("Can not save microcod patches from initrd");
  593. show_saved_mc();
  594. return ret;
  595. }
  596. static void __init
  597. _load_ucode_intel_bsp(struct mc_saved_data *mc_saved_data,
  598. unsigned long *mc_saved_in_initrd,
  599. unsigned long initrd_start_early,
  600. unsigned long initrd_end_early,
  601. struct ucode_cpu_info *uci)
  602. {
  603. collect_cpu_info_early(uci);
  604. scan_microcode(initrd_start_early, initrd_end_early, mc_saved_data,
  605. mc_saved_in_initrd, uci);
  606. load_microcode(mc_saved_data, mc_saved_in_initrd,
  607. initrd_start_early, uci);
  608. apply_microcode_early(mc_saved_data, uci);
  609. }
  610. void __init
  611. load_ucode_intel_bsp(void)
  612. {
  613. u64 ramdisk_image, ramdisk_size;
  614. unsigned long initrd_start_early, initrd_end_early;
  615. struct ucode_cpu_info uci;
  616. #ifdef CONFIG_X86_32
  617. struct boot_params *boot_params_p;
  618. boot_params_p = (struct boot_params *)__pa_nodebug(&boot_params);
  619. ramdisk_image = boot_params_p->hdr.ramdisk_image;
  620. ramdisk_size = boot_params_p->hdr.ramdisk_size;
  621. initrd_start_early = ramdisk_image;
  622. initrd_end_early = initrd_start_early + ramdisk_size;
  623. _load_ucode_intel_bsp(
  624. (struct mc_saved_data *)__pa_nodebug(&mc_saved_data),
  625. (unsigned long *)__pa_nodebug(&mc_saved_in_initrd),
  626. initrd_start_early, initrd_end_early, &uci);
  627. #else
  628. ramdisk_image = boot_params.hdr.ramdisk_image;
  629. ramdisk_size = boot_params.hdr.ramdisk_size;
  630. initrd_start_early = ramdisk_image + PAGE_OFFSET;
  631. initrd_end_early = initrd_start_early + ramdisk_size;
  632. _load_ucode_intel_bsp(&mc_saved_data, mc_saved_in_initrd,
  633. initrd_start_early, initrd_end_early, &uci);
  634. #endif
  635. }
  636. void __cpuinit load_ucode_intel_ap(void)
  637. {
  638. struct mc_saved_data *mc_saved_data_p;
  639. struct ucode_cpu_info uci;
  640. unsigned long *mc_saved_in_initrd_p;
  641. unsigned long initrd_start_addr;
  642. #ifdef CONFIG_X86_32
  643. unsigned long *initrd_start_p;
  644. mc_saved_in_initrd_p =
  645. (unsigned long *)__pa_nodebug(mc_saved_in_initrd);
  646. mc_saved_data_p = (struct mc_saved_data *)__pa_nodebug(&mc_saved_data);
  647. initrd_start_p = (unsigned long *)__pa_nodebug(&initrd_start);
  648. initrd_start_addr = (unsigned long)__pa_nodebug(*initrd_start_p);
  649. #else
  650. mc_saved_data_p = &mc_saved_data;
  651. mc_saved_in_initrd_p = mc_saved_in_initrd;
  652. initrd_start_addr = initrd_start;
  653. #endif
  654. /*
  655. * If there is no valid ucode previously saved in memory, no need to
  656. * update ucode on this AP.
  657. */
  658. if (mc_saved_data_p->mc_saved_count == 0)
  659. return;
  660. collect_cpu_info_early(&uci);
  661. load_microcode(mc_saved_data_p, mc_saved_in_initrd_p,
  662. initrd_start_addr, &uci);
  663. apply_microcode_early(mc_saved_data_p, &uci);
  664. }