intel_early.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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 = get_matching_microcode(uci->cpu_sig.sig,
  55. uci->cpu_sig.pf,
  56. new_rev,
  57. ucode_ptr);
  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, new_rev;
  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. new_rev = mc_hdr->rev;
  216. if (!get_matching_sig(sig, pf, new_rev, ucode_ptr))
  217. continue;
  218. found = 1;
  219. if (!revision_is_newer(mc_hdr, new_rev))
  220. continue;
  221. /*
  222. * Found an older ucode saved earlier. Replace it with
  223. * this newer one.
  224. */
  225. mc_saved[i] = (struct microcode_intel *)ucode_ptr;
  226. break;
  227. }
  228. /* Newly detected microcode, save it to memory. */
  229. if (i >= num_saved && !found)
  230. mc_saved[num_saved++] = (struct microcode_intel *)ucode_ptr;
  231. return num_saved;
  232. }
  233. /*
  234. * Get microcode matching with BSP's model. Only CPUs with the same model as
  235. * BSP can stay in the platform.
  236. */
  237. static enum ucode_state __init
  238. get_matching_model_microcode(int cpu, unsigned long start,
  239. void *data, size_t size,
  240. struct mc_saved_data *mc_saved_data,
  241. unsigned long *mc_saved_in_initrd,
  242. struct ucode_cpu_info *uci)
  243. {
  244. u8 *ucode_ptr = data;
  245. unsigned int leftover = size;
  246. enum ucode_state state = UCODE_OK;
  247. unsigned int mc_size;
  248. struct microcode_header_intel *mc_header;
  249. struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
  250. unsigned int mc_saved_count = mc_saved_data->mc_saved_count;
  251. int i;
  252. while (leftover && mc_saved_count < ARRAY_SIZE(mc_saved_tmp)) {
  253. if (leftover < sizeof(mc_header))
  254. break;
  255. mc_header = (struct microcode_header_intel *)ucode_ptr;
  256. mc_size = get_totalsize(mc_header);
  257. if (!mc_size || mc_size > leftover ||
  258. microcode_sanity_check(ucode_ptr, 0) < 0)
  259. break;
  260. leftover -= mc_size;
  261. /*
  262. * Since APs with same family and model as the BSP may boot in
  263. * the platform, we need to find and save microcode patches
  264. * with the same family and model as the BSP.
  265. */
  266. if (matching_model_microcode(mc_header, uci->cpu_sig.sig) !=
  267. UCODE_OK) {
  268. ucode_ptr += mc_size;
  269. continue;
  270. }
  271. mc_saved_count = _save_mc(mc_saved_tmp, ucode_ptr, mc_saved_count);
  272. ucode_ptr += mc_size;
  273. }
  274. if (leftover) {
  275. state = UCODE_ERROR;
  276. goto out;
  277. }
  278. if (mc_saved_count == 0) {
  279. state = UCODE_NFOUND;
  280. goto out;
  281. }
  282. for (i = 0; i < mc_saved_count; i++)
  283. mc_saved_in_initrd[i] = (unsigned long)mc_saved_tmp[i] - start;
  284. mc_saved_data->mc_saved_count = mc_saved_count;
  285. out:
  286. return state;
  287. }
  288. static int collect_cpu_info_early(struct ucode_cpu_info *uci)
  289. {
  290. unsigned int val[2];
  291. unsigned int family, model;
  292. struct cpu_signature csig;
  293. unsigned int eax, ebx, ecx, edx;
  294. csig.sig = 0;
  295. csig.pf = 0;
  296. csig.rev = 0;
  297. memset(uci, 0, sizeof(*uci));
  298. eax = 0x00000001;
  299. ecx = 0;
  300. native_cpuid(&eax, &ebx, &ecx, &edx);
  301. csig.sig = eax;
  302. family = __x86_family(csig.sig);
  303. model = x86_model(csig.sig);
  304. if ((model >= 5) || (family > 6)) {
  305. /* get processor flags from MSR 0x17 */
  306. native_rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  307. csig.pf = 1 << ((val[1] >> 18) & 7);
  308. }
  309. native_wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  310. /* As documented in the SDM: Do a CPUID 1 here */
  311. sync_core();
  312. /* get the current revision from MSR 0x8B */
  313. native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  314. csig.rev = val[1];
  315. uci->cpu_sig = csig;
  316. uci->valid = 1;
  317. return 0;
  318. }
  319. #ifdef DEBUG
  320. static void __ref show_saved_mc(void)
  321. {
  322. int i, j;
  323. unsigned int sig, pf, rev, total_size, data_size, date;
  324. struct ucode_cpu_info uci;
  325. if (mc_saved_data.mc_saved_count == 0) {
  326. pr_debug("no microcode data saved.\n");
  327. return;
  328. }
  329. pr_debug("Total microcode saved: %d\n", mc_saved_data.mc_saved_count);
  330. collect_cpu_info_early(&uci);
  331. sig = uci.cpu_sig.sig;
  332. pf = uci.cpu_sig.pf;
  333. rev = uci.cpu_sig.rev;
  334. pr_debug("CPU: sig=0x%x, pf=0x%x, rev=0x%x\n", sig, pf, rev);
  335. for (i = 0; i < mc_saved_data.mc_saved_count; i++) {
  336. struct microcode_header_intel *mc_saved_header;
  337. struct extended_sigtable *ext_header;
  338. int ext_sigcount;
  339. struct extended_signature *ext_sig;
  340. mc_saved_header = (struct microcode_header_intel *)
  341. mc_saved_data.mc_saved[i];
  342. sig = mc_saved_header->sig;
  343. pf = mc_saved_header->pf;
  344. rev = mc_saved_header->rev;
  345. total_size = get_totalsize(mc_saved_header);
  346. data_size = get_datasize(mc_saved_header);
  347. date = mc_saved_header->date;
  348. pr_debug("mc_saved[%d]: sig=0x%x, pf=0x%x, rev=0x%x, toal size=0x%x, date = %04x-%02x-%02x\n",
  349. i, sig, pf, rev, total_size,
  350. date & 0xffff,
  351. date >> 24,
  352. (date >> 16) & 0xff);
  353. /* Look for ext. headers: */
  354. if (total_size <= data_size + MC_HEADER_SIZE)
  355. continue;
  356. ext_header = (void *) mc_saved_header + data_size + MC_HEADER_SIZE;
  357. ext_sigcount = ext_header->count;
  358. ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
  359. for (j = 0; j < ext_sigcount; j++) {
  360. sig = ext_sig->sig;
  361. pf = ext_sig->pf;
  362. pr_debug("\tExtended[%d]: sig=0x%x, pf=0x%x\n",
  363. j, sig, pf);
  364. ext_sig++;
  365. }
  366. }
  367. }
  368. #else
  369. static inline void show_saved_mc(void)
  370. {
  371. }
  372. #endif
  373. #if defined(CONFIG_MICROCODE_INTEL_EARLY) && defined(CONFIG_HOTPLUG_CPU)
  374. static DEFINE_MUTEX(x86_cpu_microcode_mutex);
  375. /*
  376. * Save this mc into mc_saved_data. So it will be loaded early when a CPU is
  377. * hot added or resumes.
  378. *
  379. * Please make sure this mc should be a valid microcode patch before calling
  380. * this function.
  381. */
  382. int save_mc_for_early(u8 *mc)
  383. {
  384. struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
  385. unsigned int mc_saved_count_init;
  386. unsigned int mc_saved_count;
  387. struct microcode_intel **mc_saved;
  388. int ret = 0;
  389. int i;
  390. /*
  391. * Hold hotplug lock so mc_saved_data is not accessed by a CPU in
  392. * hotplug.
  393. */
  394. mutex_lock(&x86_cpu_microcode_mutex);
  395. mc_saved_count_init = mc_saved_data.mc_saved_count;
  396. mc_saved_count = mc_saved_data.mc_saved_count;
  397. mc_saved = mc_saved_data.mc_saved;
  398. if (mc_saved && mc_saved_count)
  399. memcpy(mc_saved_tmp, mc_saved,
  400. mc_saved_count * sizeof(struct microcode_intel *));
  401. /*
  402. * Save the microcode patch mc in mc_save_tmp structure if it's a newer
  403. * version.
  404. */
  405. mc_saved_count = _save_mc(mc_saved_tmp, mc, mc_saved_count);
  406. /*
  407. * Save the mc_save_tmp in global mc_saved_data.
  408. */
  409. ret = save_microcode(&mc_saved_data, mc_saved_tmp, mc_saved_count);
  410. if (ret) {
  411. pr_err("Cannot save microcode patch.\n");
  412. goto out;
  413. }
  414. show_saved_mc();
  415. /*
  416. * Free old saved microcode data.
  417. */
  418. if (mc_saved) {
  419. for (i = 0; i < mc_saved_count_init; i++)
  420. kfree(mc_saved[i]);
  421. kfree(mc_saved);
  422. }
  423. out:
  424. mutex_unlock(&x86_cpu_microcode_mutex);
  425. return ret;
  426. }
  427. EXPORT_SYMBOL_GPL(save_mc_for_early);
  428. #endif
  429. static __initdata char ucode_name[] = "kernel/x86/microcode/GenuineIntel.bin";
  430. static __init enum ucode_state
  431. scan_microcode(struct mc_saved_data *mc_saved_data, unsigned long *initrd,
  432. unsigned long start, unsigned long size,
  433. struct ucode_cpu_info *uci)
  434. {
  435. struct cpio_data cd;
  436. long offset = 0;
  437. #ifdef CONFIG_X86_32
  438. char *p = (char *)__pa_nodebug(ucode_name);
  439. #else
  440. char *p = ucode_name;
  441. #endif
  442. cd.data = NULL;
  443. cd.size = 0;
  444. cd = find_cpio_data(p, (void *)start, size, &offset);
  445. if (!cd.data)
  446. return UCODE_ERROR;
  447. return get_matching_model_microcode(0, start, cd.data, cd.size,
  448. mc_saved_data, initrd, uci);
  449. }
  450. /*
  451. * Print ucode update info.
  452. */
  453. static void
  454. print_ucode_info(struct ucode_cpu_info *uci, unsigned int date)
  455. {
  456. int cpu = smp_processor_id();
  457. pr_info("CPU%d microcode updated early to revision 0x%x, date = %04x-%02x-%02x\n",
  458. cpu,
  459. uci->cpu_sig.rev,
  460. date & 0xffff,
  461. date >> 24,
  462. (date >> 16) & 0xff);
  463. }
  464. #ifdef CONFIG_X86_32
  465. static int delay_ucode_info;
  466. static int current_mc_date;
  467. /*
  468. * Print early updated ucode info after printk works. This is delayed info dump.
  469. */
  470. void show_ucode_info_early(void)
  471. {
  472. struct ucode_cpu_info uci;
  473. if (delay_ucode_info) {
  474. collect_cpu_info_early(&uci);
  475. print_ucode_info(&uci, current_mc_date);
  476. delay_ucode_info = 0;
  477. }
  478. }
  479. /*
  480. * At this point, we can not call printk() yet. Keep microcode patch number in
  481. * mc_saved_data.mc_saved and delay printing microcode info in
  482. * show_ucode_info_early() until printk() works.
  483. */
  484. static void print_ucode(struct ucode_cpu_info *uci)
  485. {
  486. struct microcode_intel *mc_intel;
  487. int *delay_ucode_info_p;
  488. int *current_mc_date_p;
  489. mc_intel = uci->mc;
  490. if (mc_intel == NULL)
  491. return;
  492. delay_ucode_info_p = (int *)__pa_nodebug(&delay_ucode_info);
  493. current_mc_date_p = (int *)__pa_nodebug(&current_mc_date);
  494. *delay_ucode_info_p = 1;
  495. *current_mc_date_p = mc_intel->hdr.date;
  496. }
  497. #else
  498. /*
  499. * Flush global tlb. We only do this in x86_64 where paging has been enabled
  500. * already and PGE should be enabled as well.
  501. */
  502. static inline void flush_tlb_early(void)
  503. {
  504. __native_flush_tlb_global_irq_disabled();
  505. }
  506. static inline void print_ucode(struct ucode_cpu_info *uci)
  507. {
  508. struct microcode_intel *mc_intel;
  509. mc_intel = uci->mc;
  510. if (mc_intel == NULL)
  511. return;
  512. print_ucode_info(uci, mc_intel->hdr.date);
  513. }
  514. #endif
  515. static int apply_microcode_early(struct ucode_cpu_info *uci, bool early)
  516. {
  517. struct microcode_intel *mc_intel;
  518. unsigned int val[2];
  519. mc_intel = uci->mc;
  520. if (mc_intel == NULL)
  521. return 0;
  522. /* write microcode via MSR 0x79 */
  523. native_wrmsr(MSR_IA32_UCODE_WRITE,
  524. (unsigned long) mc_intel->bits,
  525. (unsigned long) mc_intel->bits >> 16 >> 16);
  526. native_wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  527. /* As documented in the SDM: Do a CPUID 1 here */
  528. sync_core();
  529. /* get the current revision from MSR 0x8B */
  530. native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  531. if (val[1] != mc_intel->hdr.rev)
  532. return -1;
  533. #ifdef CONFIG_X86_64
  534. /* Flush global tlb. This is precaution. */
  535. flush_tlb_early();
  536. #endif
  537. uci->cpu_sig.rev = val[1];
  538. if (early)
  539. print_ucode(uci);
  540. else
  541. print_ucode_info(uci, mc_intel->hdr.date);
  542. return 0;
  543. }
  544. /*
  545. * This function converts microcode patch offsets previously stored in
  546. * mc_saved_in_initrd to pointers and stores the pointers in mc_saved_data.
  547. */
  548. int __init save_microcode_in_initrd_intel(void)
  549. {
  550. unsigned int count = mc_saved_data.mc_saved_count;
  551. struct microcode_intel *mc_saved[MAX_UCODE_COUNT];
  552. int ret = 0;
  553. if (count == 0)
  554. return ret;
  555. copy_initrd_ptrs(mc_saved, mc_saved_in_initrd, initrd_start, count);
  556. ret = save_microcode(&mc_saved_data, mc_saved, count);
  557. if (ret)
  558. pr_err("Cannot save microcode patches from initrd.\n");
  559. show_saved_mc();
  560. return ret;
  561. }
  562. static void __init
  563. _load_ucode_intel_bsp(struct mc_saved_data *mc_saved_data,
  564. unsigned long *initrd,
  565. unsigned long start, unsigned long size)
  566. {
  567. struct ucode_cpu_info uci;
  568. enum ucode_state ret;
  569. collect_cpu_info_early(&uci);
  570. ret = scan_microcode(mc_saved_data, initrd, start, size, &uci);
  571. if (ret != UCODE_OK)
  572. return;
  573. ret = load_microcode(mc_saved_data, initrd, start, &uci);
  574. if (ret != UCODE_OK)
  575. return;
  576. apply_microcode_early(&uci, true);
  577. }
  578. void __init load_ucode_intel_bsp(void)
  579. {
  580. u64 start, size;
  581. #ifdef CONFIG_X86_32
  582. struct boot_params *p;
  583. p = (struct boot_params *)__pa_nodebug(&boot_params);
  584. start = p->hdr.ramdisk_image;
  585. size = p->hdr.ramdisk_size;
  586. _load_ucode_intel_bsp(
  587. (struct mc_saved_data *)__pa_nodebug(&mc_saved_data),
  588. (unsigned long *)__pa_nodebug(&mc_saved_in_initrd),
  589. start, size);
  590. #else
  591. start = boot_params.hdr.ramdisk_image + PAGE_OFFSET;
  592. size = boot_params.hdr.ramdisk_size;
  593. _load_ucode_intel_bsp(&mc_saved_data, mc_saved_in_initrd, start, size);
  594. #endif
  595. }
  596. void load_ucode_intel_ap(void)
  597. {
  598. struct mc_saved_data *mc_saved_data_p;
  599. struct ucode_cpu_info uci;
  600. unsigned long *mc_saved_in_initrd_p;
  601. unsigned long initrd_start_addr;
  602. enum ucode_state ret;
  603. #ifdef CONFIG_X86_32
  604. unsigned long *initrd_start_p;
  605. mc_saved_in_initrd_p =
  606. (unsigned long *)__pa_nodebug(mc_saved_in_initrd);
  607. mc_saved_data_p = (struct mc_saved_data *)__pa_nodebug(&mc_saved_data);
  608. initrd_start_p = (unsigned long *)__pa_nodebug(&initrd_start);
  609. initrd_start_addr = (unsigned long)__pa_nodebug(*initrd_start_p);
  610. #else
  611. mc_saved_data_p = &mc_saved_data;
  612. mc_saved_in_initrd_p = mc_saved_in_initrd;
  613. initrd_start_addr = initrd_start;
  614. #endif
  615. /*
  616. * If there is no valid ucode previously saved in memory, no need to
  617. * update ucode on this AP.
  618. */
  619. if (mc_saved_data_p->mc_saved_count == 0)
  620. return;
  621. collect_cpu_info_early(&uci);
  622. ret = load_microcode(mc_saved_data_p, mc_saved_in_initrd_p,
  623. initrd_start_addr, &uci);
  624. if (ret != UCODE_OK)
  625. return;
  626. apply_microcode_early(&uci, true);
  627. }
  628. void reload_ucode_intel(void)
  629. {
  630. struct ucode_cpu_info uci;
  631. enum ucode_state ret;
  632. if (!mc_saved_data.mc_saved_count)
  633. return;
  634. collect_cpu_info_early(&uci);
  635. ret = load_microcode_early(mc_saved_data.mc_saved,
  636. mc_saved_data.mc_saved_count, &uci);
  637. if (ret != UCODE_OK)
  638. return;
  639. apply_microcode_early(&uci, false);
  640. }