intel.c 23 KB

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