intel.c 23 KB

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