intel.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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. struct mc_saved_data *mcs;
  647. struct ucode_cpu_info uci;
  648. enum ucode_state ret;
  649. unsigned long *ptrs;
  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. collect_cpu_info_early(&uci);
  666. ret = load_microcode(mcs, ptrs, blobs_p->start, &uci);
  667. if (ret != UCODE_OK)
  668. return;
  669. apply_microcode_early(&uci, true);
  670. }
  671. void reload_ucode_intel(void)
  672. {
  673. struct ucode_cpu_info uci;
  674. enum ucode_state ret;
  675. if (!mc_saved_data.num_saved)
  676. return;
  677. collect_cpu_info_early(&uci);
  678. ret = find_microcode_patch(mc_saved_data.mc_saved,
  679. mc_saved_data.num_saved, &uci);
  680. if (ret != UCODE_OK)
  681. return;
  682. apply_microcode_early(&uci, false);
  683. }
  684. static int collect_cpu_info(int cpu_num, struct cpu_signature *csig)
  685. {
  686. struct cpuinfo_x86 *c = &cpu_data(cpu_num);
  687. unsigned int val[2];
  688. memset(csig, 0, sizeof(*csig));
  689. csig->sig = cpuid_eax(0x00000001);
  690. if ((c->x86_model >= 5) || (c->x86 > 6)) {
  691. /* get processor flags from MSR 0x17 */
  692. rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  693. csig->pf = 1 << ((val[1] >> 18) & 7);
  694. }
  695. csig->rev = c->microcode;
  696. pr_info("CPU%d sig=0x%x, pf=0x%x, revision=0x%x\n",
  697. cpu_num, csig->sig, csig->pf, csig->rev);
  698. return 0;
  699. }
  700. /*
  701. * return 0 - no update found
  702. * return 1 - found update
  703. */
  704. static int get_matching_mc(struct microcode_intel *mc, int cpu)
  705. {
  706. struct cpu_signature cpu_sig;
  707. unsigned int csig, cpf, crev;
  708. collect_cpu_info(cpu, &cpu_sig);
  709. csig = cpu_sig.sig;
  710. cpf = cpu_sig.pf;
  711. crev = cpu_sig.rev;
  712. return has_newer_microcode(mc, csig, cpf, crev);
  713. }
  714. static int apply_microcode_intel(int cpu)
  715. {
  716. struct microcode_intel *mc;
  717. struct ucode_cpu_info *uci;
  718. struct cpuinfo_x86 *c;
  719. unsigned int val[2];
  720. /* We should bind the task to the CPU */
  721. if (WARN_ON(raw_smp_processor_id() != cpu))
  722. return -1;
  723. uci = ucode_cpu_info + cpu;
  724. mc = uci->mc;
  725. if (!mc)
  726. return 0;
  727. /*
  728. * Microcode on this CPU could be updated earlier. Only apply the
  729. * microcode patch in mc when it is newer than the one on this
  730. * CPU.
  731. */
  732. if (!get_matching_mc(mc, cpu))
  733. return 0;
  734. /* write microcode via MSR 0x79 */
  735. wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits);
  736. wrmsrl(MSR_IA32_UCODE_REV, 0);
  737. /* As documented in the SDM: Do a CPUID 1 here */
  738. sync_core();
  739. /* get the current revision from MSR 0x8B */
  740. rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  741. if (val[1] != mc->hdr.rev) {
  742. pr_err("CPU%d update to revision 0x%x failed\n",
  743. cpu, mc->hdr.rev);
  744. return -1;
  745. }
  746. pr_info("CPU%d updated to revision 0x%x, date = %04x-%02x-%02x\n",
  747. cpu, val[1],
  748. mc->hdr.date & 0xffff,
  749. mc->hdr.date >> 24,
  750. (mc->hdr.date >> 16) & 0xff);
  751. c = &cpu_data(cpu);
  752. uci->cpu_sig.rev = val[1];
  753. c->microcode = val[1];
  754. return 0;
  755. }
  756. static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size,
  757. int (*get_ucode_data)(void *, const void *, size_t))
  758. {
  759. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  760. u8 *ucode_ptr = data, *new_mc = NULL, *mc = NULL;
  761. int new_rev = uci->cpu_sig.rev;
  762. unsigned int leftover = size;
  763. enum ucode_state state = UCODE_OK;
  764. unsigned int curr_mc_size = 0;
  765. unsigned int csig, cpf;
  766. while (leftover) {
  767. struct microcode_header_intel mc_header;
  768. unsigned int mc_size;
  769. if (leftover < sizeof(mc_header)) {
  770. pr_err("error! Truncated header in microcode data file\n");
  771. break;
  772. }
  773. if (get_ucode_data(&mc_header, ucode_ptr, sizeof(mc_header)))
  774. break;
  775. mc_size = get_totalsize(&mc_header);
  776. if (!mc_size || mc_size > leftover) {
  777. pr_err("error! Bad data in microcode data file\n");
  778. break;
  779. }
  780. /* For performance reasons, reuse mc area when possible */
  781. if (!mc || mc_size > curr_mc_size) {
  782. vfree(mc);
  783. mc = vmalloc(mc_size);
  784. if (!mc)
  785. break;
  786. curr_mc_size = mc_size;
  787. }
  788. if (get_ucode_data(mc, ucode_ptr, mc_size) ||
  789. microcode_sanity_check(mc, 1) < 0) {
  790. break;
  791. }
  792. csig = uci->cpu_sig.sig;
  793. cpf = uci->cpu_sig.pf;
  794. if (has_newer_microcode(mc, csig, cpf, new_rev)) {
  795. vfree(new_mc);
  796. new_rev = mc_header.rev;
  797. new_mc = mc;
  798. mc = NULL; /* trigger new vmalloc */
  799. }
  800. ucode_ptr += mc_size;
  801. leftover -= mc_size;
  802. }
  803. vfree(mc);
  804. if (leftover) {
  805. vfree(new_mc);
  806. state = UCODE_ERROR;
  807. goto out;
  808. }
  809. if (!new_mc) {
  810. state = UCODE_NFOUND;
  811. goto out;
  812. }
  813. vfree(uci->mc);
  814. uci->mc = (struct microcode_intel *)new_mc;
  815. /*
  816. * If early loading microcode is supported, save this mc into
  817. * permanent memory. So it will be loaded early when a CPU is hot added
  818. * or resumes.
  819. */
  820. save_mc_for_early(new_mc);
  821. pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n",
  822. cpu, new_rev, uci->cpu_sig.rev);
  823. out:
  824. return state;
  825. }
  826. static int get_ucode_fw(void *to, const void *from, size_t n)
  827. {
  828. memcpy(to, from, n);
  829. return 0;
  830. }
  831. static enum ucode_state request_microcode_fw(int cpu, struct device *device,
  832. bool refresh_fw)
  833. {
  834. char name[30];
  835. struct cpuinfo_x86 *c = &cpu_data(cpu);
  836. const struct firmware *firmware;
  837. enum ucode_state ret;
  838. sprintf(name, "intel-ucode/%02x-%02x-%02x",
  839. c->x86, c->x86_model, c->x86_mask);
  840. if (request_firmware_direct(&firmware, name, device)) {
  841. pr_debug("data file %s load failed\n", name);
  842. return UCODE_NFOUND;
  843. }
  844. ret = generic_load_microcode(cpu, (void *)firmware->data,
  845. firmware->size, &get_ucode_fw);
  846. release_firmware(firmware);
  847. return ret;
  848. }
  849. static int get_ucode_user(void *to, const void *from, size_t n)
  850. {
  851. return copy_from_user(to, from, n);
  852. }
  853. static enum ucode_state
  854. request_microcode_user(int cpu, const void __user *buf, size_t size)
  855. {
  856. return generic_load_microcode(cpu, (void *)buf, size, &get_ucode_user);
  857. }
  858. static void microcode_fini_cpu(int cpu)
  859. {
  860. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  861. vfree(uci->mc);
  862. uci->mc = NULL;
  863. }
  864. static struct microcode_ops microcode_intel_ops = {
  865. .request_microcode_user = request_microcode_user,
  866. .request_microcode_fw = request_microcode_fw,
  867. .collect_cpu_info = collect_cpu_info,
  868. .apply_microcode = apply_microcode_intel,
  869. .microcode_fini_cpu = microcode_fini_cpu,
  870. };
  871. struct microcode_ops * __init init_intel_microcode(void)
  872. {
  873. struct cpuinfo_x86 *c = &boot_cpu_data;
  874. if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
  875. cpu_has(c, X86_FEATURE_IA64)) {
  876. pr_err("Intel CPU family 0x%x not supported\n", c->x86);
  877. return NULL;
  878. }
  879. return &microcode_intel_ops;
  880. }