intel_early.c 20 KB

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