intel.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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. /* write microcode via MSR 0x79 */
  477. native_wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits);
  478. rev = intel_get_microcode_revision();
  479. if (rev != mc->hdr.rev)
  480. return -1;
  481. uci->cpu_sig.rev = rev;
  482. if (early)
  483. print_ucode(uci);
  484. else
  485. print_ucode_info(uci, mc->hdr.date);
  486. return 0;
  487. }
  488. int __init save_microcode_in_initrd_intel(void)
  489. {
  490. struct ucode_cpu_info uci;
  491. struct cpio_data cp;
  492. /*
  493. * initrd is going away, clear patch ptr. We will scan the microcode one
  494. * last time before jettisoning and save a patch, if found. Then we will
  495. * update that pointer too, with a stable patch address to use when
  496. * resuming the cores.
  497. */
  498. intel_ucode_patch = NULL;
  499. if (!load_builtin_intel_microcode(&cp))
  500. cp = find_microcode_in_initrd(ucode_path, false);
  501. if (!(cp.data && cp.size))
  502. return 0;
  503. collect_cpu_info_early(&uci);
  504. scan_microcode(cp.data, cp.size, &uci, true);
  505. show_saved_mc();
  506. return 0;
  507. }
  508. /*
  509. * @res_patch, output: a pointer to the patch we found.
  510. */
  511. static struct microcode_intel *__load_ucode_intel(struct ucode_cpu_info *uci)
  512. {
  513. static const char *path;
  514. struct cpio_data cp;
  515. bool use_pa;
  516. if (IS_ENABLED(CONFIG_X86_32)) {
  517. path = (const char *)__pa_nodebug(ucode_path);
  518. use_pa = true;
  519. } else {
  520. path = ucode_path;
  521. use_pa = false;
  522. }
  523. /* try built-in microcode first */
  524. if (!load_builtin_intel_microcode(&cp))
  525. cp = find_microcode_in_initrd(path, use_pa);
  526. if (!(cp.data && cp.size))
  527. return NULL;
  528. collect_cpu_info_early(uci);
  529. return scan_microcode(cp.data, cp.size, uci, false);
  530. }
  531. void __init load_ucode_intel_bsp(void)
  532. {
  533. struct microcode_intel *patch;
  534. struct ucode_cpu_info uci;
  535. patch = __load_ucode_intel(&uci);
  536. if (!patch)
  537. return;
  538. uci.mc = patch;
  539. apply_microcode_early(&uci, true);
  540. }
  541. void load_ucode_intel_ap(void)
  542. {
  543. struct microcode_intel *patch, **iup;
  544. struct ucode_cpu_info uci;
  545. if (IS_ENABLED(CONFIG_X86_32))
  546. iup = (struct microcode_intel **) __pa_nodebug(&intel_ucode_patch);
  547. else
  548. iup = &intel_ucode_patch;
  549. reget:
  550. if (!*iup) {
  551. patch = __load_ucode_intel(&uci);
  552. if (!patch)
  553. return;
  554. *iup = patch;
  555. }
  556. uci.mc = *iup;
  557. if (apply_microcode_early(&uci, true)) {
  558. /* Mixed-silicon system? Try to refetch the proper patch: */
  559. *iup = NULL;
  560. goto reget;
  561. }
  562. }
  563. static struct microcode_intel *find_patch(struct ucode_cpu_info *uci)
  564. {
  565. struct microcode_header_intel *phdr;
  566. struct ucode_patch *iter, *tmp;
  567. list_for_each_entry_safe(iter, tmp, &microcode_cache, plist) {
  568. phdr = (struct microcode_header_intel *)iter->data;
  569. if (phdr->rev <= uci->cpu_sig.rev)
  570. continue;
  571. if (!find_matching_signature(phdr,
  572. uci->cpu_sig.sig,
  573. uci->cpu_sig.pf))
  574. continue;
  575. return iter->data;
  576. }
  577. return NULL;
  578. }
  579. void reload_ucode_intel(void)
  580. {
  581. struct microcode_intel *p;
  582. struct ucode_cpu_info uci;
  583. collect_cpu_info_early(&uci);
  584. p = find_patch(&uci);
  585. if (!p)
  586. return;
  587. uci.mc = p;
  588. apply_microcode_early(&uci, false);
  589. }
  590. static int collect_cpu_info(int cpu_num, struct cpu_signature *csig)
  591. {
  592. static struct cpu_signature prev;
  593. struct cpuinfo_x86 *c = &cpu_data(cpu_num);
  594. unsigned int val[2];
  595. memset(csig, 0, sizeof(*csig));
  596. csig->sig = cpuid_eax(0x00000001);
  597. if ((c->x86_model >= 5) || (c->x86 > 6)) {
  598. /* get processor flags from MSR 0x17 */
  599. rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  600. csig->pf = 1 << ((val[1] >> 18) & 7);
  601. }
  602. csig->rev = c->microcode;
  603. /* No extra locking on prev, races are harmless. */
  604. if (csig->sig != prev.sig || csig->pf != prev.pf || csig->rev != prev.rev) {
  605. pr_info("sig=0x%x, pf=0x%x, revision=0x%x\n",
  606. csig->sig, csig->pf, csig->rev);
  607. prev = *csig;
  608. }
  609. return 0;
  610. }
  611. static int apply_microcode_intel(int cpu)
  612. {
  613. struct microcode_intel *mc;
  614. struct ucode_cpu_info *uci;
  615. struct cpuinfo_x86 *c;
  616. static int prev_rev;
  617. u32 rev;
  618. /* We should bind the task to the CPU */
  619. if (WARN_ON(raw_smp_processor_id() != cpu))
  620. return -1;
  621. uci = ucode_cpu_info + cpu;
  622. mc = uci->mc;
  623. if (!mc) {
  624. /* Look for a newer patch in our cache: */
  625. mc = find_patch(uci);
  626. if (!mc)
  627. return 0;
  628. }
  629. /* write microcode via MSR 0x79 */
  630. wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits);
  631. rev = intel_get_microcode_revision();
  632. if (rev != mc->hdr.rev) {
  633. pr_err("CPU%d update to revision 0x%x failed\n",
  634. cpu, mc->hdr.rev);
  635. return -1;
  636. }
  637. if (rev != prev_rev) {
  638. pr_info("updated to revision 0x%x, date = %04x-%02x-%02x\n",
  639. rev,
  640. mc->hdr.date & 0xffff,
  641. mc->hdr.date >> 24,
  642. (mc->hdr.date >> 16) & 0xff);
  643. prev_rev = rev;
  644. }
  645. c = &cpu_data(cpu);
  646. uci->cpu_sig.rev = rev;
  647. c->microcode = rev;
  648. return 0;
  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. while (leftover) {
  660. struct microcode_header_intel mc_header;
  661. unsigned int mc_size;
  662. if (leftover < sizeof(mc_header)) {
  663. pr_err("error! Truncated header in microcode data file\n");
  664. break;
  665. }
  666. if (get_ucode_data(&mc_header, ucode_ptr, sizeof(mc_header)))
  667. break;
  668. mc_size = get_totalsize(&mc_header);
  669. if (!mc_size || mc_size > leftover) {
  670. pr_err("error! Bad data in microcode data file\n");
  671. break;
  672. }
  673. /* For performance reasons, reuse mc area when possible */
  674. if (!mc || mc_size > curr_mc_size) {
  675. vfree(mc);
  676. mc = vmalloc(mc_size);
  677. if (!mc)
  678. break;
  679. curr_mc_size = mc_size;
  680. }
  681. if (get_ucode_data(mc, ucode_ptr, mc_size) ||
  682. microcode_sanity_check(mc, 1) < 0) {
  683. break;
  684. }
  685. csig = uci->cpu_sig.sig;
  686. cpf = uci->cpu_sig.pf;
  687. if (has_newer_microcode(mc, csig, cpf, new_rev)) {
  688. vfree(new_mc);
  689. new_rev = mc_header.rev;
  690. new_mc = mc;
  691. new_mc_size = mc_size;
  692. mc = NULL; /* trigger new vmalloc */
  693. }
  694. ucode_ptr += mc_size;
  695. leftover -= mc_size;
  696. }
  697. vfree(mc);
  698. if (leftover) {
  699. vfree(new_mc);
  700. return UCODE_ERROR;
  701. }
  702. if (!new_mc)
  703. return UCODE_NFOUND;
  704. vfree(uci->mc);
  705. uci->mc = (struct microcode_intel *)new_mc;
  706. /*
  707. * If early loading microcode is supported, save this mc into
  708. * permanent memory. So it will be loaded early when a CPU is hot added
  709. * or resumes.
  710. */
  711. save_mc_for_early(new_mc, new_mc_size);
  712. pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n",
  713. cpu, new_rev, uci->cpu_sig.rev);
  714. return UCODE_OK;
  715. }
  716. static int get_ucode_fw(void *to, const void *from, size_t n)
  717. {
  718. memcpy(to, from, n);
  719. return 0;
  720. }
  721. static bool is_blacklisted(unsigned int cpu)
  722. {
  723. struct cpuinfo_x86 *c = &cpu_data(cpu);
  724. /*
  725. * Late loading on model 79 with microcode revision less than 0x0b000021
  726. * and LLC size per core bigger than 2.5MB may result in a system hang.
  727. * This behavior is documented in item BDF90, #334165 (Intel Xeon
  728. * Processor E7-8800/4800 v4 Product Family).
  729. */
  730. if (c->x86 == 6 &&
  731. c->x86_model == INTEL_FAM6_BROADWELL_X &&
  732. c->x86_stepping == 0x01 &&
  733. llc_size_per_core > 2621440 &&
  734. c->microcode < 0x0b000021) {
  735. pr_err_once("Erratum BDF90: late loading with revision < 0x0b000021 (0x%x) disabled.\n", c->microcode);
  736. pr_err_once("Please consider either early loading through initrd/built-in or a potential BIOS update.\n");
  737. return true;
  738. }
  739. return false;
  740. }
  741. static enum ucode_state request_microcode_fw(int cpu, struct device *device,
  742. bool refresh_fw)
  743. {
  744. char name[30];
  745. struct cpuinfo_x86 *c = &cpu_data(cpu);
  746. const struct firmware *firmware;
  747. enum ucode_state ret;
  748. if (is_blacklisted(cpu))
  749. return UCODE_NFOUND;
  750. sprintf(name, "intel-ucode/%02x-%02x-%02x",
  751. c->x86, c->x86_model, c->x86_stepping);
  752. if (request_firmware_direct(&firmware, name, device)) {
  753. pr_debug("data file %s load failed\n", name);
  754. return UCODE_NFOUND;
  755. }
  756. ret = generic_load_microcode(cpu, (void *)firmware->data,
  757. firmware->size, &get_ucode_fw);
  758. release_firmware(firmware);
  759. return ret;
  760. }
  761. static int get_ucode_user(void *to, const void *from, size_t n)
  762. {
  763. return copy_from_user(to, from, n);
  764. }
  765. static enum ucode_state
  766. request_microcode_user(int cpu, const void __user *buf, size_t size)
  767. {
  768. if (is_blacklisted(cpu))
  769. return UCODE_NFOUND;
  770. return generic_load_microcode(cpu, (void *)buf, size, &get_ucode_user);
  771. }
  772. static struct microcode_ops microcode_intel_ops = {
  773. .request_microcode_user = request_microcode_user,
  774. .request_microcode_fw = request_microcode_fw,
  775. .collect_cpu_info = collect_cpu_info,
  776. .apply_microcode = apply_microcode_intel,
  777. };
  778. static int __init calc_llc_size_per_core(struct cpuinfo_x86 *c)
  779. {
  780. u64 llc_size = c->x86_cache_size * 1024ULL;
  781. do_div(llc_size, c->x86_max_cores);
  782. return (int)llc_size;
  783. }
  784. struct microcode_ops * __init init_intel_microcode(void)
  785. {
  786. struct cpuinfo_x86 *c = &boot_cpu_data;
  787. if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
  788. cpu_has(c, X86_FEATURE_IA64)) {
  789. pr_err("Intel CPU family 0x%x not supported\n", c->x86);
  790. return NULL;
  791. }
  792. llc_size_per_core = calc_llc_size_per_core(c);
  793. return &microcode_intel_ops;
  794. }