intel.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. /*
  2. * Intel CPU Microcode Update Driver for Linux
  3. *
  4. * Copyright (C) 2000-2006 Tigran Aivazian <aivazian.tigran@gmail.com>
  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/intel-family.h>
  35. #include <asm/processor.h>
  36. #include <asm/tlbflush.h>
  37. #include <asm/setup.h>
  38. #include <asm/msr.h>
  39. static const char ucode_path[] = "kernel/x86/microcode/GenuineIntel.bin";
  40. /* Current microcode patch used in early patching on the APs. */
  41. static struct microcode_intel *intel_ucode_patch;
  42. /* last level cache size per core */
  43. static int llc_size_per_core;
  44. static inline bool cpu_signatures_match(unsigned int s1, unsigned int p1,
  45. unsigned int s2, unsigned int p2)
  46. {
  47. if (s1 != s2)
  48. return false;
  49. /* Processor flags are either both 0 ... */
  50. if (!p1 && !p2)
  51. return true;
  52. /* ... or they intersect. */
  53. return p1 & p2;
  54. }
  55. /*
  56. * Returns 1 if update has been found, 0 otherwise.
  57. */
  58. static int find_matching_signature(void *mc, unsigned int csig, int cpf)
  59. {
  60. struct microcode_header_intel *mc_hdr = mc;
  61. struct extended_sigtable *ext_hdr;
  62. struct extended_signature *ext_sig;
  63. int i;
  64. if (cpu_signatures_match(csig, cpf, mc_hdr->sig, mc_hdr->pf))
  65. return 1;
  66. /* Look for ext. headers: */
  67. if (get_totalsize(mc_hdr) <= get_datasize(mc_hdr) + MC_HEADER_SIZE)
  68. return 0;
  69. ext_hdr = mc + get_datasize(mc_hdr) + MC_HEADER_SIZE;
  70. ext_sig = (void *)ext_hdr + EXT_HEADER_SIZE;
  71. for (i = 0; i < ext_hdr->count; i++) {
  72. if (cpu_signatures_match(csig, cpf, ext_sig->sig, ext_sig->pf))
  73. return 1;
  74. ext_sig++;
  75. }
  76. return 0;
  77. }
  78. /*
  79. * Returns 1 if update has been found, 0 otherwise.
  80. */
  81. static int has_newer_microcode(void *mc, unsigned int csig, int cpf, int new_rev)
  82. {
  83. struct microcode_header_intel *mc_hdr = mc;
  84. if (mc_hdr->rev <= new_rev)
  85. return 0;
  86. return find_matching_signature(mc, csig, cpf);
  87. }
  88. /*
  89. * Given CPU signature and a microcode patch, this function finds if the
  90. * microcode patch has matching family and model with the CPU.
  91. *
  92. * %true - if there's a match
  93. * %false - otherwise
  94. */
  95. static bool microcode_matches(struct microcode_header_intel *mc_header,
  96. unsigned long sig)
  97. {
  98. unsigned long total_size = get_totalsize(mc_header);
  99. unsigned long data_size = get_datasize(mc_header);
  100. struct extended_sigtable *ext_header;
  101. unsigned int fam_ucode, model_ucode;
  102. struct extended_signature *ext_sig;
  103. unsigned int fam, model;
  104. int ext_sigcount, i;
  105. fam = x86_family(sig);
  106. model = x86_model(sig);
  107. fam_ucode = x86_family(mc_header->sig);
  108. model_ucode = x86_model(mc_header->sig);
  109. if (fam == fam_ucode && model == model_ucode)
  110. return true;
  111. /* Look for ext. headers: */
  112. if (total_size <= data_size + MC_HEADER_SIZE)
  113. return false;
  114. ext_header = (void *) mc_header + data_size + MC_HEADER_SIZE;
  115. ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
  116. ext_sigcount = ext_header->count;
  117. for (i = 0; i < ext_sigcount; i++) {
  118. fam_ucode = x86_family(ext_sig->sig);
  119. model_ucode = x86_model(ext_sig->sig);
  120. if (fam == fam_ucode && model == model_ucode)
  121. return true;
  122. ext_sig++;
  123. }
  124. return false;
  125. }
  126. static struct ucode_patch *memdup_patch(void *data, unsigned int size)
  127. {
  128. struct ucode_patch *p;
  129. p = kzalloc(sizeof(struct ucode_patch), GFP_KERNEL);
  130. if (!p)
  131. return NULL;
  132. p->data = kmemdup(data, size, GFP_KERNEL);
  133. if (!p->data) {
  134. kfree(p);
  135. return NULL;
  136. }
  137. return p;
  138. }
  139. static void save_microcode_patch(void *data, unsigned int size)
  140. {
  141. struct microcode_header_intel *mc_hdr, *mc_saved_hdr;
  142. struct ucode_patch *iter, *tmp, *p = NULL;
  143. bool prev_found = false;
  144. unsigned int sig, pf;
  145. mc_hdr = (struct microcode_header_intel *)data;
  146. list_for_each_entry_safe(iter, tmp, &microcode_cache, plist) {
  147. mc_saved_hdr = (struct microcode_header_intel *)iter->data;
  148. sig = mc_saved_hdr->sig;
  149. pf = mc_saved_hdr->pf;
  150. if (find_matching_signature(data, sig, pf)) {
  151. prev_found = true;
  152. if (mc_hdr->rev <= mc_saved_hdr->rev)
  153. continue;
  154. p = memdup_patch(data, size);
  155. if (!p)
  156. pr_err("Error allocating buffer %p\n", data);
  157. else
  158. list_replace(&iter->plist, &p->plist);
  159. }
  160. }
  161. /*
  162. * There weren't any previous patches found in the list cache; save the
  163. * newly found.
  164. */
  165. if (!prev_found) {
  166. p = memdup_patch(data, size);
  167. if (!p)
  168. pr_err("Error allocating buffer for %p\n", data);
  169. else
  170. list_add_tail(&p->plist, &microcode_cache);
  171. }
  172. if (!p)
  173. return;
  174. /*
  175. * Save for early loading. On 32-bit, that needs to be a physical
  176. * address as the APs are running from physical addresses, before
  177. * paging has been enabled.
  178. */
  179. if (IS_ENABLED(CONFIG_X86_32))
  180. intel_ucode_patch = (struct microcode_intel *)__pa_nodebug(p->data);
  181. else
  182. intel_ucode_patch = p->data;
  183. }
  184. static int microcode_sanity_check(void *mc, int print_err)
  185. {
  186. unsigned long total_size, data_size, ext_table_size;
  187. struct microcode_header_intel *mc_header = mc;
  188. struct extended_sigtable *ext_header = NULL;
  189. u32 sum, orig_sum, ext_sigcount = 0, i;
  190. struct extended_signature *ext_sig;
  191. total_size = get_totalsize(mc_header);
  192. data_size = get_datasize(mc_header);
  193. if (data_size + MC_HEADER_SIZE > total_size) {
  194. if (print_err)
  195. pr_err("Error: bad microcode data file size.\n");
  196. return -EINVAL;
  197. }
  198. if (mc_header->ldrver != 1 || mc_header->hdrver != 1) {
  199. if (print_err)
  200. pr_err("Error: invalid/unknown microcode update format.\n");
  201. return -EINVAL;
  202. }
  203. ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
  204. if (ext_table_size) {
  205. u32 ext_table_sum = 0;
  206. u32 *ext_tablep;
  207. if ((ext_table_size < EXT_HEADER_SIZE)
  208. || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
  209. if (print_err)
  210. pr_err("Error: truncated extended signature table.\n");
  211. return -EINVAL;
  212. }
  213. ext_header = mc + MC_HEADER_SIZE + data_size;
  214. if (ext_table_size != exttable_size(ext_header)) {
  215. if (print_err)
  216. pr_err("Error: extended signature table size mismatch.\n");
  217. return -EFAULT;
  218. }
  219. ext_sigcount = ext_header->count;
  220. /*
  221. * Check extended table checksum: the sum of all dwords that
  222. * comprise a valid table must be 0.
  223. */
  224. ext_tablep = (u32 *)ext_header;
  225. i = ext_table_size / sizeof(u32);
  226. while (i--)
  227. ext_table_sum += ext_tablep[i];
  228. if (ext_table_sum) {
  229. if (print_err)
  230. pr_warn("Bad extended signature table checksum, aborting.\n");
  231. return -EINVAL;
  232. }
  233. }
  234. /*
  235. * Calculate the checksum of update data and header. The checksum of
  236. * valid update data and header including the extended signature table
  237. * must be 0.
  238. */
  239. orig_sum = 0;
  240. i = (MC_HEADER_SIZE + data_size) / sizeof(u32);
  241. while (i--)
  242. orig_sum += ((u32 *)mc)[i];
  243. if (orig_sum) {
  244. if (print_err)
  245. pr_err("Bad microcode data checksum, aborting.\n");
  246. return -EINVAL;
  247. }
  248. if (!ext_table_size)
  249. return 0;
  250. /*
  251. * Check extended signature checksum: 0 => valid.
  252. */
  253. for (i = 0; i < ext_sigcount; i++) {
  254. ext_sig = (void *)ext_header + EXT_HEADER_SIZE +
  255. EXT_SIGNATURE_SIZE * i;
  256. sum = (mc_header->sig + mc_header->pf + mc_header->cksum) -
  257. (ext_sig->sig + ext_sig->pf + ext_sig->cksum);
  258. if (sum) {
  259. if (print_err)
  260. pr_err("Bad extended signature checksum, aborting.\n");
  261. return -EINVAL;
  262. }
  263. }
  264. return 0;
  265. }
  266. /*
  267. * Get microcode matching with BSP's model. Only CPUs with the same model as
  268. * BSP can stay in the platform.
  269. */
  270. static struct microcode_intel *
  271. scan_microcode(void *data, size_t size, struct ucode_cpu_info *uci, bool save)
  272. {
  273. struct microcode_header_intel *mc_header;
  274. struct microcode_intel *patch = NULL;
  275. unsigned int mc_size;
  276. while (size) {
  277. if (size < sizeof(struct microcode_header_intel))
  278. break;
  279. mc_header = (struct microcode_header_intel *)data;
  280. mc_size = get_totalsize(mc_header);
  281. if (!mc_size ||
  282. mc_size > size ||
  283. microcode_sanity_check(data, 0) < 0)
  284. break;
  285. size -= mc_size;
  286. if (!microcode_matches(mc_header, uci->cpu_sig.sig)) {
  287. data += mc_size;
  288. continue;
  289. }
  290. if (save) {
  291. save_microcode_patch(data, mc_size);
  292. goto next;
  293. }
  294. if (!patch) {
  295. if (!has_newer_microcode(data,
  296. uci->cpu_sig.sig,
  297. uci->cpu_sig.pf,
  298. uci->cpu_sig.rev))
  299. goto next;
  300. } else {
  301. struct microcode_header_intel *phdr = &patch->hdr;
  302. if (!has_newer_microcode(data,
  303. phdr->sig,
  304. phdr->pf,
  305. phdr->rev))
  306. goto next;
  307. }
  308. /* We have a newer patch, save it. */
  309. patch = data;
  310. next:
  311. data += mc_size;
  312. }
  313. if (size)
  314. return NULL;
  315. return patch;
  316. }
  317. static int collect_cpu_info_early(struct ucode_cpu_info *uci)
  318. {
  319. unsigned int val[2];
  320. unsigned int family, model;
  321. struct cpu_signature csig = { 0 };
  322. unsigned int eax, ebx, ecx, edx;
  323. memset(uci, 0, sizeof(*uci));
  324. eax = 0x00000001;
  325. ecx = 0;
  326. native_cpuid(&eax, &ebx, &ecx, &edx);
  327. csig.sig = eax;
  328. family = x86_family(eax);
  329. model = x86_model(eax);
  330. if ((model >= 5) || (family > 6)) {
  331. /* get processor flags from MSR 0x17 */
  332. native_rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  333. csig.pf = 1 << ((val[1] >> 18) & 7);
  334. }
  335. csig.rev = intel_get_microcode_revision();
  336. uci->cpu_sig = csig;
  337. uci->valid = 1;
  338. return 0;
  339. }
  340. static void show_saved_mc(void)
  341. {
  342. #ifdef DEBUG
  343. int i = 0, j;
  344. unsigned int sig, pf, rev, total_size, data_size, date;
  345. struct ucode_cpu_info uci;
  346. struct ucode_patch *p;
  347. if (list_empty(&microcode_cache)) {
  348. pr_debug("no microcode data saved.\n");
  349. return;
  350. }
  351. collect_cpu_info_early(&uci);
  352. sig = uci.cpu_sig.sig;
  353. pf = uci.cpu_sig.pf;
  354. rev = uci.cpu_sig.rev;
  355. pr_debug("CPU: sig=0x%x, pf=0x%x, rev=0x%x\n", sig, pf, rev);
  356. list_for_each_entry(p, &microcode_cache, plist) {
  357. struct microcode_header_intel *mc_saved_header;
  358. struct extended_sigtable *ext_header;
  359. struct extended_signature *ext_sig;
  360. int ext_sigcount;
  361. mc_saved_header = (struct microcode_header_intel *)p->data;
  362. sig = mc_saved_header->sig;
  363. pf = mc_saved_header->pf;
  364. rev = mc_saved_header->rev;
  365. date = mc_saved_header->date;
  366. total_size = get_totalsize(mc_saved_header);
  367. data_size = get_datasize(mc_saved_header);
  368. pr_debug("mc_saved[%d]: sig=0x%x, pf=0x%x, rev=0x%x, total size=0x%x, date = %04x-%02x-%02x\n",
  369. i++, sig, pf, rev, total_size,
  370. date & 0xffff,
  371. date >> 24,
  372. (date >> 16) & 0xff);
  373. /* Look for ext. headers: */
  374. if (total_size <= data_size + MC_HEADER_SIZE)
  375. continue;
  376. ext_header = (void *)mc_saved_header + data_size + MC_HEADER_SIZE;
  377. ext_sigcount = ext_header->count;
  378. ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
  379. for (j = 0; j < ext_sigcount; j++) {
  380. sig = ext_sig->sig;
  381. pf = ext_sig->pf;
  382. pr_debug("\tExtended[%d]: sig=0x%x, pf=0x%x\n",
  383. j, sig, pf);
  384. ext_sig++;
  385. }
  386. }
  387. #endif
  388. }
  389. /*
  390. * Save this microcode patch. It will be loaded early when a CPU is
  391. * hot-added or resumes.
  392. */
  393. static void save_mc_for_early(u8 *mc, unsigned int size)
  394. {
  395. /* Synchronization during CPU hotplug. */
  396. static DEFINE_MUTEX(x86_cpu_microcode_mutex);
  397. mutex_lock(&x86_cpu_microcode_mutex);
  398. save_microcode_patch(mc, size);
  399. show_saved_mc();
  400. mutex_unlock(&x86_cpu_microcode_mutex);
  401. }
  402. static bool load_builtin_intel_microcode(struct cpio_data *cp)
  403. {
  404. unsigned int eax = 1, ebx, ecx = 0, edx;
  405. char name[30];
  406. if (IS_ENABLED(CONFIG_X86_32))
  407. return false;
  408. native_cpuid(&eax, &ebx, &ecx, &edx);
  409. sprintf(name, "intel-ucode/%02x-%02x-%02x",
  410. x86_family(eax), x86_model(eax), x86_stepping(eax));
  411. return get_builtin_firmware(cp, name);
  412. }
  413. /*
  414. * Print ucode update info.
  415. */
  416. static void
  417. print_ucode_info(struct ucode_cpu_info *uci, unsigned int date)
  418. {
  419. pr_info_once("microcode updated early to revision 0x%x, date = %04x-%02x-%02x\n",
  420. uci->cpu_sig.rev,
  421. date & 0xffff,
  422. date >> 24,
  423. (date >> 16) & 0xff);
  424. }
  425. #ifdef CONFIG_X86_32
  426. static int delay_ucode_info;
  427. static int current_mc_date;
  428. /*
  429. * Print early updated ucode info after printk works. This is delayed info dump.
  430. */
  431. void show_ucode_info_early(void)
  432. {
  433. struct ucode_cpu_info uci;
  434. if (delay_ucode_info) {
  435. collect_cpu_info_early(&uci);
  436. print_ucode_info(&uci, current_mc_date);
  437. delay_ucode_info = 0;
  438. }
  439. }
  440. /*
  441. * At this point, we can not call printk() yet. Delay printing microcode info in
  442. * show_ucode_info_early() until printk() works.
  443. */
  444. static void print_ucode(struct ucode_cpu_info *uci)
  445. {
  446. struct microcode_intel *mc;
  447. int *delay_ucode_info_p;
  448. int *current_mc_date_p;
  449. mc = uci->mc;
  450. if (!mc)
  451. return;
  452. delay_ucode_info_p = (int *)__pa_nodebug(&delay_ucode_info);
  453. current_mc_date_p = (int *)__pa_nodebug(&current_mc_date);
  454. *delay_ucode_info_p = 1;
  455. *current_mc_date_p = mc->hdr.date;
  456. }
  457. #else
  458. static inline void print_ucode(struct ucode_cpu_info *uci)
  459. {
  460. struct microcode_intel *mc;
  461. mc = uci->mc;
  462. if (!mc)
  463. return;
  464. print_ucode_info(uci, mc->hdr.date);
  465. }
  466. #endif
  467. static int apply_microcode_early(struct ucode_cpu_info *uci, bool early)
  468. {
  469. struct microcode_intel *mc;
  470. u32 rev;
  471. mc = uci->mc;
  472. if (!mc)
  473. return 0;
  474. /*
  475. * Save us the MSR write below - which is a particular expensive
  476. * operation - when the other hyperthread has updated the microcode
  477. * already.
  478. */
  479. rev = intel_get_microcode_revision();
  480. if (rev >= mc->hdr.rev) {
  481. uci->cpu_sig.rev = rev;
  482. return UCODE_OK;
  483. }
  484. /*
  485. * Writeback and invalidate caches before updating microcode to avoid
  486. * internal issues depending on what the microcode is updating.
  487. */
  488. native_wbinvd();
  489. /* write microcode via MSR 0x79 */
  490. native_wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits);
  491. rev = intel_get_microcode_revision();
  492. if (rev != mc->hdr.rev)
  493. return -1;
  494. uci->cpu_sig.rev = rev;
  495. if (early)
  496. print_ucode(uci);
  497. else
  498. print_ucode_info(uci, mc->hdr.date);
  499. return 0;
  500. }
  501. int __init save_microcode_in_initrd_intel(void)
  502. {
  503. struct ucode_cpu_info uci;
  504. struct cpio_data cp;
  505. /*
  506. * initrd is going away, clear patch ptr. We will scan the microcode one
  507. * last time before jettisoning and save a patch, if found. Then we will
  508. * update that pointer too, with a stable patch address to use when
  509. * resuming the cores.
  510. */
  511. intel_ucode_patch = NULL;
  512. if (!load_builtin_intel_microcode(&cp))
  513. cp = find_microcode_in_initrd(ucode_path, false);
  514. if (!(cp.data && cp.size))
  515. return 0;
  516. collect_cpu_info_early(&uci);
  517. scan_microcode(cp.data, cp.size, &uci, true);
  518. show_saved_mc();
  519. return 0;
  520. }
  521. /*
  522. * @res_patch, output: a pointer to the patch we found.
  523. */
  524. static struct microcode_intel *__load_ucode_intel(struct ucode_cpu_info *uci)
  525. {
  526. static const char *path;
  527. struct cpio_data cp;
  528. bool use_pa;
  529. if (IS_ENABLED(CONFIG_X86_32)) {
  530. path = (const char *)__pa_nodebug(ucode_path);
  531. use_pa = true;
  532. } else {
  533. path = ucode_path;
  534. use_pa = false;
  535. }
  536. /* try built-in microcode first */
  537. if (!load_builtin_intel_microcode(&cp))
  538. cp = find_microcode_in_initrd(path, use_pa);
  539. if (!(cp.data && cp.size))
  540. return NULL;
  541. collect_cpu_info_early(uci);
  542. return scan_microcode(cp.data, cp.size, uci, false);
  543. }
  544. void __init load_ucode_intel_bsp(void)
  545. {
  546. struct microcode_intel *patch;
  547. struct ucode_cpu_info uci;
  548. patch = __load_ucode_intel(&uci);
  549. if (!patch)
  550. return;
  551. uci.mc = patch;
  552. apply_microcode_early(&uci, true);
  553. }
  554. void load_ucode_intel_ap(void)
  555. {
  556. struct microcode_intel *patch, **iup;
  557. struct ucode_cpu_info uci;
  558. if (IS_ENABLED(CONFIG_X86_32))
  559. iup = (struct microcode_intel **) __pa_nodebug(&intel_ucode_patch);
  560. else
  561. iup = &intel_ucode_patch;
  562. reget:
  563. if (!*iup) {
  564. patch = __load_ucode_intel(&uci);
  565. if (!patch)
  566. return;
  567. *iup = patch;
  568. }
  569. uci.mc = *iup;
  570. if (apply_microcode_early(&uci, true)) {
  571. /* Mixed-silicon system? Try to refetch the proper patch: */
  572. *iup = NULL;
  573. goto reget;
  574. }
  575. }
  576. static struct microcode_intel *find_patch(struct ucode_cpu_info *uci)
  577. {
  578. struct microcode_header_intel *phdr;
  579. struct ucode_patch *iter, *tmp;
  580. list_for_each_entry_safe(iter, tmp, &microcode_cache, plist) {
  581. phdr = (struct microcode_header_intel *)iter->data;
  582. if (phdr->rev <= uci->cpu_sig.rev)
  583. continue;
  584. if (!find_matching_signature(phdr,
  585. uci->cpu_sig.sig,
  586. uci->cpu_sig.pf))
  587. continue;
  588. return iter->data;
  589. }
  590. return NULL;
  591. }
  592. void reload_ucode_intel(void)
  593. {
  594. struct microcode_intel *p;
  595. struct ucode_cpu_info uci;
  596. collect_cpu_info_early(&uci);
  597. p = find_patch(&uci);
  598. if (!p)
  599. return;
  600. uci.mc = p;
  601. apply_microcode_early(&uci, false);
  602. }
  603. static int collect_cpu_info(int cpu_num, struct cpu_signature *csig)
  604. {
  605. static struct cpu_signature prev;
  606. struct cpuinfo_x86 *c = &cpu_data(cpu_num);
  607. unsigned int val[2];
  608. memset(csig, 0, sizeof(*csig));
  609. csig->sig = cpuid_eax(0x00000001);
  610. if ((c->x86_model >= 5) || (c->x86 > 6)) {
  611. /* get processor flags from MSR 0x17 */
  612. rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  613. csig->pf = 1 << ((val[1] >> 18) & 7);
  614. }
  615. csig->rev = c->microcode;
  616. /* No extra locking on prev, races are harmless. */
  617. if (csig->sig != prev.sig || csig->pf != prev.pf || csig->rev != prev.rev) {
  618. pr_info("sig=0x%x, pf=0x%x, revision=0x%x\n",
  619. csig->sig, csig->pf, csig->rev);
  620. prev = *csig;
  621. }
  622. return 0;
  623. }
  624. static enum ucode_state apply_microcode_intel(int cpu)
  625. {
  626. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  627. struct cpuinfo_x86 *c = &cpu_data(cpu);
  628. struct microcode_intel *mc;
  629. static int prev_rev;
  630. u32 rev;
  631. /* We should bind the task to the CPU */
  632. if (WARN_ON(raw_smp_processor_id() != cpu))
  633. return UCODE_ERROR;
  634. /* Look for a newer patch in our cache: */
  635. mc = find_patch(uci);
  636. if (!mc) {
  637. mc = uci->mc;
  638. if (!mc)
  639. return UCODE_NFOUND;
  640. }
  641. /*
  642. * Save us the MSR write below - which is a particular expensive
  643. * operation - when the other hyperthread has updated the microcode
  644. * already.
  645. */
  646. rev = intel_get_microcode_revision();
  647. if (rev >= mc->hdr.rev) {
  648. uci->cpu_sig.rev = rev;
  649. c->microcode = rev;
  650. return UCODE_OK;
  651. }
  652. /*
  653. * Writeback and invalidate caches before updating microcode to avoid
  654. * internal issues depending on what the microcode is updating.
  655. */
  656. native_wbinvd();
  657. /* write microcode via MSR 0x79 */
  658. wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits);
  659. rev = intel_get_microcode_revision();
  660. if (rev != mc->hdr.rev) {
  661. pr_err("CPU%d update to revision 0x%x failed\n",
  662. cpu, mc->hdr.rev);
  663. return UCODE_ERROR;
  664. }
  665. if (rev != prev_rev) {
  666. pr_info("updated to revision 0x%x, date = %04x-%02x-%02x\n",
  667. rev,
  668. mc->hdr.date & 0xffff,
  669. mc->hdr.date >> 24,
  670. (mc->hdr.date >> 16) & 0xff);
  671. prev_rev = rev;
  672. }
  673. uci->cpu_sig.rev = rev;
  674. c->microcode = rev;
  675. return UCODE_UPDATED;
  676. }
  677. static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size,
  678. int (*get_ucode_data)(void *, const void *, size_t))
  679. {
  680. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  681. u8 *ucode_ptr = data, *new_mc = NULL, *mc = NULL;
  682. int new_rev = uci->cpu_sig.rev;
  683. unsigned int leftover = size;
  684. unsigned int curr_mc_size = 0, new_mc_size = 0;
  685. unsigned int csig, cpf;
  686. enum ucode_state ret = UCODE_OK;
  687. while (leftover) {
  688. struct microcode_header_intel mc_header;
  689. unsigned int mc_size;
  690. if (leftover < sizeof(mc_header)) {
  691. pr_err("error! Truncated header in microcode data file\n");
  692. break;
  693. }
  694. if (get_ucode_data(&mc_header, ucode_ptr, sizeof(mc_header)))
  695. break;
  696. mc_size = get_totalsize(&mc_header);
  697. if (!mc_size || mc_size > leftover) {
  698. pr_err("error! Bad data in microcode data file\n");
  699. break;
  700. }
  701. /* For performance reasons, reuse mc area when possible */
  702. if (!mc || mc_size > curr_mc_size) {
  703. vfree(mc);
  704. mc = vmalloc(mc_size);
  705. if (!mc)
  706. break;
  707. curr_mc_size = mc_size;
  708. }
  709. if (get_ucode_data(mc, ucode_ptr, mc_size) ||
  710. microcode_sanity_check(mc, 1) < 0) {
  711. break;
  712. }
  713. csig = uci->cpu_sig.sig;
  714. cpf = uci->cpu_sig.pf;
  715. if (has_newer_microcode(mc, csig, cpf, new_rev)) {
  716. vfree(new_mc);
  717. new_rev = mc_header.rev;
  718. new_mc = mc;
  719. new_mc_size = mc_size;
  720. mc = NULL; /* trigger new vmalloc */
  721. ret = UCODE_NEW;
  722. }
  723. ucode_ptr += mc_size;
  724. leftover -= mc_size;
  725. }
  726. vfree(mc);
  727. if (leftover) {
  728. vfree(new_mc);
  729. return UCODE_ERROR;
  730. }
  731. if (!new_mc)
  732. return UCODE_NFOUND;
  733. vfree(uci->mc);
  734. uci->mc = (struct microcode_intel *)new_mc;
  735. /*
  736. * If early loading microcode is supported, save this mc into
  737. * permanent memory. So it will be loaded early when a CPU is hot added
  738. * or resumes.
  739. */
  740. save_mc_for_early(new_mc, new_mc_size);
  741. pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n",
  742. cpu, new_rev, uci->cpu_sig.rev);
  743. return ret;
  744. }
  745. static int get_ucode_fw(void *to, const void *from, size_t n)
  746. {
  747. memcpy(to, from, n);
  748. return 0;
  749. }
  750. static bool is_blacklisted(unsigned int cpu)
  751. {
  752. struct cpuinfo_x86 *c = &cpu_data(cpu);
  753. /*
  754. * Late loading on model 79 with microcode revision less than 0x0b000021
  755. * and LLC size per core bigger than 2.5MB may result in a system hang.
  756. * This behavior is documented in item BDF90, #334165 (Intel Xeon
  757. * Processor E7-8800/4800 v4 Product Family).
  758. */
  759. if (c->x86 == 6 &&
  760. c->x86_model == INTEL_FAM6_BROADWELL_X &&
  761. c->x86_stepping == 0x01 &&
  762. llc_size_per_core > 2621440 &&
  763. c->microcode < 0x0b000021) {
  764. pr_err_once("Erratum BDF90: late loading with revision < 0x0b000021 (0x%x) disabled.\n", c->microcode);
  765. pr_err_once("Please consider either early loading through initrd/built-in or a potential BIOS update.\n");
  766. return true;
  767. }
  768. return false;
  769. }
  770. static enum ucode_state request_microcode_fw(int cpu, struct device *device,
  771. bool refresh_fw)
  772. {
  773. char name[30];
  774. struct cpuinfo_x86 *c = &cpu_data(cpu);
  775. const struct firmware *firmware;
  776. enum ucode_state ret;
  777. if (is_blacklisted(cpu))
  778. return UCODE_NFOUND;
  779. sprintf(name, "intel-ucode/%02x-%02x-%02x",
  780. c->x86, c->x86_model, c->x86_stepping);
  781. if (request_firmware_direct(&firmware, name, device)) {
  782. pr_debug("data file %s load failed\n", name);
  783. return UCODE_NFOUND;
  784. }
  785. ret = generic_load_microcode(cpu, (void *)firmware->data,
  786. firmware->size, &get_ucode_fw);
  787. release_firmware(firmware);
  788. return ret;
  789. }
  790. static int get_ucode_user(void *to, const void *from, size_t n)
  791. {
  792. return copy_from_user(to, from, n);
  793. }
  794. static enum ucode_state
  795. request_microcode_user(int cpu, const void __user *buf, size_t size)
  796. {
  797. if (is_blacklisted(cpu))
  798. return UCODE_NFOUND;
  799. return generic_load_microcode(cpu, (void *)buf, size, &get_ucode_user);
  800. }
  801. static struct microcode_ops microcode_intel_ops = {
  802. .request_microcode_user = request_microcode_user,
  803. .request_microcode_fw = request_microcode_fw,
  804. .collect_cpu_info = collect_cpu_info,
  805. .apply_microcode = apply_microcode_intel,
  806. };
  807. static int __init calc_llc_size_per_core(struct cpuinfo_x86 *c)
  808. {
  809. u64 llc_size = c->x86_cache_size * 1024ULL;
  810. do_div(llc_size, c->x86_max_cores);
  811. return (int)llc_size;
  812. }
  813. struct microcode_ops * __init init_intel_microcode(void)
  814. {
  815. struct cpuinfo_x86 *c = &boot_cpu_data;
  816. if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
  817. cpu_has(c, X86_FEATURE_IA64)) {
  818. pr_err("Intel CPU family 0x%x not supported\n", c->x86);
  819. return NULL;
  820. }
  821. llc_size_per_core = calc_llc_size_per_core(c);
  822. return &microcode_intel_ops;
  823. }