intel.c 26 KB

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