intel.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  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. #ifdef CONFIG_HOTPLUG_CPU
  396. /* Synchronization during CPU hotplug. */
  397. static DEFINE_MUTEX(x86_cpu_microcode_mutex);
  398. mutex_lock(&x86_cpu_microcode_mutex);
  399. save_microcode_patch(mc, size);
  400. show_saved_mc();
  401. mutex_unlock(&x86_cpu_microcode_mutex);
  402. #endif
  403. }
  404. static bool load_builtin_intel_microcode(struct cpio_data *cp)
  405. {
  406. unsigned int eax = 1, ebx, ecx = 0, edx;
  407. char name[30];
  408. if (IS_ENABLED(CONFIG_X86_32))
  409. return false;
  410. native_cpuid(&eax, &ebx, &ecx, &edx);
  411. sprintf(name, "intel-ucode/%02x-%02x-%02x",
  412. x86_family(eax), x86_model(eax), x86_stepping(eax));
  413. return get_builtin_firmware(cp, name);
  414. }
  415. /*
  416. * Print ucode update info.
  417. */
  418. static void
  419. print_ucode_info(struct ucode_cpu_info *uci, unsigned int date)
  420. {
  421. pr_info_once("microcode updated early to revision 0x%x, date = %04x-%02x-%02x\n",
  422. uci->cpu_sig.rev,
  423. date & 0xffff,
  424. date >> 24,
  425. (date >> 16) & 0xff);
  426. }
  427. #ifdef CONFIG_X86_32
  428. static int delay_ucode_info;
  429. static int current_mc_date;
  430. /*
  431. * Print early updated ucode info after printk works. This is delayed info dump.
  432. */
  433. void show_ucode_info_early(void)
  434. {
  435. struct ucode_cpu_info uci;
  436. if (delay_ucode_info) {
  437. collect_cpu_info_early(&uci);
  438. print_ucode_info(&uci, current_mc_date);
  439. delay_ucode_info = 0;
  440. }
  441. }
  442. /*
  443. * At this point, we can not call printk() yet. Delay printing microcode info in
  444. * show_ucode_info_early() until printk() works.
  445. */
  446. static void print_ucode(struct ucode_cpu_info *uci)
  447. {
  448. struct microcode_intel *mc;
  449. int *delay_ucode_info_p;
  450. int *current_mc_date_p;
  451. mc = uci->mc;
  452. if (!mc)
  453. return;
  454. delay_ucode_info_p = (int *)__pa_nodebug(&delay_ucode_info);
  455. current_mc_date_p = (int *)__pa_nodebug(&current_mc_date);
  456. *delay_ucode_info_p = 1;
  457. *current_mc_date_p = mc->hdr.date;
  458. }
  459. #else
  460. static inline void print_ucode(struct ucode_cpu_info *uci)
  461. {
  462. struct microcode_intel *mc;
  463. mc = uci->mc;
  464. if (!mc)
  465. return;
  466. print_ucode_info(uci, mc->hdr.date);
  467. }
  468. #endif
  469. static int apply_microcode_early(struct ucode_cpu_info *uci, bool early)
  470. {
  471. struct microcode_intel *mc;
  472. u32 rev;
  473. mc = uci->mc;
  474. if (!mc)
  475. return 0;
  476. /*
  477. * Save us the MSR write below - which is a particular expensive
  478. * operation - when the other hyperthread has updated the microcode
  479. * already.
  480. */
  481. rev = intel_get_microcode_revision();
  482. if (rev >= mc->hdr.rev) {
  483. uci->cpu_sig.rev = rev;
  484. return UCODE_OK;
  485. }
  486. /*
  487. * Writeback and invalidate caches before updating microcode to avoid
  488. * internal issues depending on what the microcode is updating.
  489. */
  490. native_wbinvd();
  491. /* write microcode via MSR 0x79 */
  492. native_wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits);
  493. rev = intel_get_microcode_revision();
  494. if (rev != mc->hdr.rev)
  495. return -1;
  496. uci->cpu_sig.rev = rev;
  497. if (early)
  498. print_ucode(uci);
  499. else
  500. print_ucode_info(uci, mc->hdr.date);
  501. return 0;
  502. }
  503. int __init save_microcode_in_initrd_intel(void)
  504. {
  505. struct ucode_cpu_info uci;
  506. struct cpio_data cp;
  507. /*
  508. * initrd is going away, clear patch ptr. We will scan the microcode one
  509. * last time before jettisoning and save a patch, if found. Then we will
  510. * update that pointer too, with a stable patch address to use when
  511. * resuming the cores.
  512. */
  513. intel_ucode_patch = NULL;
  514. if (!load_builtin_intel_microcode(&cp))
  515. cp = find_microcode_in_initrd(ucode_path, false);
  516. if (!(cp.data && cp.size))
  517. return 0;
  518. collect_cpu_info_early(&uci);
  519. scan_microcode(cp.data, cp.size, &uci, true);
  520. show_saved_mc();
  521. return 0;
  522. }
  523. /*
  524. * @res_patch, output: a pointer to the patch we found.
  525. */
  526. static struct microcode_intel *__load_ucode_intel(struct ucode_cpu_info *uci)
  527. {
  528. static const char *path;
  529. struct cpio_data cp;
  530. bool use_pa;
  531. if (IS_ENABLED(CONFIG_X86_32)) {
  532. path = (const char *)__pa_nodebug(ucode_path);
  533. use_pa = true;
  534. } else {
  535. path = ucode_path;
  536. use_pa = false;
  537. }
  538. /* try built-in microcode first */
  539. if (!load_builtin_intel_microcode(&cp))
  540. cp = find_microcode_in_initrd(path, use_pa);
  541. if (!(cp.data && cp.size))
  542. return NULL;
  543. collect_cpu_info_early(uci);
  544. return scan_microcode(cp.data, cp.size, uci, false);
  545. }
  546. void __init load_ucode_intel_bsp(void)
  547. {
  548. struct microcode_intel *patch;
  549. struct ucode_cpu_info uci;
  550. patch = __load_ucode_intel(&uci);
  551. if (!patch)
  552. return;
  553. uci.mc = patch;
  554. apply_microcode_early(&uci, true);
  555. }
  556. void load_ucode_intel_ap(void)
  557. {
  558. struct microcode_intel *patch, **iup;
  559. struct ucode_cpu_info uci;
  560. if (IS_ENABLED(CONFIG_X86_32))
  561. iup = (struct microcode_intel **) __pa_nodebug(&intel_ucode_patch);
  562. else
  563. iup = &intel_ucode_patch;
  564. reget:
  565. if (!*iup) {
  566. patch = __load_ucode_intel(&uci);
  567. if (!patch)
  568. return;
  569. *iup = patch;
  570. }
  571. uci.mc = *iup;
  572. if (apply_microcode_early(&uci, true)) {
  573. /* Mixed-silicon system? Try to refetch the proper patch: */
  574. *iup = NULL;
  575. goto reget;
  576. }
  577. }
  578. static struct microcode_intel *find_patch(struct ucode_cpu_info *uci)
  579. {
  580. struct microcode_header_intel *phdr;
  581. struct ucode_patch *iter, *tmp;
  582. list_for_each_entry_safe(iter, tmp, &microcode_cache, plist) {
  583. phdr = (struct microcode_header_intel *)iter->data;
  584. if (phdr->rev <= uci->cpu_sig.rev)
  585. continue;
  586. if (!find_matching_signature(phdr,
  587. uci->cpu_sig.sig,
  588. uci->cpu_sig.pf))
  589. continue;
  590. return iter->data;
  591. }
  592. return NULL;
  593. }
  594. void reload_ucode_intel(void)
  595. {
  596. struct microcode_intel *p;
  597. struct ucode_cpu_info uci;
  598. collect_cpu_info_early(&uci);
  599. p = find_patch(&uci);
  600. if (!p)
  601. return;
  602. uci.mc = p;
  603. apply_microcode_early(&uci, false);
  604. }
  605. static int collect_cpu_info(int cpu_num, struct cpu_signature *csig)
  606. {
  607. static struct cpu_signature prev;
  608. struct cpuinfo_x86 *c = &cpu_data(cpu_num);
  609. unsigned int val[2];
  610. memset(csig, 0, sizeof(*csig));
  611. csig->sig = cpuid_eax(0x00000001);
  612. if ((c->x86_model >= 5) || (c->x86 > 6)) {
  613. /* get processor flags from MSR 0x17 */
  614. rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  615. csig->pf = 1 << ((val[1] >> 18) & 7);
  616. }
  617. csig->rev = c->microcode;
  618. /* No extra locking on prev, races are harmless. */
  619. if (csig->sig != prev.sig || csig->pf != prev.pf || csig->rev != prev.rev) {
  620. pr_info("sig=0x%x, pf=0x%x, revision=0x%x\n",
  621. csig->sig, csig->pf, csig->rev);
  622. prev = *csig;
  623. }
  624. return 0;
  625. }
  626. static enum ucode_state apply_microcode_intel(int cpu)
  627. {
  628. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  629. struct cpuinfo_x86 *c = &cpu_data(cpu);
  630. struct microcode_intel *mc;
  631. static int prev_rev;
  632. u32 rev;
  633. /* We should bind the task to the CPU */
  634. if (WARN_ON(raw_smp_processor_id() != cpu))
  635. return UCODE_ERROR;
  636. /* Look for a newer patch in our cache: */
  637. mc = find_patch(uci);
  638. if (!mc) {
  639. mc = uci->mc;
  640. if (!mc)
  641. return UCODE_NFOUND;
  642. }
  643. /*
  644. * Save us the MSR write below - which is a particular expensive
  645. * operation - when the other hyperthread has updated the microcode
  646. * already.
  647. */
  648. rev = intel_get_microcode_revision();
  649. if (rev >= mc->hdr.rev) {
  650. uci->cpu_sig.rev = rev;
  651. c->microcode = rev;
  652. return UCODE_OK;
  653. }
  654. /*
  655. * Writeback and invalidate caches before updating microcode to avoid
  656. * internal issues depending on what the microcode is updating.
  657. */
  658. native_wbinvd();
  659. /* write microcode via MSR 0x79 */
  660. wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits);
  661. rev = intel_get_microcode_revision();
  662. if (rev != mc->hdr.rev) {
  663. pr_err("CPU%d update to revision 0x%x failed\n",
  664. cpu, mc->hdr.rev);
  665. return UCODE_ERROR;
  666. }
  667. if (rev != prev_rev) {
  668. pr_info("updated to revision 0x%x, date = %04x-%02x-%02x\n",
  669. rev,
  670. mc->hdr.date & 0xffff,
  671. mc->hdr.date >> 24,
  672. (mc->hdr.date >> 16) & 0xff);
  673. prev_rev = rev;
  674. }
  675. uci->cpu_sig.rev = rev;
  676. c->microcode = rev;
  677. return UCODE_UPDATED;
  678. }
  679. static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size,
  680. int (*get_ucode_data)(void *, const void *, size_t))
  681. {
  682. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  683. u8 *ucode_ptr = data, *new_mc = NULL, *mc = NULL;
  684. int new_rev = uci->cpu_sig.rev;
  685. unsigned int leftover = size;
  686. unsigned int curr_mc_size = 0, new_mc_size = 0;
  687. unsigned int csig, cpf;
  688. enum ucode_state ret = UCODE_OK;
  689. while (leftover) {
  690. struct microcode_header_intel mc_header;
  691. unsigned int mc_size;
  692. if (leftover < sizeof(mc_header)) {
  693. pr_err("error! Truncated header in microcode data file\n");
  694. break;
  695. }
  696. if (get_ucode_data(&mc_header, ucode_ptr, sizeof(mc_header)))
  697. break;
  698. mc_size = get_totalsize(&mc_header);
  699. if (!mc_size || mc_size > leftover) {
  700. pr_err("error! Bad data in microcode data file\n");
  701. break;
  702. }
  703. /* For performance reasons, reuse mc area when possible */
  704. if (!mc || mc_size > curr_mc_size) {
  705. vfree(mc);
  706. mc = vmalloc(mc_size);
  707. if (!mc)
  708. break;
  709. curr_mc_size = mc_size;
  710. }
  711. if (get_ucode_data(mc, ucode_ptr, mc_size) ||
  712. microcode_sanity_check(mc, 1) < 0) {
  713. break;
  714. }
  715. csig = uci->cpu_sig.sig;
  716. cpf = uci->cpu_sig.pf;
  717. if (has_newer_microcode(mc, csig, cpf, new_rev)) {
  718. vfree(new_mc);
  719. new_rev = mc_header.rev;
  720. new_mc = mc;
  721. new_mc_size = mc_size;
  722. mc = NULL; /* trigger new vmalloc */
  723. ret = UCODE_NEW;
  724. }
  725. ucode_ptr += mc_size;
  726. leftover -= mc_size;
  727. }
  728. vfree(mc);
  729. if (leftover) {
  730. vfree(new_mc);
  731. return UCODE_ERROR;
  732. }
  733. if (!new_mc)
  734. return UCODE_NFOUND;
  735. vfree(uci->mc);
  736. uci->mc = (struct microcode_intel *)new_mc;
  737. /*
  738. * If early loading microcode is supported, save this mc into
  739. * permanent memory. So it will be loaded early when a CPU is hot added
  740. * or resumes.
  741. */
  742. save_mc_for_early(new_mc, new_mc_size);
  743. pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n",
  744. cpu, new_rev, uci->cpu_sig.rev);
  745. return ret;
  746. }
  747. static int get_ucode_fw(void *to, const void *from, size_t n)
  748. {
  749. memcpy(to, from, n);
  750. return 0;
  751. }
  752. static bool is_blacklisted(unsigned int cpu)
  753. {
  754. struct cpuinfo_x86 *c = &cpu_data(cpu);
  755. /*
  756. * Late loading on model 79 with microcode revision less than 0x0b000021
  757. * and LLC size per core bigger than 2.5MB may result in a system hang.
  758. * This behavior is documented in item BDF90, #334165 (Intel Xeon
  759. * Processor E7-8800/4800 v4 Product Family).
  760. */
  761. if (c->x86 == 6 &&
  762. c->x86_model == INTEL_FAM6_BROADWELL_X &&
  763. c->x86_stepping == 0x01 &&
  764. llc_size_per_core > 2621440 &&
  765. c->microcode < 0x0b000021) {
  766. pr_err_once("Erratum BDF90: late loading with revision < 0x0b000021 (0x%x) disabled.\n", c->microcode);
  767. pr_err_once("Please consider either early loading through initrd/built-in or a potential BIOS update.\n");
  768. return true;
  769. }
  770. return false;
  771. }
  772. static enum ucode_state request_microcode_fw(int cpu, struct device *device,
  773. bool refresh_fw)
  774. {
  775. char name[30];
  776. struct cpuinfo_x86 *c = &cpu_data(cpu);
  777. const struct firmware *firmware;
  778. enum ucode_state ret;
  779. if (is_blacklisted(cpu))
  780. return UCODE_NFOUND;
  781. sprintf(name, "intel-ucode/%02x-%02x-%02x",
  782. c->x86, c->x86_model, c->x86_stepping);
  783. if (request_firmware_direct(&firmware, name, device)) {
  784. pr_debug("data file %s load failed\n", name);
  785. return UCODE_NFOUND;
  786. }
  787. ret = generic_load_microcode(cpu, (void *)firmware->data,
  788. firmware->size, &get_ucode_fw);
  789. release_firmware(firmware);
  790. return ret;
  791. }
  792. static int get_ucode_user(void *to, const void *from, size_t n)
  793. {
  794. return copy_from_user(to, from, n);
  795. }
  796. static enum ucode_state
  797. request_microcode_user(int cpu, const void __user *buf, size_t size)
  798. {
  799. if (is_blacklisted(cpu))
  800. return UCODE_NFOUND;
  801. return generic_load_microcode(cpu, (void *)buf, size, &get_ucode_user);
  802. }
  803. static struct microcode_ops microcode_intel_ops = {
  804. .request_microcode_user = request_microcode_user,
  805. .request_microcode_fw = request_microcode_fw,
  806. .collect_cpu_info = collect_cpu_info,
  807. .apply_microcode = apply_microcode_intel,
  808. };
  809. static int __init calc_llc_size_per_core(struct cpuinfo_x86 *c)
  810. {
  811. u64 llc_size = c->x86_cache_size * 1024ULL;
  812. do_div(llc_size, c->x86_max_cores);
  813. return (int)llc_size;
  814. }
  815. struct microcode_ops * __init init_intel_microcode(void)
  816. {
  817. struct cpuinfo_x86 *c = &boot_cpu_data;
  818. if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
  819. cpu_has(c, X86_FEATURE_IA64)) {
  820. pr_err("Intel CPU family 0x%x not supported\n", c->x86);
  821. return NULL;
  822. }
  823. llc_size_per_core = calc_llc_size_per_core(c);
  824. return &microcode_intel_ops;
  825. }