intel_early.c 18 KB

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