intel.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. /*
  2. * Intel CPU Microcode Update Driver for Linux
  3. *
  4. * Copyright (C) 2000-2006 Tigran Aivazian <tigran@aivazian.fsnet.co.uk>
  5. * 2006 Shaohua Li <shaohua.li@intel.com>
  6. *
  7. * Intel CPU microcode early update for Linux
  8. *
  9. * Copyright (C) 2012 Fenghua Yu <fenghua.yu@intel.com>
  10. * H Peter Anvin" <hpa@zytor.com>
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. /*
  18. * This needs to be before all headers so that pr_debug in printk.h doesn't turn
  19. * printk calls into no_printk().
  20. *
  21. *#define DEBUG
  22. */
  23. #define pr_fmt(fmt) "microcode: " fmt
  24. #include <linux/earlycpio.h>
  25. #include <linux/firmware.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/initrd.h>
  29. #include <linux/kernel.h>
  30. #include <linux/slab.h>
  31. #include <linux/cpu.h>
  32. #include <linux/mm.h>
  33. #include <asm/microcode_intel.h>
  34. #include <asm/processor.h>
  35. #include <asm/tlbflush.h>
  36. #include <asm/setup.h>
  37. #include <asm/msr.h>
  38. /*
  39. * Temporary microcode blobs pointers storage. We note here the pointers to
  40. * microcode blobs we've got from whatever storage (detached initrd, builtin).
  41. * Later on, we put those into final storage mc_saved_data.mc_saved.
  42. */
  43. static unsigned long mc_tmp_ptrs[MAX_UCODE_COUNT];
  44. static struct mc_saved_data {
  45. unsigned int num_saved;
  46. struct microcode_intel **mc_saved;
  47. } mc_saved_data;
  48. /* Microcode blobs within the initrd. 0 if builtin. */
  49. static struct ucode_blobs {
  50. unsigned long start;
  51. bool valid;
  52. } blobs;
  53. static enum ucode_state
  54. load_microcode_early(struct microcode_intel **saved,
  55. unsigned int num_saved, struct ucode_cpu_info *uci)
  56. {
  57. struct microcode_intel *ucode_ptr, *new_mc = NULL;
  58. struct microcode_header_intel *mc_hdr;
  59. int new_rev, ret, i;
  60. new_rev = uci->cpu_sig.rev;
  61. for (i = 0; i < num_saved; i++) {
  62. ucode_ptr = saved[i];
  63. mc_hdr = (struct microcode_header_intel *)ucode_ptr;
  64. ret = has_newer_microcode(ucode_ptr,
  65. uci->cpu_sig.sig,
  66. uci->cpu_sig.pf,
  67. new_rev);
  68. if (!ret)
  69. continue;
  70. new_rev = mc_hdr->rev;
  71. new_mc = ucode_ptr;
  72. }
  73. if (!new_mc)
  74. return UCODE_NFOUND;
  75. uci->mc = (struct microcode_intel *)new_mc;
  76. return UCODE_OK;
  77. }
  78. static inline void
  79. copy_ptrs(struct microcode_intel **mc_saved, unsigned long *mc_ptrs,
  80. unsigned long off, int num_saved)
  81. {
  82. int i;
  83. for (i = 0; i < num_saved; i++)
  84. mc_saved[i] = (struct microcode_intel *)(mc_ptrs[i] + off);
  85. }
  86. #ifdef CONFIG_X86_32
  87. static void
  88. microcode_phys(struct microcode_intel **mc_saved_tmp, struct mc_saved_data *mcs)
  89. {
  90. int i;
  91. struct microcode_intel ***mc_saved;
  92. mc_saved = (struct microcode_intel ***)__pa_nodebug(&mcs->mc_saved);
  93. for (i = 0; i < mcs->num_saved; i++) {
  94. struct microcode_intel *p;
  95. p = *(struct microcode_intel **)__pa_nodebug(mcs->mc_saved + i);
  96. mc_saved_tmp[i] = (struct microcode_intel *)__pa_nodebug(p);
  97. }
  98. }
  99. #endif
  100. static enum ucode_state
  101. load_microcode(struct mc_saved_data *mcs, unsigned long *mc_ptrs,
  102. unsigned long offset, struct ucode_cpu_info *uci)
  103. {
  104. struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
  105. unsigned int count = mcs->num_saved;
  106. if (!mcs->mc_saved) {
  107. copy_ptrs(mc_saved_tmp, mc_ptrs, offset, count);
  108. return load_microcode_early(mc_saved_tmp, count, uci);
  109. } else {
  110. #ifdef CONFIG_X86_32
  111. microcode_phys(mc_saved_tmp, mcs);
  112. return load_microcode_early(mc_saved_tmp, count, uci);
  113. #else
  114. return load_microcode_early(mcs->mc_saved, count, uci);
  115. #endif
  116. }
  117. }
  118. /*
  119. * Given CPU signature and a microcode patch, this function finds if the
  120. * microcode patch has matching family and model with the CPU.
  121. */
  122. static enum ucode_state
  123. matching_model_microcode(struct microcode_header_intel *mc_header,
  124. unsigned long sig)
  125. {
  126. unsigned int fam, model;
  127. unsigned int fam_ucode, model_ucode;
  128. struct extended_sigtable *ext_header;
  129. unsigned long total_size = get_totalsize(mc_header);
  130. unsigned long data_size = get_datasize(mc_header);
  131. int ext_sigcount, i;
  132. struct extended_signature *ext_sig;
  133. fam = x86_family(sig);
  134. model = x86_model(sig);
  135. fam_ucode = x86_family(mc_header->sig);
  136. model_ucode = x86_model(mc_header->sig);
  137. if (fam == fam_ucode && model == model_ucode)
  138. return UCODE_OK;
  139. /* Look for ext. headers: */
  140. if (total_size <= data_size + MC_HEADER_SIZE)
  141. return UCODE_NFOUND;
  142. ext_header = (void *) mc_header + data_size + MC_HEADER_SIZE;
  143. ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
  144. ext_sigcount = ext_header->count;
  145. for (i = 0; i < ext_sigcount; i++) {
  146. fam_ucode = x86_family(ext_sig->sig);
  147. model_ucode = x86_model(ext_sig->sig);
  148. if (fam == fam_ucode && model == model_ucode)
  149. return UCODE_OK;
  150. ext_sig++;
  151. }
  152. return UCODE_NFOUND;
  153. }
  154. static int
  155. save_microcode(struct mc_saved_data *mcs,
  156. struct microcode_intel **mc_saved_src,
  157. unsigned int num_saved)
  158. {
  159. int i, j;
  160. struct microcode_intel **saved_ptr;
  161. int ret;
  162. if (!num_saved)
  163. return -EINVAL;
  164. /*
  165. * Copy new microcode data.
  166. */
  167. saved_ptr = kcalloc(num_saved, sizeof(struct microcode_intel *), GFP_KERNEL);
  168. if (!saved_ptr)
  169. return -ENOMEM;
  170. for (i = 0; i < num_saved; i++) {
  171. struct microcode_header_intel *mc_hdr;
  172. struct microcode_intel *mc;
  173. unsigned long size;
  174. if (!mc_saved_src[i]) {
  175. ret = -EINVAL;
  176. goto err;
  177. }
  178. mc = mc_saved_src[i];
  179. mc_hdr = &mc->hdr;
  180. size = get_totalsize(mc_hdr);
  181. saved_ptr[i] = kmemdup(mc, size, GFP_KERNEL);
  182. if (!saved_ptr[i]) {
  183. ret = -ENOMEM;
  184. goto err;
  185. }
  186. }
  187. /*
  188. * Point to newly saved microcode.
  189. */
  190. mcs->mc_saved = saved_ptr;
  191. mcs->num_saved = num_saved;
  192. return 0;
  193. err:
  194. for (j = 0; j <= i; j++)
  195. kfree(saved_ptr[j]);
  196. kfree(saved_ptr);
  197. return ret;
  198. }
  199. /*
  200. * A microcode patch in ucode_ptr is saved into mc_saved
  201. * - if it has matching signature and newer revision compared to an existing
  202. * patch mc_saved.
  203. * - or if it is a newly discovered microcode patch.
  204. *
  205. * The microcode patch should have matching model with CPU.
  206. *
  207. * Returns: The updated number @num_saved of saved microcode patches.
  208. */
  209. static unsigned int _save_mc(struct microcode_intel **mc_saved,
  210. u8 *ucode_ptr, unsigned int num_saved)
  211. {
  212. struct microcode_header_intel *mc_hdr, *mc_saved_hdr;
  213. unsigned int sig, pf;
  214. int found = 0, i;
  215. mc_hdr = (struct microcode_header_intel *)ucode_ptr;
  216. for (i = 0; i < num_saved; i++) {
  217. mc_saved_hdr = (struct microcode_header_intel *)mc_saved[i];
  218. sig = mc_saved_hdr->sig;
  219. pf = mc_saved_hdr->pf;
  220. if (!find_matching_signature(ucode_ptr, sig, pf))
  221. continue;
  222. found = 1;
  223. if (mc_hdr->rev <= mc_saved_hdr->rev)
  224. continue;
  225. /*
  226. * Found an older ucode saved earlier. Replace it with
  227. * this newer one.
  228. */
  229. mc_saved[i] = (struct microcode_intel *)ucode_ptr;
  230. break;
  231. }
  232. /* Newly detected microcode, save it to memory. */
  233. if (i >= num_saved && !found)
  234. mc_saved[num_saved++] = (struct microcode_intel *)ucode_ptr;
  235. return num_saved;
  236. }
  237. /*
  238. * Get microcode matching with BSP's model. Only CPUs with the same model as
  239. * BSP can stay in the platform.
  240. */
  241. static enum ucode_state __init
  242. get_matching_model_microcode(unsigned long start, void *data, size_t size,
  243. struct mc_saved_data *mcs, unsigned long *mc_ptrs,
  244. struct ucode_cpu_info *uci)
  245. {
  246. struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
  247. struct microcode_header_intel *mc_header;
  248. unsigned int num_saved = mcs->num_saved;
  249. enum ucode_state state = UCODE_OK;
  250. unsigned int leftover = size;
  251. u8 *ucode_ptr = data;
  252. unsigned int mc_size;
  253. int i;
  254. while (leftover && num_saved < ARRAY_SIZE(mc_saved_tmp)) {
  255. if (leftover < sizeof(mc_header))
  256. break;
  257. mc_header = (struct microcode_header_intel *)ucode_ptr;
  258. mc_size = get_totalsize(mc_header);
  259. if (!mc_size || mc_size > leftover ||
  260. microcode_sanity_check(ucode_ptr, 0) < 0)
  261. break;
  262. leftover -= mc_size;
  263. /*
  264. * Since APs with same family and model as the BSP may boot in
  265. * the platform, we need to find and save microcode patches
  266. * with the same family and model as the BSP.
  267. */
  268. if (matching_model_microcode(mc_header, uci->cpu_sig.sig) != UCODE_OK) {
  269. ucode_ptr += mc_size;
  270. continue;
  271. }
  272. num_saved = _save_mc(mc_saved_tmp, ucode_ptr, num_saved);
  273. ucode_ptr += mc_size;
  274. }
  275. if (leftover) {
  276. state = UCODE_ERROR;
  277. return state;
  278. }
  279. if (!num_saved) {
  280. state = UCODE_NFOUND;
  281. return state;
  282. }
  283. for (i = 0; i < num_saved; i++)
  284. mc_ptrs[i] = (unsigned long)mc_saved_tmp[i] - start;
  285. mcs->num_saved = num_saved;
  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_wrmsrl(MSR_IA32_UCODE_REV, 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. static void show_saved_mc(void)
  320. {
  321. #ifdef DEBUG
  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.num_saved) {
  326. pr_debug("no microcode data saved.\n");
  327. return;
  328. }
  329. pr_debug("Total microcode saved: %d\n", mc_saved_data.num_saved);
  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.num_saved; 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, total 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. #endif
  368. }
  369. #ifdef CONFIG_HOTPLUG_CPU
  370. static DEFINE_MUTEX(x86_cpu_microcode_mutex);
  371. /*
  372. * Save this mc into mc_saved_data. So it will be loaded early when a CPU is
  373. * hot added or resumes.
  374. *
  375. * Please make sure this mc should be a valid microcode patch before calling
  376. * this function.
  377. */
  378. int save_mc_for_early(u8 *mc)
  379. {
  380. struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
  381. unsigned int mc_saved_count_init;
  382. unsigned int num_saved;
  383. struct microcode_intel **mc_saved;
  384. int ret = 0;
  385. int i;
  386. /*
  387. * Hold hotplug lock so mc_saved_data is not accessed by a CPU in
  388. * hotplug.
  389. */
  390. mutex_lock(&x86_cpu_microcode_mutex);
  391. mc_saved_count_init = mc_saved_data.num_saved;
  392. num_saved = mc_saved_data.num_saved;
  393. mc_saved = mc_saved_data.mc_saved;
  394. if (mc_saved && num_saved)
  395. memcpy(mc_saved_tmp, mc_saved,
  396. num_saved * sizeof(struct microcode_intel *));
  397. /*
  398. * Save the microcode patch mc in mc_save_tmp structure if it's a newer
  399. * version.
  400. */
  401. num_saved = _save_mc(mc_saved_tmp, mc, num_saved);
  402. /*
  403. * Save the mc_save_tmp in global mc_saved_data.
  404. */
  405. ret = save_microcode(&mc_saved_data, mc_saved_tmp, num_saved);
  406. if (ret) {
  407. pr_err("Cannot save microcode patch.\n");
  408. goto out;
  409. }
  410. show_saved_mc();
  411. /*
  412. * Free old saved microcode data.
  413. */
  414. if (mc_saved) {
  415. for (i = 0; i < mc_saved_count_init; i++)
  416. kfree(mc_saved[i]);
  417. kfree(mc_saved);
  418. }
  419. out:
  420. mutex_unlock(&x86_cpu_microcode_mutex);
  421. return ret;
  422. }
  423. EXPORT_SYMBOL_GPL(save_mc_for_early);
  424. #endif
  425. static bool __init load_builtin_intel_microcode(struct cpio_data *cp)
  426. {
  427. #ifdef CONFIG_X86_64
  428. unsigned int eax = 0x00000001, ebx, ecx = 0, edx;
  429. char name[30];
  430. native_cpuid(&eax, &ebx, &ecx, &edx);
  431. sprintf(name, "intel-ucode/%02x-%02x-%02x",
  432. x86_family(eax), x86_model(eax), x86_stepping(eax));
  433. return get_builtin_firmware(cp, name);
  434. #else
  435. return false;
  436. #endif
  437. }
  438. /*
  439. * Print ucode update info.
  440. */
  441. static void
  442. print_ucode_info(struct ucode_cpu_info *uci, unsigned int date)
  443. {
  444. pr_info_once("microcode updated early to revision 0x%x, date = %04x-%02x-%02x\n",
  445. uci->cpu_sig.rev,
  446. date & 0xffff,
  447. date >> 24,
  448. (date >> 16) & 0xff);
  449. }
  450. #ifdef CONFIG_X86_32
  451. static int delay_ucode_info;
  452. static int current_mc_date;
  453. /*
  454. * Print early updated ucode info after printk works. This is delayed info dump.
  455. */
  456. void show_ucode_info_early(void)
  457. {
  458. struct ucode_cpu_info uci;
  459. if (delay_ucode_info) {
  460. collect_cpu_info_early(&uci);
  461. print_ucode_info(&uci, current_mc_date);
  462. delay_ucode_info = 0;
  463. }
  464. }
  465. /*
  466. * At this point, we can not call printk() yet. Keep microcode patch number in
  467. * mc_saved_data.mc_saved and delay printing microcode info in
  468. * show_ucode_info_early() until printk() works.
  469. */
  470. static void print_ucode(struct ucode_cpu_info *uci)
  471. {
  472. struct microcode_intel *mc;
  473. int *delay_ucode_info_p;
  474. int *current_mc_date_p;
  475. mc = uci->mc;
  476. if (!mc)
  477. return;
  478. delay_ucode_info_p = (int *)__pa_nodebug(&delay_ucode_info);
  479. current_mc_date_p = (int *)__pa_nodebug(&current_mc_date);
  480. *delay_ucode_info_p = 1;
  481. *current_mc_date_p = mc->hdr.date;
  482. }
  483. #else
  484. /*
  485. * Flush global tlb. We only do this in x86_64 where paging has been enabled
  486. * already and PGE should be enabled as well.
  487. */
  488. static inline void flush_tlb_early(void)
  489. {
  490. __native_flush_tlb_global_irq_disabled();
  491. }
  492. static inline void print_ucode(struct ucode_cpu_info *uci)
  493. {
  494. struct microcode_intel *mc;
  495. mc = uci->mc;
  496. if (!mc)
  497. return;
  498. print_ucode_info(uci, mc->hdr.date);
  499. }
  500. #endif
  501. static int apply_microcode_early(struct ucode_cpu_info *uci, bool early)
  502. {
  503. struct microcode_intel *mc;
  504. unsigned int val[2];
  505. mc = uci->mc;
  506. if (!mc)
  507. return 0;
  508. /* write microcode via MSR 0x79 */
  509. native_wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits);
  510. native_wrmsrl(MSR_IA32_UCODE_REV, 0);
  511. /* As documented in the SDM: Do a CPUID 1 here */
  512. sync_core();
  513. /* get the current revision from MSR 0x8B */
  514. native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  515. if (val[1] != mc->hdr.rev)
  516. return -1;
  517. #ifdef CONFIG_X86_64
  518. /* Flush global tlb. This is precaution. */
  519. flush_tlb_early();
  520. #endif
  521. uci->cpu_sig.rev = val[1];
  522. if (early)
  523. print_ucode(uci);
  524. else
  525. print_ucode_info(uci, mc->hdr.date);
  526. return 0;
  527. }
  528. /*
  529. * This function converts microcode patch offsets previously stored in
  530. * mc_tmp_ptrs to pointers and stores the pointers in mc_saved_data.
  531. */
  532. int __init save_microcode_in_initrd_intel(void)
  533. {
  534. struct microcode_intel *mc_saved[MAX_UCODE_COUNT];
  535. unsigned int count = mc_saved_data.num_saved;
  536. unsigned long offset = 0;
  537. int ret;
  538. if (!count)
  539. return 0;
  540. /*
  541. * We have found a valid initrd but it might've been relocated in the
  542. * meantime so get its updated address.
  543. */
  544. if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && blobs.valid)
  545. offset = initrd_start;
  546. copy_ptrs(mc_saved, mc_tmp_ptrs, offset, count);
  547. ret = save_microcode(&mc_saved_data, mc_saved, count);
  548. if (ret)
  549. pr_err("Cannot save microcode patches from initrd.\n");
  550. else
  551. show_saved_mc();
  552. return ret;
  553. }
  554. static __init enum ucode_state
  555. __scan_microcode_initrd(struct cpio_data *cd, struct ucode_blobs *blbp)
  556. {
  557. #ifdef CONFIG_BLK_DEV_INITRD
  558. static __initdata char ucode_name[] = "kernel/x86/microcode/GenuineIntel.bin";
  559. char *p = IS_ENABLED(CONFIG_X86_32) ? (char *)__pa_nodebug(ucode_name)
  560. : ucode_name;
  561. # ifdef CONFIG_X86_32
  562. unsigned long start = 0, size;
  563. struct boot_params *params;
  564. params = (struct boot_params *)__pa_nodebug(&boot_params);
  565. size = params->hdr.ramdisk_size;
  566. /*
  567. * Set start only if we have an initrd image. We cannot use initrd_start
  568. * because it is not set that early yet.
  569. */
  570. start = (size ? params->hdr.ramdisk_image : 0);
  571. # else /* CONFIG_X86_64 */
  572. unsigned long start = 0, size;
  573. size = (u64)boot_params.ext_ramdisk_size << 32;
  574. size |= boot_params.hdr.ramdisk_size;
  575. if (size) {
  576. start = (u64)boot_params.ext_ramdisk_image << 32;
  577. start |= boot_params.hdr.ramdisk_image;
  578. start += PAGE_OFFSET;
  579. }
  580. # endif
  581. *cd = find_cpio_data(p, (void *)start, size, NULL);
  582. if (cd->data) {
  583. blbp->start = start;
  584. blbp->valid = true;
  585. return UCODE_OK;
  586. } else
  587. #endif /* CONFIG_BLK_DEV_INITRD */
  588. return UCODE_ERROR;
  589. }
  590. static __init enum ucode_state
  591. scan_microcode(struct mc_saved_data *mcs, unsigned long *mc_ptrs,
  592. struct ucode_cpu_info *uci, struct ucode_blobs *blbp)
  593. {
  594. struct cpio_data cd = { NULL, 0, "" };
  595. enum ucode_state ret;
  596. /* try built-in microcode first */
  597. if (load_builtin_intel_microcode(&cd))
  598. /*
  599. * Invalidate blobs as we might've gotten an initrd too,
  600. * supplied by the boot loader, by mistake or simply forgotten
  601. * there. That's fine, we ignore it since we've found builtin
  602. * microcode already.
  603. */
  604. blbp->valid = false;
  605. else {
  606. ret = __scan_microcode_initrd(&cd, blbp);
  607. if (ret != UCODE_OK)
  608. return ret;
  609. }
  610. return get_matching_model_microcode(blbp->start, cd.data, cd.size,
  611. mcs, mc_ptrs, uci);
  612. }
  613. static void __init
  614. _load_ucode_intel_bsp(struct mc_saved_data *mcs, unsigned long *mc_ptrs,
  615. struct ucode_blobs *blbp)
  616. {
  617. struct ucode_cpu_info uci;
  618. enum ucode_state ret;
  619. collect_cpu_info_early(&uci);
  620. ret = scan_microcode(mcs, mc_ptrs, &uci, blbp);
  621. if (ret != UCODE_OK)
  622. return;
  623. ret = load_microcode(mcs, mc_ptrs, blbp->start, &uci);
  624. if (ret != UCODE_OK)
  625. return;
  626. apply_microcode_early(&uci, true);
  627. }
  628. void __init load_ucode_intel_bsp(void)
  629. {
  630. struct ucode_blobs *blobs_p;
  631. struct mc_saved_data *mcs;
  632. unsigned long *ptrs;
  633. #ifdef CONFIG_X86_32
  634. mcs = (struct mc_saved_data *)__pa_nodebug(&mc_saved_data);
  635. ptrs = (unsigned long *)__pa_nodebug(&mc_tmp_ptrs);
  636. blobs_p = (struct ucode_blobs *)__pa_nodebug(&blobs);
  637. #else
  638. mcs = &mc_saved_data;
  639. ptrs = mc_tmp_ptrs;
  640. blobs_p = &blobs;
  641. #endif
  642. _load_ucode_intel_bsp(mcs, ptrs, blobs_p);
  643. }
  644. void load_ucode_intel_ap(void)
  645. {
  646. struct ucode_blobs *blobs_p;
  647. struct mc_saved_data *mcs;
  648. struct ucode_cpu_info uci;
  649. enum ucode_state ret;
  650. unsigned long *ptrs;
  651. #ifdef CONFIG_X86_32
  652. mcs = (struct mc_saved_data *)__pa_nodebug(&mc_saved_data);
  653. ptrs = (unsigned long *)__pa_nodebug(mc_tmp_ptrs);
  654. blobs_p = (struct ucode_blobs *)__pa_nodebug(&blobs);
  655. #else
  656. mcs = &mc_saved_data;
  657. ptrs = mc_tmp_ptrs;
  658. blobs_p = &blobs;
  659. #endif
  660. /*
  661. * If there is no valid ucode previously saved in memory, no need to
  662. * update ucode on this AP.
  663. */
  664. if (!mcs->num_saved)
  665. return;
  666. collect_cpu_info_early(&uci);
  667. ret = load_microcode(mcs, ptrs, blobs_p->start, &uci);
  668. if (ret != UCODE_OK)
  669. return;
  670. apply_microcode_early(&uci, true);
  671. }
  672. void reload_ucode_intel(void)
  673. {
  674. struct ucode_cpu_info uci;
  675. enum ucode_state ret;
  676. if (!mc_saved_data.num_saved)
  677. return;
  678. collect_cpu_info_early(&uci);
  679. ret = load_microcode_early(mc_saved_data.mc_saved,
  680. mc_saved_data.num_saved, &uci);
  681. if (ret != UCODE_OK)
  682. return;
  683. apply_microcode_early(&uci, false);
  684. }
  685. static int collect_cpu_info(int cpu_num, struct cpu_signature *csig)
  686. {
  687. struct cpuinfo_x86 *c = &cpu_data(cpu_num);
  688. unsigned int val[2];
  689. memset(csig, 0, sizeof(*csig));
  690. csig->sig = cpuid_eax(0x00000001);
  691. if ((c->x86_model >= 5) || (c->x86 > 6)) {
  692. /* get processor flags from MSR 0x17 */
  693. rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  694. csig->pf = 1 << ((val[1] >> 18) & 7);
  695. }
  696. csig->rev = c->microcode;
  697. pr_info("CPU%d sig=0x%x, pf=0x%x, revision=0x%x\n",
  698. cpu_num, csig->sig, csig->pf, csig->rev);
  699. return 0;
  700. }
  701. /*
  702. * return 0 - no update found
  703. * return 1 - found update
  704. */
  705. static int get_matching_mc(struct microcode_intel *mc, int cpu)
  706. {
  707. struct cpu_signature cpu_sig;
  708. unsigned int csig, cpf, crev;
  709. collect_cpu_info(cpu, &cpu_sig);
  710. csig = cpu_sig.sig;
  711. cpf = cpu_sig.pf;
  712. crev = cpu_sig.rev;
  713. return has_newer_microcode(mc, csig, cpf, crev);
  714. }
  715. static int apply_microcode_intel(int cpu)
  716. {
  717. struct microcode_intel *mc;
  718. struct ucode_cpu_info *uci;
  719. struct cpuinfo_x86 *c;
  720. unsigned int val[2];
  721. /* We should bind the task to the CPU */
  722. if (WARN_ON(raw_smp_processor_id() != cpu))
  723. return -1;
  724. uci = ucode_cpu_info + cpu;
  725. mc = uci->mc;
  726. if (!mc)
  727. return 0;
  728. /*
  729. * Microcode on this CPU could be updated earlier. Only apply the
  730. * microcode patch in mc when it is newer than the one on this
  731. * CPU.
  732. */
  733. if (!get_matching_mc(mc, cpu))
  734. return 0;
  735. /* write microcode via MSR 0x79 */
  736. wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits);
  737. wrmsrl(MSR_IA32_UCODE_REV, 0);
  738. /* As documented in the SDM: Do a CPUID 1 here */
  739. sync_core();
  740. /* get the current revision from MSR 0x8B */
  741. rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  742. if (val[1] != mc->hdr.rev) {
  743. pr_err("CPU%d update to revision 0x%x failed\n",
  744. cpu, mc->hdr.rev);
  745. return -1;
  746. }
  747. pr_info("CPU%d updated to revision 0x%x, date = %04x-%02x-%02x\n",
  748. cpu, val[1],
  749. mc->hdr.date & 0xffff,
  750. mc->hdr.date >> 24,
  751. (mc->hdr.date >> 16) & 0xff);
  752. c = &cpu_data(cpu);
  753. uci->cpu_sig.rev = val[1];
  754. c->microcode = val[1];
  755. return 0;
  756. }
  757. static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size,
  758. int (*get_ucode_data)(void *, const void *, size_t))
  759. {
  760. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  761. u8 *ucode_ptr = data, *new_mc = NULL, *mc = NULL;
  762. int new_rev = uci->cpu_sig.rev;
  763. unsigned int leftover = size;
  764. enum ucode_state state = UCODE_OK;
  765. unsigned int curr_mc_size = 0;
  766. unsigned int csig, cpf;
  767. while (leftover) {
  768. struct microcode_header_intel mc_header;
  769. unsigned int mc_size;
  770. if (leftover < sizeof(mc_header)) {
  771. pr_err("error! Truncated header in microcode data file\n");
  772. break;
  773. }
  774. if (get_ucode_data(&mc_header, ucode_ptr, sizeof(mc_header)))
  775. break;
  776. mc_size = get_totalsize(&mc_header);
  777. if (!mc_size || mc_size > leftover) {
  778. pr_err("error! Bad data in microcode data file\n");
  779. break;
  780. }
  781. /* For performance reasons, reuse mc area when possible */
  782. if (!mc || mc_size > curr_mc_size) {
  783. vfree(mc);
  784. mc = vmalloc(mc_size);
  785. if (!mc)
  786. break;
  787. curr_mc_size = mc_size;
  788. }
  789. if (get_ucode_data(mc, ucode_ptr, mc_size) ||
  790. microcode_sanity_check(mc, 1) < 0) {
  791. break;
  792. }
  793. csig = uci->cpu_sig.sig;
  794. cpf = uci->cpu_sig.pf;
  795. if (has_newer_microcode(mc, csig, cpf, new_rev)) {
  796. vfree(new_mc);
  797. new_rev = mc_header.rev;
  798. new_mc = mc;
  799. mc = NULL; /* trigger new vmalloc */
  800. }
  801. ucode_ptr += mc_size;
  802. leftover -= mc_size;
  803. }
  804. vfree(mc);
  805. if (leftover) {
  806. vfree(new_mc);
  807. state = UCODE_ERROR;
  808. goto out;
  809. }
  810. if (!new_mc) {
  811. state = UCODE_NFOUND;
  812. goto out;
  813. }
  814. vfree(uci->mc);
  815. uci->mc = (struct microcode_intel *)new_mc;
  816. /*
  817. * If early loading microcode is supported, save this mc into
  818. * permanent memory. So it will be loaded early when a CPU is hot added
  819. * or resumes.
  820. */
  821. save_mc_for_early(new_mc);
  822. pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n",
  823. cpu, new_rev, uci->cpu_sig.rev);
  824. out:
  825. return state;
  826. }
  827. static int get_ucode_fw(void *to, const void *from, size_t n)
  828. {
  829. memcpy(to, from, n);
  830. return 0;
  831. }
  832. static enum ucode_state request_microcode_fw(int cpu, struct device *device,
  833. bool refresh_fw)
  834. {
  835. char name[30];
  836. struct cpuinfo_x86 *c = &cpu_data(cpu);
  837. const struct firmware *firmware;
  838. enum ucode_state ret;
  839. sprintf(name, "intel-ucode/%02x-%02x-%02x",
  840. c->x86, c->x86_model, c->x86_mask);
  841. if (request_firmware_direct(&firmware, name, device)) {
  842. pr_debug("data file %s load failed\n", name);
  843. return UCODE_NFOUND;
  844. }
  845. ret = generic_load_microcode(cpu, (void *)firmware->data,
  846. firmware->size, &get_ucode_fw);
  847. release_firmware(firmware);
  848. return ret;
  849. }
  850. static int get_ucode_user(void *to, const void *from, size_t n)
  851. {
  852. return copy_from_user(to, from, n);
  853. }
  854. static enum ucode_state
  855. request_microcode_user(int cpu, const void __user *buf, size_t size)
  856. {
  857. return generic_load_microcode(cpu, (void *)buf, size, &get_ucode_user);
  858. }
  859. static void microcode_fini_cpu(int cpu)
  860. {
  861. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  862. vfree(uci->mc);
  863. uci->mc = NULL;
  864. }
  865. static struct microcode_ops microcode_intel_ops = {
  866. .request_microcode_user = request_microcode_user,
  867. .request_microcode_fw = request_microcode_fw,
  868. .collect_cpu_info = collect_cpu_info,
  869. .apply_microcode = apply_microcode_intel,
  870. .microcode_fini_cpu = microcode_fini_cpu,
  871. };
  872. struct microcode_ops * __init init_intel_microcode(void)
  873. {
  874. struct cpuinfo_x86 *c = &boot_cpu_data;
  875. if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
  876. cpu_has(c, X86_FEATURE_IA64)) {
  877. pr_err("Intel CPU family 0x%x not supported\n", c->x86);
  878. return NULL;
  879. }
  880. return &microcode_intel_ops;
  881. }