intel.c 22 KB

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