intel.c 26 KB

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