intel.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  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. find_microcode_patch(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 find_microcode_patch(mc_saved_tmp, count, uci);
  109. } else {
  110. #ifdef CONFIG_X86_32
  111. microcode_phys(mc_saved_tmp, mcs);
  112. return find_microcode_patch(mc_saved_tmp, count, uci);
  113. #else
  114. return find_microcode_patch(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. /*
  370. * Save this mc into mc_saved_data. So it will be loaded early when a CPU is
  371. * hot added or resumes.
  372. *
  373. * Please make sure this mc should be a valid microcode patch before calling
  374. * this function.
  375. */
  376. static void save_mc_for_early(u8 *mc)
  377. {
  378. #ifdef CONFIG_HOTPLUG_CPU
  379. static DEFINE_MUTEX(x86_cpu_microcode_mutex);
  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, i;
  385. /*
  386. * Hold hotplug lock so mc_saved_data is not accessed by a CPU in
  387. * hotplug.
  388. */
  389. mutex_lock(&x86_cpu_microcode_mutex);
  390. mc_saved_count_init = mc_saved_data.num_saved;
  391. num_saved = mc_saved_data.num_saved;
  392. mc_saved = mc_saved_data.mc_saved;
  393. if (mc_saved && num_saved)
  394. memcpy(mc_saved_tmp, mc_saved,
  395. num_saved * sizeof(struct microcode_intel *));
  396. /*
  397. * Save the microcode patch mc in mc_save_tmp structure if it's a newer
  398. * version.
  399. */
  400. num_saved = _save_mc(mc_saved_tmp, mc, num_saved);
  401. /*
  402. * Save the mc_save_tmp in global mc_saved_data.
  403. */
  404. ret = save_microcode(&mc_saved_data, mc_saved_tmp, num_saved);
  405. if (ret) {
  406. pr_err("Cannot save microcode patch.\n");
  407. goto out;
  408. }
  409. show_saved_mc();
  410. /*
  411. * Free old saved microcode data.
  412. */
  413. if (mc_saved) {
  414. for (i = 0; i < mc_saved_count_init; i++)
  415. kfree(mc_saved[i]);
  416. kfree(mc_saved);
  417. }
  418. out:
  419. mutex_unlock(&x86_cpu_microcode_mutex);
  420. #endif
  421. }
  422. static bool __init load_builtin_intel_microcode(struct cpio_data *cp)
  423. {
  424. #ifdef CONFIG_X86_64
  425. unsigned int eax = 0x00000001, ebx, ecx = 0, edx;
  426. char name[30];
  427. native_cpuid(&eax, &ebx, &ecx, &edx);
  428. sprintf(name, "intel-ucode/%02x-%02x-%02x",
  429. x86_family(eax), x86_model(eax), x86_stepping(eax));
  430. return get_builtin_firmware(cp, name);
  431. #else
  432. return false;
  433. #endif
  434. }
  435. /*
  436. * Print ucode update info.
  437. */
  438. static void
  439. print_ucode_info(struct ucode_cpu_info *uci, unsigned int date)
  440. {
  441. pr_info_once("microcode updated early to revision 0x%x, date = %04x-%02x-%02x\n",
  442. uci->cpu_sig.rev,
  443. date & 0xffff,
  444. date >> 24,
  445. (date >> 16) & 0xff);
  446. }
  447. #ifdef CONFIG_X86_32
  448. static int delay_ucode_info;
  449. static int current_mc_date;
  450. /*
  451. * Print early updated ucode info after printk works. This is delayed info dump.
  452. */
  453. void show_ucode_info_early(void)
  454. {
  455. struct ucode_cpu_info uci;
  456. if (delay_ucode_info) {
  457. collect_cpu_info_early(&uci);
  458. print_ucode_info(&uci, current_mc_date);
  459. delay_ucode_info = 0;
  460. }
  461. }
  462. /*
  463. * At this point, we can not call printk() yet. Keep microcode patch number in
  464. * mc_saved_data.mc_saved and delay printing microcode info in
  465. * show_ucode_info_early() until printk() works.
  466. */
  467. static void print_ucode(struct ucode_cpu_info *uci)
  468. {
  469. struct microcode_intel *mc;
  470. int *delay_ucode_info_p;
  471. int *current_mc_date_p;
  472. mc = uci->mc;
  473. if (!mc)
  474. return;
  475. delay_ucode_info_p = (int *)__pa_nodebug(&delay_ucode_info);
  476. current_mc_date_p = (int *)__pa_nodebug(&current_mc_date);
  477. *delay_ucode_info_p = 1;
  478. *current_mc_date_p = mc->hdr.date;
  479. }
  480. #else
  481. /*
  482. * Flush global tlb. We only do this in x86_64 where paging has been enabled
  483. * already and PGE should be enabled as well.
  484. */
  485. static inline void flush_tlb_early(void)
  486. {
  487. __native_flush_tlb_global_irq_disabled();
  488. }
  489. static inline void print_ucode(struct ucode_cpu_info *uci)
  490. {
  491. struct microcode_intel *mc;
  492. mc = uci->mc;
  493. if (!mc)
  494. return;
  495. print_ucode_info(uci, mc->hdr.date);
  496. }
  497. #endif
  498. static int apply_microcode_early(struct ucode_cpu_info *uci, bool early)
  499. {
  500. struct microcode_intel *mc;
  501. unsigned int val[2];
  502. mc = uci->mc;
  503. if (!mc)
  504. return 0;
  505. /* write microcode via MSR 0x79 */
  506. native_wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits);
  507. native_wrmsrl(MSR_IA32_UCODE_REV, 0);
  508. /* As documented in the SDM: Do a CPUID 1 here */
  509. sync_core();
  510. /* get the current revision from MSR 0x8B */
  511. native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  512. if (val[1] != mc->hdr.rev)
  513. return -1;
  514. #ifdef CONFIG_X86_64
  515. /* Flush global tlb. This is precaution. */
  516. flush_tlb_early();
  517. #endif
  518. uci->cpu_sig.rev = val[1];
  519. if (early)
  520. print_ucode(uci);
  521. else
  522. print_ucode_info(uci, mc->hdr.date);
  523. return 0;
  524. }
  525. /*
  526. * This function converts microcode patch offsets previously stored in
  527. * mc_tmp_ptrs to pointers and stores the pointers in mc_saved_data.
  528. */
  529. int __init save_microcode_in_initrd_intel(void)
  530. {
  531. struct microcode_intel *mc_saved[MAX_UCODE_COUNT];
  532. unsigned int count = mc_saved_data.num_saved;
  533. unsigned long offset = 0;
  534. int ret;
  535. if (!count)
  536. return 0;
  537. /*
  538. * We have found a valid initrd but it might've been relocated in the
  539. * meantime so get its updated address.
  540. */
  541. if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && blobs.valid)
  542. offset = initrd_start;
  543. copy_ptrs(mc_saved, mc_tmp_ptrs, offset, count);
  544. ret = save_microcode(&mc_saved_data, mc_saved, count);
  545. if (ret)
  546. pr_err("Cannot save microcode patches from initrd.\n");
  547. else
  548. show_saved_mc();
  549. return ret;
  550. }
  551. static __init enum ucode_state
  552. __scan_microcode_initrd(struct cpio_data *cd, struct ucode_blobs *blbp)
  553. {
  554. #ifdef CONFIG_BLK_DEV_INITRD
  555. static __initdata char ucode_name[] = "kernel/x86/microcode/GenuineIntel.bin";
  556. char *p = IS_ENABLED(CONFIG_X86_32) ? (char *)__pa_nodebug(ucode_name)
  557. : ucode_name;
  558. # ifdef CONFIG_X86_32
  559. unsigned long start = 0, size;
  560. struct boot_params *params;
  561. params = (struct boot_params *)__pa_nodebug(&boot_params);
  562. size = params->hdr.ramdisk_size;
  563. /*
  564. * Set start only if we have an initrd image. We cannot use initrd_start
  565. * because it is not set that early yet.
  566. */
  567. start = (size ? params->hdr.ramdisk_image : 0);
  568. # else /* CONFIG_X86_64 */
  569. unsigned long start = 0, size;
  570. size = (u64)boot_params.ext_ramdisk_size << 32;
  571. size |= boot_params.hdr.ramdisk_size;
  572. if (size) {
  573. start = (u64)boot_params.ext_ramdisk_image << 32;
  574. start |= boot_params.hdr.ramdisk_image;
  575. start += PAGE_OFFSET;
  576. }
  577. # endif
  578. *cd = find_cpio_data(p, (void *)start, size, NULL);
  579. if (cd->data) {
  580. blbp->start = start;
  581. blbp->valid = true;
  582. return UCODE_OK;
  583. } else
  584. #endif /* CONFIG_BLK_DEV_INITRD */
  585. return UCODE_ERROR;
  586. }
  587. static __init enum ucode_state
  588. scan_microcode(struct mc_saved_data *mcs, unsigned long *mc_ptrs,
  589. struct ucode_cpu_info *uci, struct ucode_blobs *blbp)
  590. {
  591. struct cpio_data cd = { NULL, 0, "" };
  592. enum ucode_state ret;
  593. /* try built-in microcode first */
  594. if (load_builtin_intel_microcode(&cd))
  595. /*
  596. * Invalidate blobs as we might've gotten an initrd too,
  597. * supplied by the boot loader, by mistake or simply forgotten
  598. * there. That's fine, we ignore it since we've found builtin
  599. * microcode already.
  600. */
  601. blbp->valid = false;
  602. else {
  603. ret = __scan_microcode_initrd(&cd, blbp);
  604. if (ret != UCODE_OK)
  605. return ret;
  606. }
  607. return get_matching_model_microcode(blbp->start, cd.data, cd.size,
  608. mcs, mc_ptrs, uci);
  609. }
  610. static void __init
  611. _load_ucode_intel_bsp(struct mc_saved_data *mcs, unsigned long *mc_ptrs,
  612. struct ucode_blobs *blbp)
  613. {
  614. struct ucode_cpu_info uci;
  615. enum ucode_state ret;
  616. collect_cpu_info_early(&uci);
  617. ret = scan_microcode(mcs, mc_ptrs, &uci, blbp);
  618. if (ret != UCODE_OK)
  619. return;
  620. ret = load_microcode(mcs, mc_ptrs, blbp->start, &uci);
  621. if (ret != UCODE_OK)
  622. return;
  623. apply_microcode_early(&uci, true);
  624. }
  625. void __init load_ucode_intel_bsp(void)
  626. {
  627. struct ucode_blobs *blobs_p;
  628. struct mc_saved_data *mcs;
  629. unsigned long *ptrs;
  630. #ifdef CONFIG_X86_32
  631. mcs = (struct mc_saved_data *)__pa_nodebug(&mc_saved_data);
  632. ptrs = (unsigned long *)__pa_nodebug(&mc_tmp_ptrs);
  633. blobs_p = (struct ucode_blobs *)__pa_nodebug(&blobs);
  634. #else
  635. mcs = &mc_saved_data;
  636. ptrs = mc_tmp_ptrs;
  637. blobs_p = &blobs;
  638. #endif
  639. _load_ucode_intel_bsp(mcs, ptrs, blobs_p);
  640. }
  641. void load_ucode_intel_ap(void)
  642. {
  643. struct ucode_blobs *blobs_p;
  644. struct mc_saved_data *mcs;
  645. struct ucode_cpu_info uci;
  646. enum ucode_state ret;
  647. unsigned long *ptrs;
  648. #ifdef CONFIG_X86_32
  649. mcs = (struct mc_saved_data *)__pa_nodebug(&mc_saved_data);
  650. ptrs = (unsigned long *)__pa_nodebug(mc_tmp_ptrs);
  651. blobs_p = (struct ucode_blobs *)__pa_nodebug(&blobs);
  652. #else
  653. mcs = &mc_saved_data;
  654. ptrs = mc_tmp_ptrs;
  655. blobs_p = &blobs;
  656. #endif
  657. /*
  658. * If there is no valid ucode previously saved in memory, no need to
  659. * update ucode on this AP.
  660. */
  661. if (!mcs->num_saved)
  662. return;
  663. collect_cpu_info_early(&uci);
  664. ret = load_microcode(mcs, ptrs, blobs_p->start, &uci);
  665. if (ret != UCODE_OK)
  666. return;
  667. apply_microcode_early(&uci, true);
  668. }
  669. void reload_ucode_intel(void)
  670. {
  671. struct ucode_cpu_info uci;
  672. enum ucode_state ret;
  673. if (!mc_saved_data.num_saved)
  674. return;
  675. collect_cpu_info_early(&uci);
  676. ret = find_microcode_patch(mc_saved_data.mc_saved,
  677. mc_saved_data.num_saved, &uci);
  678. if (ret != UCODE_OK)
  679. return;
  680. apply_microcode_early(&uci, false);
  681. }
  682. static int collect_cpu_info(int cpu_num, struct cpu_signature *csig)
  683. {
  684. struct cpuinfo_x86 *c = &cpu_data(cpu_num);
  685. unsigned int val[2];
  686. memset(csig, 0, sizeof(*csig));
  687. csig->sig = cpuid_eax(0x00000001);
  688. if ((c->x86_model >= 5) || (c->x86 > 6)) {
  689. /* get processor flags from MSR 0x17 */
  690. rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  691. csig->pf = 1 << ((val[1] >> 18) & 7);
  692. }
  693. csig->rev = c->microcode;
  694. pr_info("CPU%d sig=0x%x, pf=0x%x, revision=0x%x\n",
  695. cpu_num, csig->sig, csig->pf, csig->rev);
  696. return 0;
  697. }
  698. /*
  699. * return 0 - no update found
  700. * return 1 - found update
  701. */
  702. static int get_matching_mc(struct microcode_intel *mc, int cpu)
  703. {
  704. struct cpu_signature cpu_sig;
  705. unsigned int csig, cpf, crev;
  706. collect_cpu_info(cpu, &cpu_sig);
  707. csig = cpu_sig.sig;
  708. cpf = cpu_sig.pf;
  709. crev = cpu_sig.rev;
  710. return has_newer_microcode(mc, csig, cpf, crev);
  711. }
  712. static int apply_microcode_intel(int cpu)
  713. {
  714. struct microcode_intel *mc;
  715. struct ucode_cpu_info *uci;
  716. struct cpuinfo_x86 *c;
  717. unsigned int val[2];
  718. /* We should bind the task to the CPU */
  719. if (WARN_ON(raw_smp_processor_id() != cpu))
  720. return -1;
  721. uci = ucode_cpu_info + cpu;
  722. mc = uci->mc;
  723. if (!mc)
  724. return 0;
  725. /*
  726. * Microcode on this CPU could be updated earlier. Only apply the
  727. * microcode patch in mc when it is newer than the one on this
  728. * CPU.
  729. */
  730. if (!get_matching_mc(mc, cpu))
  731. return 0;
  732. /* write microcode via MSR 0x79 */
  733. wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits);
  734. wrmsrl(MSR_IA32_UCODE_REV, 0);
  735. /* As documented in the SDM: Do a CPUID 1 here */
  736. sync_core();
  737. /* get the current revision from MSR 0x8B */
  738. rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  739. if (val[1] != mc->hdr.rev) {
  740. pr_err("CPU%d update to revision 0x%x failed\n",
  741. cpu, mc->hdr.rev);
  742. return -1;
  743. }
  744. pr_info("CPU%d updated to revision 0x%x, date = %04x-%02x-%02x\n",
  745. cpu, val[1],
  746. mc->hdr.date & 0xffff,
  747. mc->hdr.date >> 24,
  748. (mc->hdr.date >> 16) & 0xff);
  749. c = &cpu_data(cpu);
  750. uci->cpu_sig.rev = val[1];
  751. c->microcode = val[1];
  752. return 0;
  753. }
  754. static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size,
  755. int (*get_ucode_data)(void *, const void *, size_t))
  756. {
  757. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  758. u8 *ucode_ptr = data, *new_mc = NULL, *mc = NULL;
  759. int new_rev = uci->cpu_sig.rev;
  760. unsigned int leftover = size;
  761. enum ucode_state state = UCODE_OK;
  762. unsigned int curr_mc_size = 0;
  763. unsigned int csig, cpf;
  764. while (leftover) {
  765. struct microcode_header_intel mc_header;
  766. unsigned int mc_size;
  767. if (leftover < sizeof(mc_header)) {
  768. pr_err("error! Truncated header in microcode data file\n");
  769. break;
  770. }
  771. if (get_ucode_data(&mc_header, ucode_ptr, sizeof(mc_header)))
  772. break;
  773. mc_size = get_totalsize(&mc_header);
  774. if (!mc_size || mc_size > leftover) {
  775. pr_err("error! Bad data in microcode data file\n");
  776. break;
  777. }
  778. /* For performance reasons, reuse mc area when possible */
  779. if (!mc || mc_size > curr_mc_size) {
  780. vfree(mc);
  781. mc = vmalloc(mc_size);
  782. if (!mc)
  783. break;
  784. curr_mc_size = mc_size;
  785. }
  786. if (get_ucode_data(mc, ucode_ptr, mc_size) ||
  787. microcode_sanity_check(mc, 1) < 0) {
  788. break;
  789. }
  790. csig = uci->cpu_sig.sig;
  791. cpf = uci->cpu_sig.pf;
  792. if (has_newer_microcode(mc, csig, cpf, new_rev)) {
  793. vfree(new_mc);
  794. new_rev = mc_header.rev;
  795. new_mc = mc;
  796. mc = NULL; /* trigger new vmalloc */
  797. }
  798. ucode_ptr += mc_size;
  799. leftover -= mc_size;
  800. }
  801. vfree(mc);
  802. if (leftover) {
  803. vfree(new_mc);
  804. state = UCODE_ERROR;
  805. goto out;
  806. }
  807. if (!new_mc) {
  808. state = UCODE_NFOUND;
  809. goto out;
  810. }
  811. vfree(uci->mc);
  812. uci->mc = (struct microcode_intel *)new_mc;
  813. /*
  814. * If early loading microcode is supported, save this mc into
  815. * permanent memory. So it will be loaded early when a CPU is hot added
  816. * or resumes.
  817. */
  818. save_mc_for_early(new_mc);
  819. pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n",
  820. cpu, new_rev, uci->cpu_sig.rev);
  821. out:
  822. return state;
  823. }
  824. static int get_ucode_fw(void *to, const void *from, size_t n)
  825. {
  826. memcpy(to, from, n);
  827. return 0;
  828. }
  829. static enum ucode_state request_microcode_fw(int cpu, struct device *device,
  830. bool refresh_fw)
  831. {
  832. char name[30];
  833. struct cpuinfo_x86 *c = &cpu_data(cpu);
  834. const struct firmware *firmware;
  835. enum ucode_state ret;
  836. sprintf(name, "intel-ucode/%02x-%02x-%02x",
  837. c->x86, c->x86_model, c->x86_mask);
  838. if (request_firmware_direct(&firmware, name, device)) {
  839. pr_debug("data file %s load failed\n", name);
  840. return UCODE_NFOUND;
  841. }
  842. ret = generic_load_microcode(cpu, (void *)firmware->data,
  843. firmware->size, &get_ucode_fw);
  844. release_firmware(firmware);
  845. return ret;
  846. }
  847. static int get_ucode_user(void *to, const void *from, size_t n)
  848. {
  849. return copy_from_user(to, from, n);
  850. }
  851. static enum ucode_state
  852. request_microcode_user(int cpu, const void __user *buf, size_t size)
  853. {
  854. return generic_load_microcode(cpu, (void *)buf, size, &get_ucode_user);
  855. }
  856. static void microcode_fini_cpu(int cpu)
  857. {
  858. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  859. vfree(uci->mc);
  860. uci->mc = NULL;
  861. }
  862. static struct microcode_ops microcode_intel_ops = {
  863. .request_microcode_user = request_microcode_user,
  864. .request_microcode_fw = request_microcode_fw,
  865. .collect_cpu_info = collect_cpu_info,
  866. .apply_microcode = apply_microcode_intel,
  867. .microcode_fini_cpu = microcode_fini_cpu,
  868. };
  869. struct microcode_ops * __init init_intel_microcode(void)
  870. {
  871. struct cpuinfo_x86 *c = &boot_cpu_data;
  872. if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
  873. cpu_has(c, X86_FEATURE_IA64)) {
  874. pr_err("Intel CPU family 0x%x not supported\n", c->x86);
  875. return NULL;
  876. }
  877. return &microcode_intel_ops;
  878. }