intel.c 21 KB

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